Mainly to achieve the following functions:
- When the keyboard pops up, the
TextInputwill automatically adjust to the top of the keyboard. - When the keyboard pops up, the content of the
ScrollViewwill not be obscured by the keyboard. - When multiline
TextInputgets focus, the selected cursor will be automatically adjusted to the top of the keyboard. - When the multiline
TextInputcreate new line, the new line will automatically adjust to the top of the keyboard. - Put your finger on top of
TextInputand slideScrollView, when you lift your finger, theTextInputwill not get focus.
npm
$ npm install react-native-input-scroll-view --saveyarn
$ yarn add react-native-input-scroll-viewimport InputScrollView from 'react-native-input-scroll-view';
...
state = {
text: '',
};
render() {
const { text } = this.state;
return (
<InputScrollView>
<TextInput />
<TextInput />
<TextInput value={text}
onChangeText={text => this.setState({ text })}
multiline />
</InputScrollView>
);
}React-native-input-scroll-view automatically modify onContentSizeChange, onSelectionChange, and onChange TextInput props. It is not yet designed to pass them down if the TextInput is wrapped into another component so don’t forget to do it:
import InputScrollView from 'react-native-input-scroll-view';
...
const MyComponent = props => (
<View>
<TextInput {...props} />
</View>
);
...
state = {
text: '',
};
render() {
const { text } = this.state;
return (
<InputScrollView>
<MyComponent value={text}
onChangeText={text => this.setState({ text })}
/>
</InputScrollView>
);
}Note that if the cursor is to be correctly adjusted to the top of the keyboard, you must bind value to TextInput.
If your ReactNative version is on or above v0.57, skip this section.
Before a certain version of ReactNative, multiline TextInput height on an Android device could not change properly based on its content, so we need to add additional processing code
import InputScrollView from 'react-native-input-scroll-view';
...
state = {
text: '',
textareaHeight: null,
};
render() {
const { text, textareaHeight } = this.state;
return (
<InputScrollView>
<TextInput />
<TextInput />
<TextInput style={{ height: textareaHeight }}
value={text}
onChangeText={text => this.setState({ text })}
onContentSizeChange={this._onContentSizeChange}
multiline />
</InputScrollView>
);
}
_onContentSizeChange = ({nativeEvent:event}) => {
this.setState({ textareaHeight: event.contentSize.height });
};"react": "^16.0.0-alpha.12"
"react-native": ">=0.46.0"
MIT



