From 80678afcdde7ed2948730e72b2b2b2808e2cc2b7 Mon Sep 17 00:00:00 2001 From: Joris Date: Thu, 17 Mar 2022 18:08:13 +0100 Subject: [PATCH] fix: cannot empty the input (#10) --- src/index.test.tsx | 32 ++++++++++++++++++++++++++++++++ src/index.tsx | 3 ++- src/utils.test.ts | 6 +++--- src/utils.ts | 2 +- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/index.test.tsx b/src/index.test.tsx index d4eb0ab6..a95ab175 100644 --- a/src/index.test.tsx +++ b/src/index.test.tsx @@ -186,3 +186,35 @@ test('PhoneInput with ref', () => { ] `); }); + +test('should empty the field', async () => { + const handleChange = vi.fn<[React.ChangeEvent], void>(); + + render( + + + + + ); + + expect(handleChange).not.toHaveBeenCalled(); + + user.type(screen.getByPlaceholderText('3 33 33 33 33'), '334455667'); + user.type(screen.getByPlaceholderText('3 33 33 33 33'), '{Backspace}'); + user.type(screen.getByPlaceholderText('3 33 33 33 33'), '{Backspace}'); + user.type(screen.getByPlaceholderText('3 33 33 33 33'), '{Backspace}'); + + expect(screen.getByPlaceholderText('3 33 33 33 33')).toHaveValue('3 34 45 5'); + + user.type(screen.getByPlaceholderText('3 33 33 33 33'), '{Backspace}'); + user.type(screen.getByPlaceholderText('3 33 33 33 33'), '{Backspace}'); + + expect(screen.getByPlaceholderText('3 33 33 33 33')).toHaveValue('3 34 4'); + + user.type(screen.getByPlaceholderText('3 33 33 33 33'), '{Backspace}'); + user.type(screen.getByPlaceholderText('3 33 33 33 33'), '{Backspace}'); + user.type(screen.getByPlaceholderText('3 33 33 33 33'), '{Backspace}'); + user.type(screen.getByPlaceholderText('3 33 33 33 33'), '{Backspace}'); + + expect(screen.getByPlaceholderText('3 33 33 33 33')).toBeEmptyDOMElement(); +}); diff --git a/src/index.tsx b/src/index.tsx index 96015db6..601f9729 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -135,7 +135,8 @@ const _Number = forwardRef< value={_value.formatted} onChange={(e) => { props.onChange?.(e); - if (/\d+/.test(e.target.value)) { + + if (/\d+|^$/.test(e.target.value)) { setValue( Object.assign({}, _value, { raw: '+' + _value.country[3] + removeMask(e.target.value), diff --git a/src/utils.test.ts b/src/utils.test.ts index b2d42cd3..8b37d6cc 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -103,7 +103,7 @@ describe('splitPhoneNumber', () => { }); test.each([ - ['+33624157863', '33624157863'], + ['+33234567890', '33234567890'], ['(33) 33-33-33', '33333333'], ])('removeMask(%s) -> %s', (p, e) => expect(removeMask(p)).toBe(e)); @@ -125,8 +125,8 @@ test.each([ ['123456', '.... ....', '1234 56'], // Fill as much as possible ['123456', '.... ..-..', '1234 56-'], // Stop at first missing digit ['123456', '.... ....-..', '1234 56'], // Stop at first missing digit - ['6 24 15 78 63', '. .. .. .. ..', '6 24 15 78 63'], - ['+33624157863', undefined, '+33624157863'], + ['6 12 34 56 78', '. .. .. .. ..', '6 12 34 56 78'], + ['+33234567890', undefined, '+33234567890'], ])('applyMask(%s, %s) -> %s', (p, m, e) => { expect(applyMask(p, m)).toBe(e); }); diff --git a/src/utils.ts b/src/utils.ts index 20ab967b..40b24537 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -10,7 +10,7 @@ export const removeMask = (value: string) => value.replace(/\D/g, ''); export const applyMask = (value = '', mask?: string) => { if (!mask || !value) return value; const flatValue = removeMask(value).split(''); - return mask.replace(/\./g, () => flatValue.shift() || '.').split('.')[0]; + return mask.replace(/\./g, () => flatValue.shift() || '.').split('.')[0].trim(); }; export const isE164Compliant = (value: string) =>