Skip to content

Commit a968c0c

Browse files
committed
TextInput: event handlers get hostNode from event not ref
Avoid the event handlers depending on a React ref when the hostNode can be found on the event object.
1 parent 697479e commit a968c0c

File tree

1 file changed

+32
-28
lines changed
  • packages/react-native-web/src/exports/TextInput

1 file changed

+32
-28
lines changed

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

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -167,25 +167,27 @@ const TextInput: React.AbstractComponent<
167167
const dimensions = React.useRef({ height: null, width: null });
168168
const hostRef = React.useRef(null);
169169

170-
const handleContentSizeChange = React.useCallback(() => {
171-
const node = hostRef.current;
172-
if (multiline && onContentSizeChange && node != null) {
173-
const newHeight = node.scrollHeight;
174-
const newWidth = node.scrollWidth;
175-
if (newHeight !== dimensions.current.height || newWidth !== dimensions.current.width) {
176-
dimensions.current.height = newHeight;
177-
dimensions.current.width = newWidth;
178-
onContentSizeChange({
179-
nativeEvent: {
180-
contentSize: {
181-
height: dimensions.current.height,
182-
width: dimensions.current.width
170+
const handleContentSizeChange = React.useCallback(
171+
(hostNode) => {
172+
if (multiline && onContentSizeChange && hostNode != null) {
173+
const newHeight = hostNode.scrollHeight;
174+
const newWidth = hostNode.scrollWidth;
175+
if (newHeight !== dimensions.current.height || newWidth !== dimensions.current.width) {
176+
dimensions.current.height = newHeight;
177+
dimensions.current.width = newWidth;
178+
onContentSizeChange({
179+
nativeEvent: {
180+
contentSize: {
181+
height: dimensions.current.height,
182+
width: dimensions.current.width
183+
}
183184
}
184-
}
185-
});
185+
});
186+
}
186187
}
187-
}
188-
}, [hostRef, multiline, onContentSizeChange]);
188+
},
189+
[multiline, onContentSizeChange]
190+
);
189191

190192
const imperativeRef = React.useMemo(
191193
() => (hostNode) => {
@@ -201,7 +203,7 @@ const TextInput: React.AbstractComponent<
201203
hostNode.isFocused = function () {
202204
return hostNode != null && TextInputState.currentlyFocusedField() === hostNode;
203205
};
204-
handleContentSizeChange();
206+
handleContentSizeChange(hostNode);
205207
}
206208
},
207209
[handleContentSizeChange]
@@ -216,9 +218,10 @@ const TextInput: React.AbstractComponent<
216218
}
217219

218220
function handleChange(e) {
219-
const text = e.target.value;
221+
const hostNode = e.target;
222+
const text = hostNode.value;
220223
e.nativeEvent.text = text;
221-
handleContentSizeChange();
224+
handleContentSizeChange(hostNode);
222225
if (onChange) {
223226
onChange(e);
224227
}
@@ -228,26 +231,27 @@ const TextInput: React.AbstractComponent<
228231
}
229232

230233
function handleFocus(e) {
231-
const node = hostRef.current;
234+
const hostNode = e.target;
232235
if (onFocus) {
233-
e.nativeEvent.text = e.target.value;
236+
e.nativeEvent.text = hostNode.value;
234237
onFocus(e);
235238
}
236-
if (node != null) {
237-
TextInputState._currentlyFocusedNode = node;
239+
if (hostNode != null) {
240+
TextInputState._currentlyFocusedNode = hostNode;
238241
if (clearTextOnFocus) {
239-
node.value = '';
242+
hostNode.value = '';
240243
}
241244
if (selectTextOnFocus) {
242245
// Safari requires selection to occur in a setTimeout
243246
setTimeout(() => {
244-
node.select();
247+
hostNode.select();
245248
}, 0);
246249
}
247250
}
248251
}
249252

250253
function handleKeyDown(e) {
254+
const hostNode = e.target;
251255
// Prevent key events bubbling (see #612)
252256
e.stopPropagation();
253257

@@ -274,8 +278,8 @@ const TextInput: React.AbstractComponent<
274278
nativeEvent.text = e.target.value;
275279
onSubmitEditing(e);
276280
}
277-
if (shouldBlurOnSubmit && hostRef.current != null) {
278-
hostRef.current.blur();
281+
if (shouldBlurOnSubmit && hostNode != null) {
282+
hostNode.blur();
279283
}
280284
}
281285
}

0 commit comments

Comments
 (0)