From 8a55e87d1c982c64dce32f61f3e139959120b8e7 Mon Sep 17 00:00:00 2001 From: Chance Strickland Date: Mon, 26 Sep 2022 16:53:54 -0700 Subject: [PATCH 1/2] fix: check for event.preventDefault() before setting state --- src/index.tsx | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 94a940d..5c13f7a 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -213,10 +213,6 @@ function invariant(value: any, message?: string) { const getInputId = (name: string, reactId: string) => `${name}--${reactId}`; const getErrorsId = (name: string, reactId: string) => `${name}-errors--${reactId}`; -const callAll = - (...fns: (Function | undefined)[]) => - (...args: any[]) => - fns.forEach((fn) => fn?.(...args)); const composeClassNames = (classes: Array) => classes.filter((v) => v).join(" "); const omit = ( @@ -510,10 +506,13 @@ export function useValidatedInput( id: getInputId(name, id), className: getClasses("input", attrs.className), defaultValue: serverFormInfo?.submittedFormData?.lastName, - onChange: callAll(onChange, (e: React.ChangeEvent) => { - setDirty(true); - setValue(e.target.value); - }), + onChange: (event: React.ChangeEvent) => { + onChange && onChange(event); + if (!event.defaultPrevented) { + setDirty(true); + setValue(event.target.value); + } + }, ...(showErrors ? { "aria-invalid": true, From e95268680d149def513c8c3648bac2bd673599fc Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Thu, 29 Sep 2022 08:38:27 -0400 Subject: [PATCH 2/2] Switch to optional chaining MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michaƫl De Boey --- src/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.tsx b/src/index.tsx index 5c13f7a..dfdf961 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -507,7 +507,7 @@ export function useValidatedInput( className: getClasses("input", attrs.className), defaultValue: serverFormInfo?.submittedFormData?.lastName, onChange: (event: React.ChangeEvent) => { - onChange && onChange(event); + onChange?.(event); if (!event.defaultPrevented) { setDirty(true); setValue(event.target.value);