From 927f7a0b8d72641c1f6fc6a8295ba284e917bd58 Mon Sep 17 00:00:00 2001 From: klm <58208740+klm-lab@users.noreply.github.com> Date: Sun, 19 May 2024 21:28:24 +0000 Subject: [PATCH] feat: Add getErroneousInput ShowError method and onChange now set erroneous input if found. New method getErroneous Input is added to the form Object --- src/inputs/form.ts | 1 + src/inputs/handlers/changes.ts | 4 +++- src/inputs/handlers/files.ts | 4 +++- src/inputs/validations/index.ts | 12 ++++++++---- src/types/index.ts | 4 ++++ src/util/index.ts | 7 ++++--- 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/inputs/form.ts b/src/inputs/form.ts index 24b8a20..4739759 100644 --- a/src/inputs/form.ts +++ b/src/inputs/form.ts @@ -66,6 +66,7 @@ export const createForm = (initialState: Unknown, config: InputConfig) => { each, onSubmit, showError: () => touchInput(st), + getErroneousInput: () => st.get(`i.${st.get("e")}`), get: (name: string) => { const r: Input[] = []; st.ev[name].o.forEach((k) => { diff --git a/src/inputs/handlers/changes.ts b/src/inputs/handlers/changes.ts index ce6d434..2d21bad 100644 --- a/src/inputs/handlers/changes.ts +++ b/src/inputs/handlers/changes.ts @@ -116,6 +116,8 @@ export const syncChanges = (store: InputStore, data: ObjectInputs) => { // isTouched ref.t = true; // new valid state - ref.iv = validateState(data); + const valid = validateState(data); + ref.iv = valid.v; + ref.e = valid.e; }); }; diff --git a/src/inputs/handlers/files.ts b/src/inputs/handlers/files.ts index 73d4bfe..0ab3a67 100644 --- a/src/inputs/handlers/files.ts +++ b/src/inputs/handlers/files.ts @@ -80,7 +80,9 @@ export const parseFile = ( input.valid = !em; input.errorMessage = em; // Validate form - ref.iv = validateState(ref.i); + const valid = validateState(ref.i); + ref.iv = valid.v; + ref.e = valid.e; }); } } as ParsedFile; diff --git a/src/inputs/validations/index.ts b/src/inputs/validations/index.ts index ccf961b..7097c7b 100644 --- a/src/inputs/validations/index.ts +++ b/src/inputs/validations/index.ts @@ -54,14 +54,18 @@ const validate = ( // Validate the state const validateState = (data: ObjectInputs) => { - let valid = true; + // valid status + let v = true; + // erroneous key + let e = ""; for (const formKey in data) { - if (!valid) { + v = data[formKey].valid; + if (!v) { + e = data[formKey].touched ? formKey : ""; break; } - valid = data[formKey].valid; } - return valid; + return { v, e }; }; export { validate, validateState }; diff --git a/src/types/index.ts b/src/types/index.ts index 0f47fa4..48bebaa 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -167,6 +167,8 @@ interface Form { showError(): void; + getErroneousInput(): Input | undefined; + get(name: string): Input[]; getValues(): Unknown; @@ -187,6 +189,8 @@ type IPS = { inv: boolean; // asyncDelay: number; c: InputConfig; + // Erroneous key + e: string; }; type InputStore = StoreType & { fc: GetFile; diff --git a/src/util/index.ts b/src/util/index.ts index f1878a8..9631083 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -210,7 +210,7 @@ const touchInput = (store: InputStore) => { const data = store.get("i"); for (const key in data) { if (!data[key].valid) { - store.set((ref) => + store.set((ref) => { setTouchedEm( ref.i, key, @@ -221,8 +221,9 @@ const touchInput = (store: InputStore) => { // (data[key] as Input & GetValue).g(data[key].value, true) (data[key] as Input & GetValue).g("", true) ) - ) - ); + ); + ref.e = key; + }); break; } }