From 96a1772f938a14e64861c2dd5087809f5ce6f9bd Mon Sep 17 00:00:00 2001 From: Damian Stasik Date: Sat, 11 Feb 2023 00:58:33 +0100 Subject: [PATCH] fix: handle missing ref in TextInput component --- src/components/TextInput/TextInput.react.js | 9 ++------- src/lib/withForwardedRef.js | 4 +++- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/components/TextInput/TextInput.react.js b/src/components/TextInput/TextInput.react.js index 3ba85e59af..38149edf12 100644 --- a/src/components/TextInput/TextInput.react.js +++ b/src/components/TextInput/TextInput.react.js @@ -11,15 +11,10 @@ import styles from 'components/TextInput/TextInput.scss'; import { withForwardedRef } from 'lib/withForwardedRef'; class TextInput extends React.Component { - componentWillReceiveProps(props) { + componentDidUpdate(props) { if (props.multiline !== this.props.multiline) { const node = props.forwardedRef.current; - // wait a little while for component to re-render - setTimeout(function() { - node.focus(); - node.value = ''; - node.value = props.value; - }.bind(this), 1); + node.focus(); } } diff --git a/src/lib/withForwardedRef.js b/src/lib/withForwardedRef.js index 27615efe37..8b76c1333c 100644 --- a/src/lib/withForwardedRef.js +++ b/src/lib/withForwardedRef.js @@ -2,7 +2,9 @@ import React from 'react'; export function withForwardedRef(Component) { function render(props, ref) { - return ; + const innerRef = React.useRef(); + React.useImperativeHandle(ref, () => innerRef.current, [props.multiline]); + return ; } const name = Component.displayName || Component.name;