Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit ab443e1

Browse files
태재영Matt Goo
태재영
authored and
Matt Goo
committed
feat(text-field): Sync inputComponent when component is updated (#848)
1 parent 73add93 commit ab443e1

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

packages/text-field/Input.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface InputProps<T extends HTMLElement = HTMLInputElement> {
2929
isValid?: boolean;
3030
foundation?: MDCTextFieldFoundation;
3131
handleValueChange?: (value: string | number | string[] | undefined, cb: () => void) => void;
32-
ref?: (inputInstance: Input<T>) => void,
32+
syncInput?: (inputInstance: Input<T>) => void,
3333
onBlur?: Pick<React.HTMLProps<T>, 'onBlur'>;
3434
onChange?: Pick<React.HTMLProps<T>, 'onChange'>;
3535
onFocus?: Pick<React.HTMLProps<T>, 'onFocus'>;
@@ -81,6 +81,7 @@ export default class Input<T extends HTMLElement = HTMLInputElement> extends Rea
8181
componentDidMount() {
8282
const {
8383
id,
84+
syncInput,
8485
disabled,
8586
value,
8687
setInputId,
@@ -89,6 +90,9 @@ export default class Input<T extends HTMLElement = HTMLInputElement> extends Rea
8990
foundation,
9091
isValid,
9192
} = this.props;
93+
if (syncInput) {
94+
syncInput(this);
95+
}
9296
if (setInputId && id) {
9397
setInputId(id!);
9498
}
@@ -257,6 +261,7 @@ export default class Input<T extends HTMLElement = HTMLInputElement> extends Rea
257261
/* eslint-disable no-unused-vars */
258262
className,
259263
foundation,
264+
syncInput,
260265
isValid,
261266
value,
262267
handleFocusChange,

packages/text-field/index.tsx

+2-7
Original file line numberDiff line numberDiff line change
@@ -275,18 +275,13 @@ class TextField<T extends HTMLElement = HTMLInputElement> extends React.Componen
275275
inputProps(child: React.ReactElement<InputProps<T>>) {
276276
// ref does exist on React.ReactElement<InputProps<T>>
277277
// @ts-ignore
278-
const {props, ref} = child;
278+
const {props} = child;
279279
return Object.assign({}, props, {
280280
foundation: this.state.foundation,
281281
handleFocusChange: (isFocused: boolean) => this.setState({isFocused}),
282282
setDisabled: (disabled: boolean) => this.setState({disabled}),
283283
setInputId: (id: string) => this.setState({inputId: id}),
284-
ref: (input: Input<T>) => {
285-
if (typeof ref === 'function') {
286-
ref(input);
287-
}
288-
this.inputComponent_ = input;
289-
},
284+
syncInput: (input: Input<T>) => (this.inputComponent_ = input),
290285
inputType: this.props.textarea ? 'textarea' : 'input',
291286
});
292287
}

test/unit/text-field/index.test.tsx

+18
Original file line numberDiff line numberDiff line change
@@ -696,3 +696,21 @@ test('Useless test for code coverage', () => {
696696
wrapper.instance().adapter.registerInputInteractionHandler(temp, temp);
697697
wrapper.instance().adapter.deregisterInputInteractionHandler(temp, temp);
698698
});
699+
700+
test('Input component sync test in TextField', () => {
701+
class TestComponent extends React.Component {
702+
state = {
703+
disabled: false,
704+
};
705+
render() {
706+
return <TextField>
707+
<Input disabled={this.state.disabled} />
708+
</TextField>;
709+
}
710+
}
711+
const wrapper = mount<TestComponent>(<TestComponent/>);
712+
// If inputComponent is null and disabled is true,
713+
// setDisabled called #inputAdapter.getNativeInput
714+
// and throw error because there is no inputComponent
715+
assert.doesNotThrow(() => wrapper.instance().setState({disabled: true}));
716+
});

0 commit comments

Comments
 (0)