Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Cross platform clearButtonMode for Android with React Native

Tags:

react-native

How should one implement the "X" to clear button in react native so that it works with Android as well as iOS. iOS has the text input option of "clearButtonMode" enum('never', 'while-editing', 'unless-editing', 'always').

To make it cross platform, do we need to just add an android conditional rendering of the clear button? Something like:

{Platform.OS === 'android' && <ClearTextButton />}

Seems a bit hacky so I am wondering if there is a cleaner method for this.

like image 995
MonkeyBonkey Avatar asked Jan 22 '17 15:01

MonkeyBonkey


People also ask

What is Clearbuttonmode in react native?

The clear button will remove all text from the input field.

How do I use onSubmitEditing react native?

onSubmitEditing is triggered when you click the text input submit button (keyboard button). onChangeText is triggered when you type any symbol in the text input. In your example, you will achieve what you need in both cases.

How do I change text input value in react native?

TextInput needs value that it is the value that is gonna be shown inside the TextInput. And to update that value you use onChangeText that is gonna call whatever function you specify every time the text into the TextInput change.


3 Answers

For your problem, you just need to create a simple button to handle the clear function of your input field and place it right next to your TextInput component to have the effect of clearButtonMode.

A naive implementation could be something like this:

  1. Create these states in your main component constructor :
    • A state for the status of your TextInput (is it touched?, does it have text yet?)
    • A state for the actual value of your TextInput, set your TextInput's value to this state.

For example:

this.state = {
    textInput1Status: 'untouched',
    textInput1Value: '',
};
  1. Create callback functions to set your states:
    • Create a callback function to set both your TextInput's value state and status state and assign it to the onChange prop of you TextInput.

For example:

<TextInput
    onChangeText={(text) => this.onTextInput1Change(text)}
    value={this.state.textInput1Value}
/>

...

onTextInput1Change(text) {
    this.setState({
        textInput1Status: 'touched',
        textInput1Value: text
    });
}
  1. Create your own button using TouchableOpacity and handle the clear function.

For example:

<TouchableOpacity onPress={this.clearText}>
    <Image
    style={styles.button}
    source={require('./myButton.png')}
    />
</TouchableOpacity>

...

clearText() {
    this.setState({
        textInput1Status: 'untouched',
        textInput1Value: '',
    });
}
  1. Handle the rendering of your "X" button:

For example:

renderClearButotn() {
    if (this.state.textInput1Status == 'touched') {
        return (
            <TouchableOpacity onPress={this.clearText}>
                <Image
                style={styles.button}
                source={require('./myButton.png')}
                />
            </TouchableOpacity>
        );
    } else {
        return '';
    }
}

...

render() {
    return (
        <TextInput
            onChangeText={(text) => this.onTextInput1Change(text)}
            value={this.state.textInput1Value}
        />
        {this.renderClearButton()}          
    );
}

In this way your code will be independent from both iOS and Android. I hope this could help you!

like image 143
HoangLM Avatar answered Sep 19 '22 20:09

HoangLM


There is another simple solution I found from this article. It works perfect for me in Android, and it is expected to give the same view and behavior in iOS also.

I had to modify the styles slightly to match with my UI

closeButtonParent: {
    justifyContent: 'center',
    alignItems: 'center',
    borderTopRightRadius: 5,
    borderBottomRightRadius: 5,
    backgroundColor: "#cdcdcd", 
    width: 30,
},

Code credit goes to https://www.codevscolor.com/react-native-text-input-clear-button/ auther

like image 25
kavinda Avatar answered Sep 19 '22 20:09

kavinda


This solution works ok but it's not the exact same effect than the clearButtonMode in iOS. The clearButtonMode won't dismiss the keyboard when clicked, and this solution for android will dispatch the Keyboard.dismiss event natively and there's no way to catch it, so the user needs to tap again on the input to get the keyboard back.

like image 30
Gus Rodriguez Avatar answered Sep 18 '22 20:09

Gus Rodriguez