Skip to content

Commit 67979b7

Browse files
awinogradnecolas
authored andcommitted
[add] TextInput support for "onContentSizeChange"
Fix #793 Close #1226
1 parent 3d3ea9a commit 67979b7

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ React Native v0.55
147147
| SwipeableListView || |
148148
| Switch || |
149149
| Text || Missing `onLongPress` ([#1011](https://github.com/necolas/react-native-web/issues/1011)) and `numberOfLines` ([#13](https://github.com/necolas/react-native-web/issues/13)) support. |
150-
| TextInput || Missing `onContentSizeChange` ([#793](https://github.com/necolas/react-native-web/issues/793)), rich text features ([#1023](https://github.com/necolas/react-native-web/issues/1023)), and auto-expanding behaviour ([#795](https://github.com/necolas/react-native-web/issues/795)). |
150+
| TextInput || Missing rich text features ([#1023](https://github.com/necolas/react-native-web/issues/1023)), and auto-expanding behaviour ([#795](https://github.com/necolas/react-native-web/issues/795)). |
151151
| Touchable || Includes additional support for mouse and keyboard interactions. |
152152
| TouchableHighlight || |
153153
| TouchableNativeFeedback || Not started ([#1024](https://github.com/necolas/react-native-web/issues/1024)). |

packages/react-native-web/src/exports/TextInput/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ const setSelection = (node, selection) => {
6868

6969
class TextInput extends Component<*> {
7070
_node: HTMLInputElement;
71+
_nodeHeight: number;
72+
_nodeWidth: number;
7173

7274
static displayName = 'TextInput';
7375

@@ -288,9 +290,30 @@ class TextInput extends Component<*> {
288290
}
289291
};
290292

293+
_handleContentSizeChange = () => {
294+
const { onContentSizeChange, multiline } = this.props;
295+
if (multiline && onContentSizeChange) {
296+
const newHeight = this._node.scrollHeight;
297+
const newWidth = this._node.scrollWidth;
298+
if (newHeight !== this._nodeHeight || newWidth !== this._nodeWidth) {
299+
this._nodeHeight = newHeight;
300+
this._nodeWidth = newWidth;
301+
onContentSizeChange({
302+
nativeEvent: {
303+
contentSize: {
304+
height: this._nodeHeight,
305+
width: this._nodeWidth
306+
}
307+
}
308+
});
309+
}
310+
}
311+
};
312+
291313
_handleChange = e => {
292314
const { onChange, onChangeText } = this.props;
293315
const { text } = e.nativeEvent;
316+
this._handleContentSizeChange();
294317
if (onChange) {
295318
onChange(e);
296319
}
@@ -389,6 +412,9 @@ class TextInput extends Component<*> {
389412

390413
_setNode = component => {
391414
this._node = findNodeHandle(component);
415+
if (this._node) {
416+
this._handleContentSizeChange();
417+
}
392418
};
393419
}
394420

packages/website/storybook/1-components/TextInput/TextInputScreen.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ const TextInputScreen = () => (
206206
description="Callback that is called when the text input's text changes. The text is passed as an argument to the callback handler."
207207
/>
208208

209+
<DocItem
210+
name="onContentSizeChange"
211+
typeInfo="?function"
212+
description="Callback that is called when the text input's content size changes. This will be called with { nativeEvent: { contentSize: { width, height } } }. Only called for multiline text inputs."
213+
/>
214+
209215
<DocItem
210216
name="onFocus"
211217
typeInfo="?function"

0 commit comments

Comments
 (0)