Skip to content

Commit

Permalink
Fix issue #533 (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhil-varma authored Jun 9, 2021
1 parent 1d3daf4 commit 57732f6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-number-format",
"description": "React component to format number in an input or as a text.",
"version": "4.6.1",
"version": "4.6.2",
"main": "dist/react-number-format.cjs.js",
"module": "dist/react-number-format.es.js",
"author": "Sudhanshu Yadav",
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export function roundToPrecision(numStr: string, scale: number, fixedDecimalScal
const { beforeDecimal, afterDecimal, hasNagation } = splitDecimal(numStr);
const floatValue = parseFloat(`0.${afterDecimal || '0'}`);
const floatValueStr =
afterDecimal.length <= scale ? toNumericString(floatValue) : floatValue.toFixed(scale);
afterDecimal.length <= scale ? `0.${afterDecimal}` : floatValue.toFixed(scale);
const roundedDecimalParts = floatValueStr.split('.');
const intPart = beforeDecimal
.split('')
Expand Down
44 changes: 40 additions & 4 deletions test/library/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,21 +219,23 @@ describe('NumberFormat as input', () => {
});

it('should update value if group of characters got deleted with format', () => {
const wrapper = shallow(<NumberFormat format="+1 (###) ### # ## US" value="+1 (999) 999 9 99 US"/>);
const wrapper = shallow(
<NumberFormat format="+1 (###) ### # ## US" value="+1 (999) 999 9 99 US" />,
);
simulateKeyInput(wrapper.find('input'), 'Backspace', 6, 10);
expect(wrapper.state().value).toEqual('+1 (999) 999 9 US');

//when group of characters (including format character) is replaced with number
wrapper.setProps({ value: '+1 (888) 888 8 88 US' });
simulateKeyInput(wrapper.find('input'), '8', 6, 10);
expect(wrapper.state().value).toEqual('+1 (888) 888 8 8 US');
})
});

it('should maintain the format even when the format is numeric and characters are deleted', () => {
const wrapper = shallow(<NumberFormat format="0###0 ###0####" value="01230 45607899"/>);
const wrapper = shallow(<NumberFormat format="0###0 ###0####" value="01230 45607899" />);
simulateKeyInput(wrapper.find('input'), 'Backspace', 6, 10);
expect(wrapper.state().value).toEqual('01230 78909 ');
})
});

it('should update value if whole content is replaced by new number', () => {
const wrapper = shallow(<NumberFormat format="+1 (###) ### # ## US" allowEmptyFormatting />);
Expand Down Expand Up @@ -490,6 +492,7 @@ describe('NumberFormat as input', () => {
}).toThrow();
});

// Test case for Issue #533
it('should show the right decimal values based on the decimal scale provided', () => {
class WrapperComponent extends React.Component {
constructor() {
Expand Down Expand Up @@ -524,5 +527,38 @@ describe('NumberFormat as input', () => {
wrapper.setState({ value: '123.1234' });
expect(wrapper.find('input').instance().value).toEqual('$123.1234');
});

it('should show the right number of zeros in all cases', () => {
class WrapperComponent extends React.Component {
constructor() {
super();
this.state = {
value: '100.0',
};
}

render() {
return (
<NumberFormat
name="numberformat"
value={this.state.value}
thousandSeparator
prefix="$"
decimalScale={2}
isNumericString
/>
);
}
}

const wrapper = mount(<WrapperComponent />);
expect(wrapper.find('input').instance().value).toEqual('$100.0');
wrapper.setState({ value: '123.00' });
expect(wrapper.find('input').instance().value).toEqual('$123.00');
wrapper.setState({ value: '132.000' });
expect(wrapper.find('input').instance().value).toEqual('$132.00');
wrapper.setState({ value: '100.10' });
expect(wrapper.find('input').instance().value).toEqual('$100.10');
});
});
});

0 comments on commit 57732f6

Please sign in to comment.