Skip to content

Commit

Permalink
Fixed bugs #669, #670
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yadav committed Sep 18, 2022
1 parent d21c1c2 commit f1d6696
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 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": "5.0.0-beta.4",
"version": "5.0.0-beta.5",
"main": "dist/react-number-format.cjs.js",
"module": "dist/react-number-format.es.js",
"types": "types/index.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions src/number_format_base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ export default function NumberFormatBase<BaseType = InputAttributes>(
setCaretPosition: true,
input: event.target as HTMLInputElement,
});

return true;
};

const _onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
Expand Down
4 changes: 2 additions & 2 deletions src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export function roundToPrecision(numStr: string, scale: number, fixedDecimalScal
//if number is empty don't do anything return empty string
if (['', '-'].indexOf(numStr) !== -1) return numStr;

const shoudHaveDecimalSeparator = numStr.indexOf('.') !== -1 && scale;
const shouldHaveDecimalSeparator = (numStr.indexOf('.') !== -1 || fixedDecimalScale) && scale;
const { beforeDecimal, afterDecimal, hasNegation } = splitDecimal(numStr);
const floatValue = parseFloat(`0.${afterDecimal || '0'}`);
const floatValueStr =
Expand All @@ -182,7 +182,7 @@ export function roundToPrecision(numStr: string, scale: number, fixedDecimalScal

const decimalPart = limitToScale(roundedDecimalParts[1] || '', scale, fixedDecimalScale);
const negation = hasNegation ? '-' : '';
const decimalSeparator = shoudHaveDecimalSeparator ? '.' : '';
const decimalSeparator = shouldHaveDecimalSeparator ? '.' : '';
return `${negation}${intPart}${decimalSeparator}${decimalPart}`;
}

Expand Down
28 changes: 7 additions & 21 deletions test/library/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,6 @@ describe('NumberFormat as input', () => {
});
});

// it('should accept and format custom numerals', () => {
// const wrapper = mount(
// <NumberFormat customNumerals={['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']} />,
// );
// simulateKeyInput(wrapper.find('input'), '۱۲۳۴۵۶۷۸۹۰', 0);

// expect(getInputValue(wrapper)).toEqual('1234567890');
// });

// it('should accept and format custom numerals together with format input field', () => {
// const wrapper = mount(
// <NumberFormat
// customNumerals={['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']}
// format="##########"
// />,
// );
// simulateKeyInput(wrapper.find('input'), '۱۲۳۴۵۶۷۸۹۰', 0);

// expect(getInputValue(wrapper)).toEqual('1234567890');
// });

it('should render input as type text by default', () => {
const wrapper = mount(<NumericFormat />);
expect(wrapper.find('input').instance().getAttribute('type')).toEqual('text');
Expand Down Expand Up @@ -553,6 +532,13 @@ describe('NumberFormat as input', () => {
expect(spy).not.toHaveBeenCalled();
});

it('should call onChange if value is changed or reset #669 ', () => {
const spy = jasmine.createSpy('onChange');
const wrapper = mount(<NumericFormat value={1} onChange={spy} />);
simulateKeyInput(wrapper.find('input'), 'Backspace', 1);
expect(spy).toHaveBeenCalled();
});

describe('Test masking', () => {
it('should allow mask as string', () => {
const wrapper = mount(<PatternFormat format="#### #### ####" mask="_" />);
Expand Down
7 changes: 7 additions & 0 deletions test/library/input_numeric_format.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ describe('Test NumberFormat as input with numeric format options', () => {
expect(getInputValue(wrapper)).toEqual('24.4568');
});

it('should handle fixedDecimalScale correctly #670', () => {
const wrapper = mount(
<NumericFormat thousandSeparator value={12} decimalScale={2} fixedDecimalScale />,
);
expect(getInputValue(wrapper)).toEqual('12.00');
});

it('should not round the initial if decimalScale is not provided', () => {
const wrapper = mount(<NumericFormat value={123213.7535} />);
expect(getInputValue(wrapper)).toEqual('123213.7535');
Expand Down

0 comments on commit f1d6696

Please sign in to comment.