Skip to content

Commit

Permalink
Fixed <TextInput>.onContentSizeChange on Android
Browse files Browse the repository at this point in the history
Summary:
Previously <TextInput>'s onContentSizeChange event fires very rearly, usually just once after initial layout. This diff fixed that.
I also considered to a bunch of another things to get the native notification, but I found that overriding `onTextChanged` is the most reliable, easy and effitient way to implement this.

I tried/considered:
 * onLayout (does not fire)
 * OnPreDrawListener (fires to often)
 * OnGlobalLayoutListener (does not fire)
 * OnLayoutChangeListener (does not fire)
 * isLayoutRequested (too hacky)

(I also fixed the <AutoExpandingTextInput> demo to illustrate the fix.)

And just heads up, we will remove `contentSize` info from `onChange` event very soon.

GH issue: facebook#11692

Reviewed By: achen1

Differential Revision: D5132589

fbshipit-source-id: e7edbd8dc5ae891a6f4a87b51d9450b8c6ce4a1e
  • Loading branch information
shergin authored and facebook-github-bot committed May 26, 2017
1 parent 864c9f5 commit 3539352
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 5 additions & 6 deletions RNTester/js/TextInputExample.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class TextEventsExample extends React.Component {
onChange={(event) => this.updateText(
'onChange text: ' + event.nativeEvent.text
)}
onContentSizeChange={(event) => this.updateText(
'onContentSizeChange size: ' + event.nativeEvent.contentSize
)}
onEndEditing={(event) => this.updateText(
'onEndEditing text: ' + event.nativeEvent.text
)}
Expand All @@ -71,7 +74,6 @@ class AutoExpandingTextInput extends React.Component {
constructor(props) {
super(props);
this.state = {
text: 'React Native enables you to build world-class application experiences on native platforms using a consistent developer experience based on JavaScript and React. The focus of React Native is on developer efficiency across all the platforms you care about — learn once, write anywhere. Facebook uses React Native in multiple production apps and will continue investing in React Native.',
height: 0,
};
}
Expand All @@ -83,11 +85,7 @@ class AutoExpandingTextInput extends React.Component {
onContentSizeChange={(event) => {
this.setState({height: event.nativeEvent.contentSize.height});
}}
onChangeText={(text) => {
this.setState({text});
}}
style={[styles.default, {height: Math.max(35, this.state.height)}]}
value={this.state.text}
style={[styles.default, {height: Math.min(200, Math.max(35, this.state.height))}]}
/>
);
}
Expand Down Expand Up @@ -619,6 +617,7 @@ exports.examples = [
<View>
<AutoExpandingTextInput
placeholder="height increases with content"
defaultValue="React Native enables you to build world-class application experiences on native platforms using a consistent developer experience based on JavaScript and React. The focus of React Native is on developer efficiency across all the platforms you care about — learn once, write anywhere. Facebook uses React Native in multiple production apps and will continue investing in React Native."
enablesReturnKeyAutomatically={true}
returnKeyType="done"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,10 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
listener.onTextChanged(s, start, before, count);
}
}

if (mContentSizeWatcher != null) {
mContentSizeWatcher.onLayout();
}
}

@Override
Expand Down

0 comments on commit 3539352

Please sign in to comment.