Skip to content

Commit 6863b9f

Browse files
bindoonyouluna
authored andcommitted
fix(NumberPicker): value set to min while next value < min by click +
1 parent a420197 commit 6863b9f

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/number-picker/index.jsx

+19-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Icon from '../icon';
55
import Button from '../button';
66
import Input from '../input';
77
import ConfigProvider from '../config-provider';
8-
import {func, obj} from '../util';
8+
import { func, obj } from '../util';
99

1010
/** NumberPicker */
1111
class NumberPicker extends React.Component {
@@ -259,7 +259,7 @@ class NumberPicker extends React.Component {
259259
});
260260
}
261261

262-
this.props.onChange(isNaN(v) || v === '' ? undefined : v, {...e, triggerType});
262+
this.props.onChange(isNaN(v) || v === '' ? undefined : v, { ...e, triggerType });
263263
}
264264

265265
setInputValue(v, e) {
@@ -299,7 +299,7 @@ class NumberPicker extends React.Component {
299299
}
300300

301301
upStep(val) {
302-
const {step, min} = this.props;
302+
const { step, min } = this.props;
303303
const precisionFactor = this.getPrecisionFactor();
304304
let result;
305305
if (typeof val === 'number') {
@@ -313,7 +313,7 @@ class NumberPicker extends React.Component {
313313
}
314314

315315
downStep(val) {
316-
const {step, min} = this.props;
316+
const { step, min } = this.props;
317317
const precisionFactor = this.getPrecisionFactor();
318318
let result;
319319
if (typeof val === 'number') {
@@ -344,17 +344,23 @@ class NumberPicker extends React.Component {
344344
if (e) {
345345
e.preventDefault();
346346
}
347-
const {disabled, min, max} = this.props;
347+
348+
const { disabled, min, max } = this.props;
348349
if (disabled) {
349350
return;
350351
}
352+
351353
const value = this.state.value;
352354
if (isNaN(value)) {
353355
return;
354356
}
355-
const val = this[`${type}Step`](value);
356-
if (val > max || val < min) {
357-
return;
357+
358+
let val = this[`${type}Step`](value);
359+
if (val > max) {
360+
val = max;
361+
}
362+
if (val < min) {
363+
val = min;
358364
}
359365
this.setValue(val, e, type);
360366
}
@@ -382,7 +388,7 @@ class NumberPicker extends React.Component {
382388
}
383389

384390
render() {
385-
const {type, prefix, disabled, style, className, size, max, min, autoFocus, editable, state} = this.props;
391+
const { type, prefix, disabled, style, className, size, max, min, autoFocus, editable, state } = this.props;
386392

387393
const prefixCls = `${prefix}number-picker`;
388394

@@ -410,22 +416,22 @@ class NumberPicker extends React.Component {
410416
if (type === 'normal') {
411417
innerAfter = ([
412418
<Button disabled={disabled || upDisabled} onClick={this.up.bind(this)} key="0">
413-
<Icon size="xxs" type="arrow-up"/>
419+
<Icon size="xxs" type="arrow-up" />
414420
</Button>,
415421
<Button disabled={disabled || downDisabled} onClick={this.down.bind(this)} key="1">
416-
<Icon size="xxs" type="arrow-down"/>
422+
<Icon size="xxs" type="arrow-down" />
417423
</Button>
418424
]);
419425
innerAfterClassName = `${prefixCls}-handler`;
420426
} else {
421427
addonBefore = (
422428
<Button size={size} disabled={disabled || downDisabled} onClick={this.down.bind(this)}>
423-
<Icon type="minus" size="xs"/>
429+
<Icon type="minus" size="xs" />
424430
</Button>
425431
);
426432
addonAfter = (
427433
<Button size={size} disabled={disabled || upDisabled} onClick={this.up.bind(this)}>
428-
<Icon type="add" size="xs"/>
434+
<Icon type="add" size="xs" />
429435
</Button>
430436
);
431437
}

test/number-picker/index-spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,24 @@ describe('number-picker', () => {
200200
done();
201201
});
202202

203+
it('should be equal min while next value < min by click +', (done) => {
204+
let onChange = (value) => {
205+
assert(value === 30);
206+
done();
207+
}, wrapper = mount(<NumberPicker defaultValue={5} min={30} step={3} onChange={onChange}/>);
208+
209+
wrapper.find('button').at(0).simulate('click');
210+
});
211+
212+
it('should be equal max while next value > max by click -', (done) => {
213+
let onChange = (value) => {
214+
assert(value === 30);
215+
done();
216+
}, wrapper = mount(<NumberPicker defaultValue={205} max={30} step={3} onChange={onChange}/>);
217+
218+
wrapper.find('button').at(1).simulate('click');
219+
});
220+
203221
it('should support precision', (done) => {
204222
let wrapper = mount(<NumberPicker defaultValue={0.121} step={0.01} precision={3} />);
205223

0 commit comments

Comments
 (0)