Ref EditableCell signature

Signed-off-by: Jonathan <contact@grafikart.fr>
This commit is contained in:
Jonathan
2025-11-13 19:48:36 +01:00
committed by Émile Ré
parent 53a1e0f383
commit 94684e56ff
13 changed files with 705 additions and 314 deletions

View File

@@ -0,0 +1,31 @@
import { z, ZodError, type ZodTypeAny } from "zod";
import { useMemo, useState } from "react";
export function useStateWithSchema<T extends ZodTypeAny>(
schema: T,
initialValue: z.infer<T>,
) {
const [state, setState] = useState(initialValue);
const errors = useMemo(() => {
try {
schema.parse(state);
return {};
} catch (error) {
if (error instanceof ZodError) {
return Object.fromEntries(
error.issues.map((issue) => [issue.path.join("."), issue.message]) ??
[],
);
}
return {};
}
}, [state, schema]);
return [
state,
(key: keyof z.infer<T>, value: z.infer<T>[typeof key]) => {
setState((prevState) => ({ ...prevState, [key]: value }));
},
errors,
] as const;
}