Skip to content

Commit

Permalink
fix: Decimal over six become scientific notation (#246)
Browse files Browse the repository at this point in the history
* fix: Decimal over six becomee scientific notation

* fix: CI

* fix: input-value

* fix: input value

* feat: add test case

* fix: factor

* feat: move getTypeNum in getInputDisplayValue
  • Loading branch information
fireairforce committed May 20, 2020
1 parent 59d2148 commit 30f4ec2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export default class InputNumber extends React.Component {

if (onBlur) {
const originValue = this.input.value;
const inputValue = this.getInputDisplayValue({ focus: false, value: newValue });
const inputValue = Number(this.getInputDisplayValue({ focus: false, value: newValue }));
this.input.value = inputValue;
onBlur(...args);
this.input.value = originValue;
Expand Down Expand Up @@ -324,7 +324,8 @@ export default class InputNumber extends React.Component {
setValue(v, callback) {
// trigger onChange
const { precision } = this.props;
const newValue = this.isNotCompleteNumber(parseFloat(v, 10)) ? null : parseFloat(v, 10);
const newValue = this.isNotCompleteNumber(parseFloat(v, 10)) ? null
: parseFloat(v, 10);
const { value = null, inputValue = null } = this.state;
// https://github.com/ant-design/ant-design/issues/7363
// https://github.com/ant-design/ant-design/issues/16622
Expand All @@ -349,6 +350,16 @@ export default class InputNumber extends React.Component {
return newValue;
}

getFullNum(num) {
if (isNaN(num)) {
return num;
}
if (!/e/i.test(String(num))) {
return num;
}
return (num).toFixed(18).replace(/\.?0+$/, '');
}

getPrecision(value) {
if (isValidProps(this.props.precision)) {
return this.props.precision;
Expand Down Expand Up @@ -408,7 +419,7 @@ export default class InputNumber extends React.Component {
.replace('.', this.props.decimalSeparator);
}

return inputDisplayValueFormat;
return this.getFullNum(inputDisplayValueFormat);
};

recordCursorPosition = () => {
Expand Down
26 changes: 26 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,32 @@ describe('InputNumber', () => {
expect(inputNumber.state.value).to.be(-3.637978807091713e-12);
});

it('value decimal over six decimal not be scientific notation', () => {
const Demo = createReactClass({
render() {
return <InputNumber ref="inputNum" precision={7} step={0.0000001} />;
},
});
example = ReactDOM.render(<Demo />, container);
inputNumber = example.refs.inputNum;
inputElement = ReactDOM.findDOMNode(inputNumber.input);
Simulate.mouseDown(
findRenderedDOMComponentWithClass(example, 'rc-input-number-handler-up'),
);
expect(inputElement.value).to.be('0.0000001');
expect(inputElement.value).not.to.be('1e-7');
Simulate.mouseDown(
findRenderedDOMComponentWithClass(example, 'rc-input-number-handler-up'),
);
expect(inputElement.value).to.be('0.0000002');
expect(inputElement.value).not.to.be('2e-7');
Simulate.mouseDown(
findRenderedDOMComponentWithClass(example, 'rc-input-number-handler-down'),
);
expect(inputElement.value).to.be('0.0000001');
expect(inputElement.value).not.to.be('1e-7');
});

it('value can be changed when dynamic setting max', () => {
const Demo = createReactClass({
getInitialState() {
Expand Down

0 comments on commit 30f4ec2

Please sign in to comment.