react-native icon indicating copy to clipboard operation
react-native copied to clipboard

TextInput: add ability to set/clear the text

Open ryanflorence opened this issue 11 years ago • 10 comments

After you handle onSubmitText there's no way to clear (or change) the value of the input w/o making a controlled component (and that makes it a pretty terrible ux).

ryanflorence avatar Mar 28 '15 14:03 ryanflorence

cc @sahrens

vjeux avatar Mar 28 '15 14:03 vjeux

If you setState to re-render the component and pass in the string you want to the value prop it should update to reflect that. If that's not the case, can you provide a code snippet that shows the bug?

sahrens avatar Mar 28 '15 20:03 sahrens

        <TextInput
          style={styles.input}
          value={this.state.inputValue}
          controlled={true}
          onChangeText={(text) => this.setState({ inputValue: text })}
          onSubmitEditing={this.handleSubmitEditing.bind(this)}
        />

I've got that, and it works, but it is a really, really, really terrible ux.

If you really want this to behave as a controlled component, you can set this true, but you will probably see flickering, dropped keystrokes, and/or laggy typing, depending on how you process onChange events.

http://facebook.github.io/react-native/docs/textinput.html#controlled

How else can I process an onChange event?

ryanflorence avatar Mar 29 '15 02:03 ryanflorence

huh ... I closed the chrome debugger and now the UI is nice and responsive, where before it dropped characters and flickered a lot.

ryanflorence avatar Mar 29 '15 02:03 ryanflorence

The chrome debugger over websockets adds a fair bit of latency :(

You should be able to clear/reset the value without setting controlled={true}, it just won't fully control the component. Does that work at all for you?

On Mar 28, 2015, at 7:36 PM, Ryan Florence [email protected] wrote:

huh ... I closed the chrome debugger and now the UI is nice and responsive, where before it dropped characters and flickered a lot.

— Reply to this email directly or view it on GitHub.

sahrens avatar Mar 29 '15 03:03 sahrens

oh, turns out removing controlled is what fixed it, not the debugger, I didn't realize I'd removed it, sorry for the noise :)

ryanflorence avatar Mar 29 '15 03:03 ryanflorence

For future reference, if you don't want to use controlled, another way to clear is to set a ref on the TextInput then call this.refs.yourTextInputRef.setNativeProps({text: ''})

brentvatne avatar Jun 04 '15 19:06 brentvatne

On React native, i have clear text ref as below:

           <TextInput
              style={styles.input}
              autoCapitalize="none"
              placeholder="Invitees"
              keyboardType="email-address"
              clearButtonMode="while-editing"
              ref={element => {
                this.attendee = element
              }}
              onSubmitEditing={this.handleAddPress}
            />
this.attendee.setNativeProps({ text: '' })

It's work fine for me no need refs.

Thanks,

tuanluu-agilityio avatar Jan 31 '18 04:01 tuanluu-agilityio

@tuanluu-agilityio this is working on ios for you? I try it but only works on Android for me. any idea?

jevvilla avatar Apr 18 '18 15:04 jevvilla

I am not able to use the .clear() function of the TextInput component while it is working on Android, has anyone a workaround about this?

josepbordesjove avatar May 10 '18 14:05 josepbordesjove