I'm using React Native's TextInput. I want to disable return key as per the requirement. Is there any way to disable/enable return key by custom code?
I tried with enablesReturnKeyAutomatically but it doesn't work.
Yes we can, by using the onSubmitEditing props for TextInput. Posting sample here:
<TextInput
value={password}
returnKeyType="done"
onSubmitEditing={() => {
if (password != '') {
onLoginPressed();
}
}}
placeholder="Password"
sub_place="PASSWORD"
onChangeText={value => setPassword(value)}
blurOnSubmit={false}
ref={passwordFocus}
/>
Related
This is the text input that I am using, I want the input field to be disabled after entering a value.I tried using editable props, but it did not help.
I am completely new to react native, please help with an example.
<View style={editProfileStyle.textinputView}>
<TextInput
style={editProfileStyle.textInput}
placeholder="Enter your Specialization"
value={this.state.subQualification}
onChangeText={subQualification => this.setState({ subQualification: subQualification })}
/>
</View>
As per the question, since the field should get disabled when it has some value:
<View style={editProfileStyle.textinputView}>
<TextInput
editable={this.state.subQualification.length === 0}
style={editProfileStyle.textInput}
placeholder="Enter your Specialization"
value={this.state.subQualification}
onChangeText={subQualification => this.setState({ subQualification: subQualification })}
/>
</View>
using check in your editable prop of
editable={this.state.subQualification.length === 0} will make field editable when nothing in present in the field
Try to add state and modify your textInput props like this:
state ={
textDisable: true
}
render() {
return (
<View style={editProfileStyle.textinputView}>
<TextInput
style={editProfileStyle.textInput}
placeholder="Enter your Specialization"
value={this.state.subQualification}
onChangeText={subQualification => this.setState({ subQualification: subQualification })}
editable={this.state.textDisable}
onEndEditing={() => this.setState({textDisable: false})}
/>
</View>
);
}
}
after onsubmit the input should be disable.
I need to show Next button in Keyboard in React Native application when there are multiple TextInput fields are there.
Could anyone suggest, I have tried with returnKeyType = {"next"} , blurOnSubmit and onSubmitEditing props but no use.
Could anyone you please provided the solution for this?
Thanks in Advance.
Try the same without {}. returnKeyType = "next". This worked for me.
You can try onSubmitEditing as described in the docs. This method will be called when you click done on the keyboard. In the listner, use focus method to focus the required textInput.
render(){
return(
...
<TextInput
ref={(input) => this._username = input}
onSubmitEditing={() => this._email.focus()}
value={this.state.text}
blurOnSubmit={false} // prevent keyboard flickering
/>
<TextInput
ref={(input) => this._email = input}
onSubmitEditing={() => this._password.focus()} // or submit the form or focus next input and goes on
value={this.state.text}
blurOnSubmit={false}
/>
)
}
<TextInput
placeholder="Full Name"
onChangeText={userName => this.setState({ userName })}
onChangeText={text => this.validate(text, "username")}
underlineColorAndroid="transparent"
style={[
styles.TextInputStyleClass,
!this.state.nameValidate ? styles.error : null
]}
blurOnSubmit={false}
autoFocus={true}
autoCorrect={true}
autoCapitalize="none"
maxLength={25}
/>
in the above code, I use two Onchagnetext events but only on event work that calls validation another is not working means not take value. why how to fix it.
how can I use two Onchangetext events?
You don't need two onChangeText method.
If you want to validate and use setState together then you can do
...
onChangeText={userName => this.setState({ userName },
() => this.validate(username, 'username)})}
The callback in setState ensures that you are calling the method once the setState has finished updating the state
I have just placed TextInput KeyboardType ={"numeric"} just trying to find weather user enter '-' if means I have to event.preventDefault() in OnChange first.Later tried in onKeyPress but onKeyPress is not trigerring , I was just tried to do that but it allowing me to type shows warning Synthetic Event Performance issue.
guys help me .
You are testing with android mobile whereasonKeyPress method is for iOS, check documentation for same
You can use onChangeText for text change.
Example:
<TextInput
onChangeText={(text) => this.onFirstNameText(text)}
returnKeyType={'next'}
underlineColorAndroid='transparent'
style={styles.inputColLeft}
/>
Edit 2018
onKeyPress is now also supported for Android devices as per this commit.
From doc:
Note: on Android only the inputs from soft keyboard are handled, not
the hardware keyboard inputs.
const App =()=>{
const handleKeyPress = ({ nativeEvent: { key: keyValue } }) => {
console.log(keyValue);
if(keyValue === 'Enter')
{
console.log("enter");
}
};
return(
<View>
<TextInput
placeholder="Your Password"
placeholderTextColor="#666666"
style={styles.inputSearch}
// onEndEditing={(e)=>handleValidUser(e.nativeEvent.text)}
onKeyPress={handleKeyPress}
/>
</View>
)
}
Use onChangeText like this:
<TextInput onChangeText = {(text) => this.getPhone(text)} keyboardType="numeric" autoCapitalize="none" autoCorrect={false} onSubmitEditing={ () => this.addressInput.focus()} style={styles.input} placeholder="phone" placeholderTextColor="rgba(255,255,255,0.6)" underlineColorAndroid="rgba(0,0,0,0.0)" returnKeyType="next"/>
getPhone = (text) => {
this.setState({phone: text})
//use your logic here
}
Here is the solution to this issue of synthetic event:
Input element:
<Input onKeyPress = {this.keyPress} />
keyPress function
keyPress = (event) => {
/* this line will solve your error */
event.persist();
console.log(event);
}
What is the proper way to switch to the next TextInput in React-Native >0.44.0?
I tried it with refs and .focus(), but this didn't work. All answers I found were outdated.
You should specify ref to the next textinput. Try this
<TextInput
placeholder="first"
placeholderTextColor="rgba(0,0,0,0.5)"
returnKeyType="next"
onSubmitEditing={() => this.second.focus()}
/>
<TextInput
placeholder="second"
placeholderTextColor="rgba(0,0,0,0.5)"
returnKeyType="done"
ref={input => (this.second = input)} // This line is important
/>