I have created an application in react-native
and I have an option to chat in messages option. when I click inside TextInput and type two lines, the upper line gets hidden. To fix this I saw in the docs numberOfLines property but it did not work.
Here is my code:
<TextInput
ref='textInput'
multiline={true}
numberOfLines: {5}
onChangeText={this.onChangeText}
style={[styles.textInput, {height: context.props.textInputHeight}]}
placeholder={context.props.placeholder}
placeholderTextColor="#5A5A5A"
value={context.state.text}/>
I tried it in getDefaultProps
function too:
getDefaultProps() {
return {
placeholder: 'Type a message...',
navHeight: 70,
textInputHeight: 44,
numberOfLines:5,
maxHeight: Screen.height,
};
},
But did not worked.
Wrap the View component that is the parent of the TextInput in a Pressable component and then pass Keyboard. dismiss to the onPress prop. So, if the user taps anywhere outside the TextInput field, it will trigger Keyboard. dismiss , which will result in the TextInput field losing focus and the keyboard being hidden.
We are going to use onContentSizeChange prop provided by TextInput to watch input box for content size changes and accordingly call our updateSize function which will update the height of our TextInput. In our render process we are going to use the height to create anew style to use with our TextInput.
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.
To resume:
<TextInput
...
numberOfLines={Platform.OS === 'ios' ? null : numberOfLines}
minHeight={(Platform.OS === 'ios' && numberOfLines) ? (20 * numberOfLines) : null}
/>
You can use maxHeight
and minHeight
to accept what you want. For standard text fontSize, giving maxHeight={60}
will make TextInput scrollable after 3 lines. This is good for IOS - for Android you can use numberOfLines
prop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With