diff --git a/package.json b/package.json index 909fec7..45101fd 100644 --- a/package.json +++ b/package.json @@ -83,9 +83,9 @@ "eslint-import-resolver-typescript": "^3.6.1", "eslint-plugin-import": "^2.29.0", "eslint-plugin-prettier": "5.0.1", - "form-atoms": "^3.2.1", + "form-atoms": "^3.2.2", "happy-dom": "^12.10.3", - "jotai": "2.6.0", + "jotai": "2.6.1", "jotai-devtools": "0.7.1", "jotai-effect": "0.2.3", "lodash.shuffle": "^4.2.0", diff --git a/src/atoms/list-atom/listAtom.ts b/src/atoms/list-atom/listAtom.ts index 2dc9b96..c66b4c4 100644 --- a/src/atoms/list-atom/listAtom.ts +++ b/src/atoms/list-atom/listAtom.ts @@ -1,14 +1,13 @@ import { FieldAtomConfig, FormAtom, - FormFields, Validate, ValidateOn, ValidateStatus, formAtom, walkFields, } from "form-atoms"; -import { Atom, Getter, PrimitiveAtom, Setter, WritableAtom, atom } from "jotai"; +import { Atom, PrimitiveAtom, WritableAtom, atom } from "jotai"; import { RESET, atomWithReset, splitAtom } from "jotai/utils"; import { @@ -251,7 +250,7 @@ export function listAtom< // run validation for nested forms await Promise.all( get(_formListAtom).map((formAtom) => - validateFormFields(formAtom as any, get, set, event), + get(formAtom)._validateFields(get, set, event), ), ); @@ -290,12 +289,9 @@ export function listAtom< state .get(_formListAtom) .map((formAtom) => - validateFormFields( - formAtom as any, - state.get, - state.set, - state.event, - ), + state + .get(formAtom) + ._validateFields(state.get, state.set, state.event), ), ); @@ -340,64 +336,3 @@ export function listAtom< function isPromise(value: any): value is Promise { return typeof value === "object" && typeof value.then === "function"; } - -// TODO: reuse from formAtoms._validateFields -async function validateFormFields( - formAtom: FormAtom, - get: Getter, - set: Setter, - event: ValidateOn, -) { - const form = get(formAtom); - const fields = get(form.fields); - const promises: Promise[] = []; - - walkFields(fields, (nextField) => { - async function validate(field: typeof nextField) { - const fieldAtom = get(field); - const value = get(fieldAtom.value); - const dirty = get(fieldAtom.dirty); - // This pointer prevents a stale validation result from being - // set after the most recent validation has been performed. - const ptr = get(fieldAtom._validateCount) + 1; - set(fieldAtom._validateCount, ptr); - - if (event === "user" || event === "submit") { - set(fieldAtom.touched, true); - } - - const maybePromise = fieldAtom._validateCallback?.({ - get, - set, - value, - dirty, - touched: get(fieldAtom.touched), - event, - }); - - let errors: string[]; - - if (isPromise(maybePromise)) { - set(fieldAtom.validateStatus, "validating"); - errors = (await maybePromise) ?? get(fieldAtom.errors); - } else { - errors = maybePromise ?? get(fieldAtom.errors); - } - - if (ptr === get(fieldAtom._validateCount)) { - set(fieldAtom.errors, errors); - set(fieldAtom.validateStatus, errors.length > 0 ? "invalid" : "valid"); - } - - if (errors && errors.length) { - return false; - } - - return true; - } - - promises.push(validate(nextField)); - }); - - await Promise.all(promises); -} diff --git a/yarn.lock b/yarn.lock index c05763e..415c4a2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2716,9 +2716,9 @@ __metadata: eslint-import-resolver-typescript: "npm:^3.6.1" eslint-plugin-import: "npm:^2.29.0" eslint-plugin-prettier: "npm:5.0.1" - form-atoms: "npm:^3.2.1" + form-atoms: "npm:^3.2.2" happy-dom: "npm:^12.10.3" - jotai: "npm:2.6.0" + jotai: "npm:2.6.1" jotai-devtools: "npm:0.7.1" jotai-effect: "npm:0.2.3" lodash.shuffle: "npm:^4.2.0" @@ -9242,13 +9242,13 @@ __metadata: languageName: node linkType: hard -"form-atoms@npm:^3.2.1": - version: 3.2.1 - resolution: "form-atoms@npm:3.2.1" +"form-atoms@npm:^3.2.2": + version: 3.2.2 + resolution: "form-atoms@npm:3.2.2" peerDependencies: jotai: ^2.0.0 react: ">=16.8" - checksum: 70d8bf68d2462e36419622038776faad85342fcda0513b4be81cc4658adee0ab6d369f90030f5bc58c2f37cf91698b04afb076fd93db3453f28178beae59a918 + checksum: 52fff4e9938cce711afa2d3af0d062c6fa1318d491b88909b2bb69f3bd17d1522736f19b003c1e1e55fb3cf5d4b61fa1cba80bcfb41f4f95e592c24f7babae0c languageName: node linkType: hard @@ -10951,9 +10951,9 @@ __metadata: languageName: node linkType: hard -"jotai@npm:2.6.0": - version: 2.6.0 - resolution: "jotai@npm:2.6.0" +"jotai@npm:2.6.1": + version: 2.6.1 + resolution: "jotai@npm:2.6.1" peerDependencies: "@types/react": ">=17.0.0" react: ">=17.0.0" @@ -10962,7 +10962,7 @@ __metadata: optional: true react: optional: true - checksum: 7e635f4f5052582095ed3949aa46f26213b10237bc4876db242c936f0f4d4272129727d7419e4fd5b1b83fa55b7ad737575453116a955dcc6b3a26cba14591f2 + checksum: 0e15fb1ed315cad3c024af443023cdb8a968ade9c975c867c7c2f45c582aafbeef8c812e70112a1783b3b1d762b3bc8aa3bae72073b37eb7027e8ebd416c8801 languageName: node linkType: hard