Skip to content

Commit

Permalink
chore: update zod example
Browse files Browse the repository at this point in the history
  • Loading branch information
barelyhuman committed May 31, 2024
1 parent 91f425f commit 44111da
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
51 changes: 22 additions & 29 deletions examples/05_zod/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
import { atomFromZodSchema } from 'jotai-form/zod';

Check failure on line 1 in examples/05_zod/src/index.tsx

View workflow job for this annotation

GitHub Actions / test

Cannot find module 'jotai-form/zod' or its corresponding type declarations.

Check failure on line 1 in examples/05_zod/src/index.tsx

View workflow job for this annotation

GitHub Actions / test

Cannot find module 'jotai-form/zod' or its corresponding type declarations.
import { useAtomValue } from 'jotai/react';
import React from 'react';
import { createRoot } from 'react-dom/client';
import { useAtom } from 'jotai/react';
import { atomWithValidate } from 'jotai-form';
import { z } from 'zod';
import { ZodIssue, z } from 'zod';

const emailSchema = z.string().email();

const emailAtom = atomWithValidate('[email protected]', {
validate: (email) => {
try {
emailSchema.parse(email);
return email;
} catch (err: any) {
// We catch the original and pull out the array of issues
// to render
// you can also just pick the first one and throw that again
throw err.issues;
}
},
});
const formAtom = atomFromZodSchema(
z.object({
email: z.string().email().default('[email protected]'),
}),
);

const App = () => {
const [state, setValue] = useAtom(emailAtom);
const formValue = useAtomValue(formAtom);
const { fieldErrors, isDirty, isValid, values, handleOnChange } = formValue;

return (
<>
<span>{state.isDirty && '*'}</span>
<span>{isDirty && '*'}</span>
<input
value={state.value}
onChange={(e) => setValue(e.target.value as any)}
value={values.email}
onChange={(e) => handleOnChange('email')(e.target.value as any)}
/>
<span>{state.isValid && 'Valid'}</span>
{!state.isValid &&
// Since there's no way for error to inherit itself, this becomes necessary
((state.error as any[]) || []).map((issue: any, errIndex: number) => (
// eslint-disable-next-line react/no-array-index-key
<span key={`error-${errIndex}`}>{`${issue.message}`}</span>
))}
<span>{isValid && 'Valid'}</span>
{!isValid && (
<span key="error">
{[]
.concat(fieldErrors.email?.issues)
.filter((x) => x)
.map((d: ZodIssue) => d.message)}
</span>
)}
</>
);
};
Expand Down
2 changes: 2 additions & 0 deletions src/atomWithValidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function atomWithValidate<Value>(
let initialPromise: Promise<Value> | undefined;
try {
const initialValidatedValue = validate(initialValue);

if (initialValidatedValue instanceof Promise) {
initialPromise = initialValidatedValue;
initialState = {
Expand All @@ -69,6 +70,7 @@ export function atomWithValidate<Value>(
error,
};
}

const baseAtom = atom(initialState);
if (process.env.NODE_ENV !== 'production') {
baseAtom.debugPrivate = true;
Expand Down
2 changes: 2 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ module.exports = {
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
alias: {
'jotai-form/zod': `${__dirname}/src/utils/zod.ts`,
'jotai-form/yup': `${__dirname}/src/utils/yup.ts`,
'jotai-form': `${__dirname}/src`,
},
},
Expand Down

0 comments on commit 44111da

Please sign in to comment.