Skip to content

Commit

Permalink
fix(NumberPicker): ignore input not in -.\d fix alibaba-fusion#3363, a…
Browse files Browse the repository at this point in the history
  • Loading branch information
bindoon authored and lakerswgq committed Jul 28, 2022
1 parent a8e07e3 commit e9c361f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/number-picker/number-picker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ class NumberPicker extends React.Component {
onChange(value, e) {
// ignore space & Compatible Chinese Input Method
value = value.replace('。', '.').trim();
// 过滤非数字
value = value.replace(/[^-.\d]/g, '');

let onlyDisplay = false;
if (this.props.editable === true && this.shouldFireOnChange(value)) {
let valueCorrected = this.correctValue(value);
Expand Down
16 changes: 14 additions & 2 deletions test/number-picker/index-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('number-picker', () => {
wrapper
.find('input')
.simulate('change', { target: { value: `${Number.MAX_SAFE_INTEGER}a2333` } });
assert(wrapper.find('input').prop('value') === `${Number.MAX_SAFE_INTEGER}a2333`);
assert(wrapper.find('input').prop('value') === `${Number.MAX_SAFE_INTEGER}2333`);
wrapper.find('input').simulate('blur');
assert(wrapper.find('input').prop('value') === `${Number.MAX_SAFE_INTEGER}2333`);
wrapper
Expand All @@ -148,7 +148,7 @@ describe('number-picker', () => {
wrapper2
.find('input')
.simulate('change', { target: { value: `${Number.MAX_SAFE_INTEGER}a2333` } });
assert(wrapper2.find('input').prop('value') === `${Number.MAX_SAFE_INTEGER}a2333`);
assert(wrapper2.find('input').prop('value') === `${Number.MAX_SAFE_INTEGER}2333`);
wrapper2.find('input').simulate('blur');
assert(wrapper2.find('input').prop('value') === `${Number.MAX_SAFE_INTEGER}2333`);
wrapper2
Expand Down Expand Up @@ -322,6 +322,18 @@ describe('number-picker', () => {
done();
});

it('should only input -.1234567890', () => {
let wrapper = mount(
<NumberPicker />
);
wrapper.find('input').simulate('change', { target: { value: '-1.' } });
assert(wrapper.find('input').prop('value') === "-1.");
wrapper.find('input').simulate('change', { target: { value: '-1.a' } });
assert(wrapper.find('input').prop('value') === "-1.");
wrapper.find('input').simulate('change', { target: { value: '-1.13a2' } });
assert(wrapper.find('input').prop('value') === "-1.132");
})

it('onChange value 1.9 -> 1. should input displayValue === 1. onchange value === 1', done => {
const onChange = (value) => {
assert(value === 1)
Expand Down

0 comments on commit e9c361f

Please sign in to comment.