-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91f425f
commit 44111da
Showing
3 changed files
with
26 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
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> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters