Skip to content

Commit 7d618d7

Browse files
authored
Merge pull request #846 from yexiaosong/master
input添加trim属性, 以优化带有输入框的表单提交
2 parents 810306f + e83a3a7 commit 7d618d7

File tree

5 files changed

+38
-25
lines changed

5 files changed

+38
-25
lines changed

site/docs/en-US/input.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ render() {
339339
|resize| control the resizability | string | none, both, horizontal, vertical ||
340340
|autoFocus | same as `autofocus` in native input | boolean || false |
341341
| onIconClick | hook function when clicking on the input icon | function |||
342+
| trim | trim input contents | boolean || false |
342343

343344
### Autocomplete Attributes
344345

site/docs/zh-CN/input.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ render() {
460460
| resize | 控制是否能被用户缩放 | string | none, both, horizontal, vertical ||
461461
| autoFocus | 原生属性,自动获取焦点 | boolean | true, false | false |
462462
| onIconClick | 点击 Input 内的图标的钩子函数 | function |||
463+
| trim | 对input内容进行trim | boolean || false |
463464

464465
### Autocomplete Attributes
465466

src/checkbox/__test__/Checkbox_test.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ test('should render checked checkbox if checkboxGroup value contains the same la
5353
test('should display indeterminate mark when checkbox is an indeterminate state', () => {
5454
const wrapper = mount(
5555
<Checkbox
56-
indeterminate={true}/>
56+
indeterminate={true} />
5757
)
5858
expect(wrapper.find('.el-checkbox__input').hasClass('is-indeterminate')).toEqual(true)
5959
})
@@ -77,7 +77,7 @@ test('should limited to max and min value', () => {
7777
)
7878

7979
//test checked length
80-
expect(wrapper.find('.el-checkbox__input .is-checked').length).toBe(2)
80+
expect(wrapper.find('.el-checkbox .el-checkbox__input.is-checked').length).toBe(2)
8181

8282

8383
const Input = document.createElement('input');
@@ -87,7 +87,7 @@ test('should limited to max and min value', () => {
8787
wrapper.find('input[type="checkbox"]').forEach(e => {
8888
if (!e.prop('checked')) {
8989
e.simulate('change', { target: Input });
90-
expect(wrapper.find('.el-checkbox__input .is-checked').length).toBe(2)
90+
expect(wrapper.find('.el-checkbox__input.is-checked').length).toBe(2)
9191
}
9292
})
9393

@@ -96,7 +96,7 @@ test('should limited to max and min value', () => {
9696
wrapper.find('input[type="checkbox"]').forEach(e => {
9797
if (e.prop('checked')) {
9898
e.simulate('change', { target: Input })
99-
expect(wrapper.find('.el-checkbox__input .is-checked').length).toBe(1)
99+
expect(wrapper.find('.el-checkbox__input.is-checked').length).toBe(1)
100100
}
101101
})
102102
})

src/input/Input.jsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,19 @@ export default class Input extends Component {
6969
}
7070

7171
handleBlur(e: SyntheticEvent): void {
72-
const { onBlur } = this.props;
72+
const { onBlur } = this.props
73+
if (this.props.trim) this.handleTrim()
7374
if (onBlur) onBlur(e)
7475
}
7576

77+
handleTrim(): void {
78+
this.refs.input.value = this.refs.input.value.trim()
79+
if(this.props.onChange) {
80+
// this's for controlled components
81+
this.props.onChange(this.refs.input.value.trim())
82+
}
83+
}
84+
7685
handleIconClick(): void {
7786
if (this.props.onIconClick) {
7887
this.props.onIconClick()
@@ -170,6 +179,7 @@ Input.propTypes = {
170179
minLength: PropTypes.number,
171180
defaultValue: PropTypes.any,
172181
value: PropTypes.any,
182+
trim: PropTypes.bool,
173183

174184
// type !== 'textarea'
175185
size: PropTypes.oneOf(['large', 'small', 'mini']),

src/switch/__test__/switch_test.jsx

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ test('Basic usage', () => {
1111
</Switch>
1212
);
1313

14-
expect(switch1.find('div .el-switch__label').at(0).text()).toEqual('');
15-
expect(switch1.find('div .el-switch__label').at(1).text()).toEqual('');
16-
expect(switch1.find('label .el-switch').at(0).hasClass('is-checked')).toEqual(true);
14+
expect(switch1.find('label.el-switch .el-switch__label').at(0).text()).toEqual('');
15+
expect(switch1.find('label.el-switch .el-switch__label').at(1).text()).toEqual('');
16+
expect(switch1.find('label.el-switch').hasClass('is-checked')).toEqual(true);
1717

1818
// switch off
1919
switch1.find('input').simulate('change', {target: {value: false}});
2020

21-
expect(switch1.find('div .el-switch__label').at(0).text()).toEqual('');
22-
expect(switch1.find('div .el-switch__label').at(1).text()).toEqual('');
23-
expect(switch1.find('label .el-switch').at(0).hasClass('is-checked')).toEqual(false);
21+
expect(switch1.find('label.el-switch .el-switch__label').at(0).text()).toEqual('');
22+
expect(switch1.find('label.el-switch .el-switch__label').at(1).text()).toEqual('');
23+
expect(switch1.find('label.el-switch').at(0).hasClass('is-checked')).toEqual(false);
2424

2525
const switch2 = mount(
2626
<Switch
@@ -30,20 +30,21 @@ test('Basic usage', () => {
3030
</Switch>
3131
);
3232

33-
expect(switch2.find('label .el-switch').at(0).hasClass('is-checked')).toEqual(true);
34-
expect(switch2.find('div .el-switch__label').at(0).text()).toEqual('ON');
35-
expect(switch2.find('div .el-switch__label').at(1).text()).toEqual('OFF');
36-
expect(switch2.find('div .el-switch__label').at(0).prop('style').display).toEqual(undefined);
37-
expect(switch2.find('div .el-switch__label').at(1).prop('style').display).toEqual('none');
33+
expect(switch2.find('label.el-switch').at(0).hasClass('is-checked')).toEqual(true);
34+
expect(switch2.find('label.el-switch .el-switch__label').at(0).text()).toEqual('ON');
35+
expect(switch2.find('label.el-switch .el-switch__label').at(1).text()).toEqual('OFF');
36+
expect(switch2.find('label.el-switch .el-switch__label').at(0).prop('style').display).toEqual(undefined);
37+
expect(switch2.find('label.el-switch .el-switch__label').at(1).prop('style').display).toEqual('none');
3838

3939
// switch off
4040
switch2.find('input[type="checkbox"]').simulate('change', {target: {value: false}});
4141

42-
expect(switch2.find('label .el-switch').at(0).hasClass('is-checked')).toEqual(false);
43-
expect(switch2.find('div .el-switch__label').at(0).text()).toEqual('ON');
44-
expect(switch2.find('div .el-switch__label').at(1).text()).toEqual('OFF');
45-
expect(switch2.find('div .el-switch__label').at(0).prop('style').display).toEqual('none');
46-
expect(switch2.find('div .el-switch__label').at(1).prop('style').display).toEqual(undefined);
42+
expect(switch2.find('label.el-switch').at(0).hasClass('is-checked')).toEqual(false);
43+
expect(switch2.find('label.el-switch .el-switch__label').at(0).text()).toEqual('ON');
44+
expect(switch2.find('label.el-switch .el-switch__label').at(1).text()).toEqual('OFF');
45+
expect(switch2.find('label.el-switch .el-switch__label').at(0).prop('style').display).toEqual('none');
46+
expect(switch2.find('label.el-switch .el-switch__label').at(1).prop('style').display).toEqual(undefined);
47+
4748
});
4849

4950
test('Extended value types', () => {
@@ -73,7 +74,7 @@ test('Extended value types', () => {
7374
</Tooltip>
7475
</div>);
7576

76-
expect(component.find('label .el-switch .el-switch--wide').at(0).hasClass('is-checked')).toEqual(true);
77+
expect(component.find('label.el-switch.el-switch--wide').at(0).hasClass('is-checked')).toEqual(true);
7778
expect(component.find('div .el-switch__label').at(0).text()).toEqual('ON');
7879
expect(component.find('div .el-switch__label').at(1).text()).toEqual('OFF');
7980
expect(component.find('div .el-switch__label').at(0).prop('style').display).toEqual(undefined);
@@ -83,7 +84,7 @@ test('Extended value types', () => {
8384
// switch off
8485
component.find('input[type="checkbox"]').simulate('change', {target: {value: false}});
8586

86-
expect(component.find('label .el-switch .el-switch--wide').at(0).hasClass('is-checked')).toEqual(false);
87+
expect(component.find('label.el-switch.el-switch--wide').at(0).hasClass('is-checked')).toEqual(false);
8788
expect(component.find('div .el-switch__label').at(0).text()).toEqual('ON');
8889
expect(component.find('div .el-switch__label').at(1).text()).toEqual('OFF');
8990
expect(component.find('div .el-switch__label').at(0).prop('style').display).toEqual('none');
@@ -101,7 +102,7 @@ test('Disabled', () => {
101102
</Switch>
102103
);
103104

104-
expect(switch1.find('label .el-switch').at(0).hasClass('is-disabled')).toEqual(true);
105+
expect(switch1.find('label.el-switch').at(0).hasClass('is-disabled')).toEqual(true);
105106
expect(switch1.find('input[type="checkbox"]').at(0).prop('disabled')).toEqual(true);
106107

107108
const switch2 = mount(
@@ -110,7 +111,7 @@ test('Disabled', () => {
110111
disabled>
111112
</Switch>);
112113

113-
expect(switch2.find('label .el-switch').at(0).hasClass('is-disabled')).toEqual(true);
114+
expect(switch2.find('label.el-switch').at(0).hasClass('is-disabled')).toEqual(true);
114115
expect(switch2.find('input[type="checkbox"]').at(0).prop('disabled')).toEqual(true);
115116

116117
});

0 commit comments

Comments
 (0)