Skip to content

Commit e04fcb2

Browse files
committed
Fix #879, Treat empty mask as space.
1 parent 9b7ca03 commit e04fcb2

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

example/src/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class App extends React.Component {
161161
return (
162162
<div>
163163
<div className="example">
164+
164165
<h3>Prefix and thousand separator : Format currency as text</h3>
165166
<NumericFormat value={2456981} displayType="text" thousandSeparator={true} prefix="$" />
166167
</div>
@@ -172,6 +173,12 @@ class App extends React.Component {
172173

173174
<div className="example">
174175
<h3>Custom renderText method</h3>
176+
<PatternFormat
177+
format="+359 ### ### ###"
178+
onValueChange={console.log}
179+
mask=""
180+
/>
181+
175182
<PatternFormat
176183
value={4111111111111111}
177184
displayType="text"

src/pattern_format.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ export function format<BaseType = InputAttributes>(
1616
props: PatternFormatProps<BaseType>,
1717
) {
1818
const format = props.format as string;
19-
const { allowEmptyFormatting, mask, patternChar = '#' } = props;
19+
const { allowEmptyFormatting, patternChar = '#' } = props;
20+
// treat empty string mask same as space, as empty string mask will break the format
21+
const mask = !props.mask ? ' ' : props.mask;
2022

2123
if (numStr === '' && !allowEmptyFormatting) return '';
2224

@@ -174,7 +176,7 @@ export function usePatternFormat<BaseType = InputAttributes>(
174176
): NumberFormatBaseProps<BaseType> {
175177
const {
176178
/* eslint-disable no-unused-vars */
177-
mask,
179+
mask: maskProp,
178180
allowEmptyFormatting,
179181
/* eslint-enable no-unused-vars */
180182
format: formatProp,
@@ -247,7 +249,9 @@ export function usePatternFormat<BaseType = InputAttributes>(
247249
const _value = isNil(value) ? defaultValue : value;
248250
const isValueNumericString = valueIsNumericString ?? isNumericString(_value, formatProp);
249251

250-
const _props = { ...props, valueIsNumericString: isValueNumericString };
252+
// treat empty string mask same as space, as empty string mask will break the format
253+
const mask = !maskProp ? ' ' : maskProp;
254+
const _props = { ...props, mask, valueIsNumericString: isValueNumericString };
251255

252256
return {
253257
...(restProps as NumberFormatBaseProps<BaseType>),

test/library/input.spec.jsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,29 @@ describe('NumberFormat as input', () => {
311311
expect(input).toHaveValue('1334_');
312312
});
313313

314+
it('should treat mask="" as mask=" "', async () => {
315+
const { input } = await render(<PatternFormat format="+359 ### ### ###" mask="" allowEmptyFormatting />);
316+
expect(input).toHaveValue('+359 ');
317+
});
318+
319+
320+
it('should give proper value with mask="" and numeric prefix in format #879', async () => {
321+
const spy = vi.fn();
322+
const { input, user } = await render(
323+
<PatternFormat format="+359 ### ### ###" onValueChange={spy} mask="" />,
324+
);
325+
326+
await simulateKeyInput(user, input, '2', 5);
327+
328+
expect(spy).toHaveBeenCalled();
329+
expect(spy.mock.lastCall[0]).toEqual({
330+
formattedValue: '+359 2 ',
331+
value: '2',
332+
floatValue: 2,
333+
});
334+
});
335+
336+
314337
it('should allow replacing all characters with number when formatting is present for NumericFormats', async () => {
315338
//check for numeric input
316339
const value = '12.000';

0 commit comments

Comments
 (0)