Skip to content

Commit cf0b3ba

Browse files
committed
Fix #712
1 parent 5f27bbe commit cf0b3ba

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

src/number_format_base.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,13 @@ export default function NumberFormatBase<BaseType = InputAttributes>(
7676
// if the formatted value is not synced to parent, or if the formatted value is different
7777
if (lastUpdatedValue.current === undefined || newFormattedValue !== lastUpdatedValue.current) {
7878
const input = focusedElm.current;
79+
80+
// formatting can remove some of the number chars, so we need to fine number string again
81+
const _numAsString = removeFormatting(newFormattedValue, undefined);
82+
7983
updateValue({
8084
formattedValue: newFormattedValue,
81-
numAsString: numAsString,
85+
numAsString: _numAsString,
8286
input,
8387
setCaretPosition: true,
8488
source: SourceType.props,
@@ -228,9 +232,12 @@ export default function NumberFormatBase<BaseType = InputAttributes>(
228232
...changeRange,
229233
lastValue: formattedValue,
230234
};
231-
const _numAsString = removeFormatting(inputValue, changeMeta);
235+
let _numAsString = removeFormatting(inputValue, changeMeta);
232236
const _formattedValue = _format(_numAsString);
233237

238+
// formatting can remove some of the number chars, so we need to fine number string again
239+
_numAsString = removeFormatting(_formattedValue, undefined);
240+
234241
if (isAllowed && !isAllowed(getValueObject(_formattedValue, _numAsString))) {
235242
//reset the caret position
236243
const input = event.target as HTMLInputElement;

src/pattern_format.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import React from 'react';
2-
import {
3-
PatternFormatProps,
4-
InputAttributes,
5-
ChangeMeta,
6-
InternalNumberFormatBase,
7-
NumberFormatBaseProps,
8-
} from './types';
2+
import { PatternFormatProps, InputAttributes, ChangeMeta, NumberFormatBaseProps } from './types';
93
import {
104
charIsNumber,
115
getCaretPosInBoundary,
@@ -73,7 +67,9 @@ export function removeFormatting<BaseType = InputAttributes>(
7367
let str = '';
7468
for (let i = 0; i < value.length; i++) {
7569
if (isNumericSlot(i)) {
76-
str += value[i];
70+
if (charIsNumber(value[i])) {
71+
str += value[i];
72+
}
7773
} else if (value[i] !== format[i]) {
7874
// if there is a mismatch on the pattern, do plane number extract
7975
return extractNumbers(value);

test/library/input.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import {
1414
mount,
1515
shallow,
1616
getInputValue,
17+
render,
18+
wait,
19+
simulateNativeKeyInput
1720
} from '../test_util';
1821
import PatternFormat, { usePatternFormat } from '../../src/pattern_format';
1922
import NumberFormatBase from '../../src/number_format_base';
@@ -553,6 +556,34 @@ describe('NumberFormat as input', () => {
553556
expect(spy).toHaveBeenCalled();
554557
});
555558

559+
it('should not give wrong value, when user enter more number than the given hash in PatternFormat #712', async () => {
560+
const Component = () => {
561+
const [value, setValue] = useState("1232345124");
562+
return (
563+
<div>
564+
<PatternFormat
565+
value={value}
566+
format="(###) #### ###"
567+
valueIsNumericString
568+
mask="_"
569+
onValueChange={(values) => {
570+
setValue(values.value);
571+
}}
572+
/>
573+
<span data-testid="value">{value}</span>
574+
</div>
575+
)
576+
}
577+
578+
const {input, view} = await render(<Component />);
579+
simulateNativeKeyInput(input, '1', 1, 1);
580+
await wait(100);
581+
582+
expect(input.value).toEqual('(112) 3234 512');
583+
const value = await view.getByTestId('value');
584+
expect(value.innerText).toEqual('1123234512');
585+
})
586+
556587
describe('Test masking', () => {
557588
it('should allow mask as string', () => {
558589
const wrapper = mount(<PatternFormat format="#### #### ####" mask="_" />);

0 commit comments

Comments
 (0)