mirror of
https://github.com/ant-design/ant-design.git
synced 2025-01-18 14:13:37 +08:00
fix: Textarea value works differently as Input (#26652)
This commit is contained in:
parent
ad180b8dcf
commit
6fc11830d6
@ -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) {
|
||||
|
@ -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', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user