fix: Textarea value works differently as Input (#26652)

This commit is contained in:
偏右 2020-09-09 14:41:00 +08:00 committed by GitHub
parent ad180b8dcf
commit 6fc11830d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 6 deletions

View File

@ -13,6 +13,8 @@ export interface TextAreaProps extends RcTextAreaProps {
export interface TextAreaState {
value: any;
/** `value` from prev props */
prevValue: any;
}
class TextArea extends React.Component<TextAreaProps, TextAreaState> {
@ -25,16 +27,17 @@ class TextArea extends React.Component<TextAreaProps, TextAreaState> {
const value = typeof props.value === 'undefined' ? props.defaultValue : props.value;
this.state = {
value,
// eslint-disable-next-line react/no-unused-state
prevValue: props.value,
};
}
static getDerivedStateFromProps(nextProps: TextAreaProps) {
if (nextProps.value !== undefined) {
return {
value: nextProps.value,
};
static getDerivedStateFromProps(nextProps: TextAreaProps, { prevValue }: TextAreaState) {
const newState: Partial<TextAreaState> = { prevValue: nextProps.value };
if (nextProps.value !== undefined || prevValue !== nextProps.value) {
newState.value = nextProps.value;
}
return null;
return newState;
}
setValue(value: string, callback?: () => void) {

View File

@ -124,6 +124,14 @@ describe('TextArea', () => {
}),
);
});
it('should works same as Input', async () => {
const input = mount(<Input value="111" />);
const textarea = mount(<TextArea value="111" />);
input.setProps({ value: undefined });
textarea.setProps({ value: undefined });
expect(textarea.getDOMNode().value).toBe(input.getDOMNode().value);
});
});
describe('TextArea allowClear', () => {