2

I have just started with react native, and wanted to develop a custom dialog box with - a text input , basically like a prompt but it should work with both android and ios . The one currently available in react native documentation just works for ios . Any way to achieve this implementation ?

1

1 Answer 1

2

You can use several libraries for this, but if you want to do this using out of the box components you can use the Modal from React Native and customize it.

You can create something like this

const ModalInput = ({ onTextChange, onSubmit, visible, value, toggle }) => {
  return (
    <Modal visible={visible} transparent={true} style={{justifyContent:'center'}}>
      <View
        style={{
          height: 100,
          padding: 20,
          width: '80%',
          alignSelf: 'center',
          justifyContent: 'center',
          backgroundColor: 'white',
        }}>
        <TextInput
          value={value}
          onChangeText={onTextChange}
          placeholder={'Enter text'}
        />
        <View style={{ flexDirection: 'row', alignSelf: 'center' }}>
          <Button title="close" onPress={toggle} />
          <Button title="ok" onPress={onSubmit} />
        </View>
      </View>
    </Modal>
  );
};

And use it anywhere you need.

you need two states for text and visibility

const [visible, setVisible] = useState(false);
  const [text, onTextChange] = useState('');

And usage would be like below

  <ModalInput
    visible={visible}
    value={text}
    onTextChange={onTextChange}
    toggle={() => setVisible(!visible)}
    onSubmit={() => setVisible(!visible)}
  />

I have done a very basic styling, you can style it based on your requirement and even pass the text on press of the ok button, you have full control as it will be your control and style it anyway you like.

If you want to have an overlay background check this answer https://stackoverflow.com/a/62039814/1435722

If you want to test it out, you can use the snack (Switch to Andriod or IOS) https://snack.expo.io/@guruparan/custommodal

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks , could you also recommend some libraries which provide this functionality ?
Also , how can i blur the background when the dialog box is displayed ?
This is a good library npmjs.com/package/react-native-dialog. And for background you can use the same way as the answer I have provided. To have a wrapper view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.