Skip to content

Commit

Permalink
fix: reuse formAtom._validateFields (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
MiroslavPetrik authored Jan 15, 2024
1 parent b1b732a commit 05fd837
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 82 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
75 changes: 5 additions & 70 deletions src/atoms/list-atom/listAtom.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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),
),
);

Expand Down Expand Up @@ -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),
),
);

Expand Down Expand Up @@ -340,64 +336,3 @@ export function listAtom<
function isPromise(value: any): value is Promise<any> {
return typeof value === "object" && typeof value.then === "function";
}

// TODO: reuse from formAtoms._validateFields
async function validateFormFields(
formAtom: FormAtom<FormFields>,
get: Getter,
set: Setter,
event: ValidateOn,
) {
const form = get(formAtom);
const fields = get(form.fields);
const promises: Promise<boolean>[] = [];

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);
}
20 changes: 10 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand All @@ -10962,7 +10962,7 @@ __metadata:
optional: true
react:
optional: true
checksum: 7e635f4f5052582095ed3949aa46f26213b10237bc4876db242c936f0f4d4272129727d7419e4fd5b1b83fa55b7ad737575453116a955dcc6b3a26cba14591f2
checksum: 0e15fb1ed315cad3c024af443023cdb8a968ade9c975c867c7c2f45c582aafbeef8c812e70112a1783b3b1d762b3bc8aa3bae72073b37eb7027e8ebd416c8801
languageName: node
linkType: hard

Expand Down

0 comments on commit 05fd837

Please sign in to comment.