Skip to content

Commit f1d6696

Browse files
committed
Fixed bugs #669, #670
1 parent d21c1c2 commit f1d6696

File tree

5 files changed

+19
-24
lines changed

5 files changed

+19
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-number-format",
33
"description": "React component to format number in an input or as a text.",
4-
"version": "5.0.0-beta.4",
4+
"version": "5.0.0-beta.5",
55
"main": "dist/react-number-format.cjs.js",
66
"module": "dist/react-number-format.es.js",
77
"types": "types/index.d.ts",

src/number_format_base.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ export default function NumberFormatBase<BaseType = InputAttributes>(
259259
setCaretPosition: true,
260260
input: event.target as HTMLInputElement,
261261
});
262+
263+
return true;
262264
};
263265

264266
const _onChange = (e: React.ChangeEvent<HTMLInputElement>) => {

src/utils.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export function roundToPrecision(numStr: string, scale: number, fixedDecimalScal
161161
//if number is empty don't do anything return empty string
162162
if (['', '-'].indexOf(numStr) !== -1) return numStr;
163163

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

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

test/library/input.spec.js

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,6 @@ describe('NumberFormat as input', () => {
2525
});
2626
});
2727

28-
// it('should accept and format custom numerals', () => {
29-
// const wrapper = mount(
30-
// <NumberFormat customNumerals={['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']} />,
31-
// );
32-
// simulateKeyInput(wrapper.find('input'), '۱۲۳۴۵۶۷۸۹۰', 0);
33-
34-
// expect(getInputValue(wrapper)).toEqual('1234567890');
35-
// });
36-
37-
// it('should accept and format custom numerals together with format input field', () => {
38-
// const wrapper = mount(
39-
// <NumberFormat
40-
// customNumerals={['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']}
41-
// format="##########"
42-
// />,
43-
// );
44-
// simulateKeyInput(wrapper.find('input'), '۱۲۳۴۵۶۷۸۹۰', 0);
45-
46-
// expect(getInputValue(wrapper)).toEqual('1234567890');
47-
// });
48-
4928
it('should render input as type text by default', () => {
5029
const wrapper = mount(<NumericFormat />);
5130
expect(wrapper.find('input').instance().getAttribute('type')).toEqual('text');
@@ -553,6 +532,13 @@ describe('NumberFormat as input', () => {
553532
expect(spy).not.toHaveBeenCalled();
554533
});
555534

535+
it('should call onChange if value is changed or reset #669 ', () => {
536+
const spy = jasmine.createSpy('onChange');
537+
const wrapper = mount(<NumericFormat value={1} onChange={spy} />);
538+
simulateKeyInput(wrapper.find('input'), 'Backspace', 1);
539+
expect(spy).toHaveBeenCalled();
540+
});
541+
556542
describe('Test masking', () => {
557543
it('should allow mask as string', () => {
558544
const wrapper = mount(<PatternFormat format="#### #### ####" mask="_" />);

test/library/input_numeric_format.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ describe('Test NumberFormat as input with numeric format options', () => {
224224
expect(getInputValue(wrapper)).toEqual('24.4568');
225225
});
226226

227+
it('should handle fixedDecimalScale correctly #670', () => {
228+
const wrapper = mount(
229+
<NumericFormat thousandSeparator value={12} decimalScale={2} fixedDecimalScale />,
230+
);
231+
expect(getInputValue(wrapper)).toEqual('12.00');
232+
});
233+
227234
it('should not round the initial if decimalScale is not provided', () => {
228235
const wrapper = mount(<NumericFormat value={123213.7535} />);
229236
expect(getInputValue(wrapper)).toEqual('123213.7535');

0 commit comments

Comments
 (0)