Skip to content

Commit

Permalink
fix(NumberPicker): delete 0 at beginning for stringMode
Browse files Browse the repository at this point in the history
  • Loading branch information
galo.gm committed Jun 4, 2021
1 parent cf3cf89 commit 1ed7b45
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/number-picker/demo/bignumber.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 大数 与 高精度小数
# 大数与高精度小数

- order: 8

Expand Down
2 changes: 1 addition & 1 deletion docs/number-picker/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

3. 如果输入时没触发onChange,会在onBlur检测数据正确性并触发onChange

4.`1.24` 版本加入大数支持,通过 `stringMode` 开启大数或高精度小数支持,输入输出都变为 `String` 类型,具体参考[大数 与 高精度小数](#bignumber-container)
4.`1.24` 版本加入大数支持,通过 `stringMode` 开启大数或高精度小数支持,输入输出都变为 `String` 类型,具体参考[大数与高精度小数](#bignumber-container)

## API

Expand Down
13 changes: 4 additions & 9 deletions src/number-picker/number-picker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class NumberPicker extends React.Component {
if ('value' in props) {
value = props.value;
} else {
value = stringMode ? `${defaultValue}` : defaultValue;
value = defaultValue;
}
value = value === undefined || value === null ? '' : stringMode ? `${value}` : value;
this.state = {
Expand Down Expand Up @@ -272,13 +272,8 @@ class NumberPicker extends React.Component {
}

withinMinMax(value) {
const { stringMode } = this.props;
const { max, min } = this.state;
if (stringMode) {
if (isNaN(value) || this.isGreaterThan(value, max) || this.isGreaterThan(min, value)) return false;
return true;
}
if (isNaN(value) || Number(value) > max || Number(value) < min) return false;
if (isNaN(value) || this.isGreaterThan(value, max) || this.isGreaterThan(min, value)) return false;
return true;
}

Expand Down Expand Up @@ -334,13 +329,13 @@ class NumberPicker extends React.Component {
const precisionSet = this.getPrecision();
const precisionCurrent = value.length - value.indexOf('.') - 1;
const dotIndex = value.indexOf('.');
// precision === 0 should cut '.' for bigNumber
// precision === 0 should cut '.' for stringMode
const cutPosition = precisionSet !== 0 ? dotIndex + 1 + precisionSet : dotIndex + precisionSet;
if (dotIndex > -1 && precisionCurrent > precisionSet) val = val.substr(0, cutPosition);

// 边界订正:
val = this.correctBoundary(val);
val = this.props.stringMode ? val : Number(val);
val = this.props.stringMode ? BigNumber(val).toFixed() : Number(val);
}

if (isNaN(val)) val = this.state.value;
Expand Down
12 changes: 11 additions & 1 deletion test/number-picker/index-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('number-picker', () => {
});
});

describe('bigNumber', () => {
describe('stringMode', () => {
it('should support big number not within [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER] in stringMode ', () => {
const step = '0.000000000000000000000001';
class App extends React.Component {
Expand Down Expand Up @@ -191,6 +191,16 @@ describe('number-picker', () => {
assert(wrapper.find('input').prop('value') === BigNumber(20).toFixed(precision));

});

it('should delete 0 at beginning when blur',() =>{
const wrapper = mount(<NumberPicker defaultValue={Number.MAX_SAFE_INTEGER} stringMode />);
wrapper
.find('input')
.simulate('change', { target: { value: `0${Number.MAX_SAFE_INTEGER}123` } });
assert(wrapper.find('input').prop('value') === `0${Number.MAX_SAFE_INTEGER}123`);
wrapper.find('input').simulate('blur');
assert(wrapper.find('input').prop('value') === `${Number.MAX_SAFE_INTEGER}123`);
})
})

describe('behavior', () => {
Expand Down

0 comments on commit 1ed7b45

Please sign in to comment.