diff --git a/packages/text-field/Input.tsx b/packages/text-field/Input.tsx index 4fd0dabce..1c84f95f3 100644 --- a/packages/text-field/Input.tsx +++ b/packages/text-field/Input.tsx @@ -29,7 +29,7 @@ export interface InputProps { isValid?: boolean; foundation?: MDCTextFieldFoundation; handleValueChange?: (value: string | number | string[] | undefined, cb: () => void) => void; - ref?: (inputInstance: Input) => void, + syncInput?: (inputInstance: Input) => void, onBlur?: Pick, 'onBlur'>; onChange?: Pick, 'onChange'>; onFocus?: Pick, 'onFocus'>; @@ -81,6 +81,7 @@ export default class Input extends Rea componentDidMount() { const { id, + syncInput, disabled, value, setInputId, @@ -89,6 +90,9 @@ export default class Input extends Rea foundation, isValid, } = this.props; + if (syncInput) { + syncInput(this); + } if (setInputId && id) { setInputId(id!); } @@ -257,6 +261,7 @@ export default class Input extends Rea /* eslint-disable no-unused-vars */ className, foundation, + syncInput, isValid, value, handleFocusChange, diff --git a/packages/text-field/index.tsx b/packages/text-field/index.tsx index 0b1192e0e..9f2bbd452 100644 --- a/packages/text-field/index.tsx +++ b/packages/text-field/index.tsx @@ -275,18 +275,13 @@ class TextField extends React.Componen inputProps(child: React.ReactElement>) { // ref does exist on React.ReactElement> // @ts-ignore - const {props, ref} = child; + const {props} = child; return Object.assign({}, props, { foundation: this.state.foundation, handleFocusChange: (isFocused: boolean) => this.setState({isFocused}), setDisabled: (disabled: boolean) => this.setState({disabled}), setInputId: (id: string) => this.setState({inputId: id}), - ref: (input: Input) => { - if (typeof ref === 'function') { - ref(input); - } - this.inputComponent_ = input; - }, + syncInput: (input: Input) => (this.inputComponent_ = input), inputType: this.props.textarea ? 'textarea' : 'input', }); } diff --git a/test/unit/text-field/index.test.tsx b/test/unit/text-field/index.test.tsx index c2c1d35fa..fd9770bda 100644 --- a/test/unit/text-field/index.test.tsx +++ b/test/unit/text-field/index.test.tsx @@ -696,3 +696,21 @@ test('Useless test for code coverage', () => { wrapper.instance().adapter.registerInputInteractionHandler(temp, temp); wrapper.instance().adapter.deregisterInputInteractionHandler(temp, temp); }); + +test('Input component sync test in TextField', () => { + class TestComponent extends React.Component { + state = { + disabled: false, + }; + render() { + return + + ; + } + } + const wrapper = mount(); + // If inputComponent is null and disabled is true, + // setDisabled called #inputAdapter.getNativeInput + // and throw error because there is no inputComponent + assert.doesNotThrow(() => wrapper.instance().setState({disabled: true})); +});