TextArea should be resetable in form, close #6776

pull/6802/head
afc163 8 years ago
parent e6554e7edb
commit b112948130

@ -110,6 +110,11 @@ export default class TextArea extends React.Component<TextAreaProps & HTMLTextar
...props.style, ...props.style,
...this.state.textareaStyles, ...this.state.textareaStyles,
}; };
// Fix https://github.com/ant-design/ant-design/issues/6776
// Make sure it could be reset when using form.getFieldDecorator
if ('value' in otherProps) {
otherProps.value = otherProps.value || '';
}
return ( return (
<textarea <textarea
{...otherProps} {...otherProps}

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { mount } from 'enzyme'; import { mount } from 'enzyme';
import Input from '..'; import Input from '..';
import Form from '../../form';
const { TextArea } = Input; const { TextArea } = Input;
@ -20,3 +21,36 @@ describe('TextArea', () => {
expect(mockFunc).toHaveBeenCalledTimes(2); expect(mockFunc).toHaveBeenCalledTimes(2);
}); });
}); });
describe('As Form Control', () => {
it('should be reset when wrapped in form.getFieldDecorator without initialValue', async () => {
class Demo extends React.Component {
reset = () => {
this.props.form.resetFields();
}
render() {
const { getFieldDecorator } = this.props.form;
return (
<Form>
<Form.Item>
{getFieldDecorator('input')(<Input />)}
</Form.Item>
<Form.Item>
{getFieldDecorator('textarea')(<Input.TextArea />)}
</Form.Item>
<button onClick={this.reset}>reset</button>
</Form>
);
}
}
const DemoForm = Form.create()(Demo);
const wrapper = mount(<DemoForm />);
wrapper.find('input').simulate('change', { target: { value: '111' } });
wrapper.find('textarea').simulate('change', { target: { value: '222' } });
expect(wrapper.find('input').prop('value')).toBe('111');
expect(wrapper.find('textarea').prop('value')).toBe('222');
wrapper.find('button').simulate('click');
expect(wrapper.find('input').prop('value')).toBe('');
expect(wrapper.find('textarea').prop('value')).toBe('');
});
});

Loading…
Cancel
Save