-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from stevent-team/feat/support-intersections-a…
…nd-unions Support for intersections and unions in the schema
- Loading branch information
Showing
4 changed files
with
94 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@stevent-team/react-zoom-form": minor | ||
--- | ||
|
||
Support for intersections and unions in the Zod schema |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Errors, SubmitHandler, getValue, useForm } from '@stevent-team/react-zoom-form' | ||
import { z } from 'zod' | ||
import Output from '../Output' | ||
|
||
export const schema = z.intersection( | ||
z.object({ | ||
common: z.string(), | ||
}), | ||
z.discriminatedUnion('size', [ | ||
z.object({ | ||
size: z.literal('small'), | ||
smallProperty: z.string(), | ||
}), | ||
z.object({ | ||
size: z.literal('large'), | ||
largeProperty: z.string(), | ||
}), | ||
]), | ||
) | ||
|
||
const Intersection = ({ onSubmit }: { onSubmit: SubmitHandler<typeof schema> }) => { | ||
const { fields, handleSubmit, isDirty } = useForm({ schema, initialValues: { size: 'small' } }) | ||
|
||
return <> | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<label htmlFor={fields.common.name()}>Common field</label> | ||
<input {...fields.common.register()} id={fields.common.name()} type="text" /> | ||
<Errors field={fields.common} className="error" /> | ||
|
||
<label htmlFor={fields.size.name()}>Size</label> | ||
<select {...fields.size.register()} id={fields.size.name()}> | ||
<option value="small">Small</option> | ||
<option value="large">Large</option> | ||
</select> | ||
<Errors field={fields.size} className="error" /> | ||
|
||
{getValue(fields.size) === 'small' ? <> | ||
<label htmlFor={fields.smallProperty.name()}>Small property</label> | ||
<input {...fields.smallProperty.register()} id={fields.smallProperty.name()} type="text" /> | ||
<Errors field={fields.smallProperty} className="error" /> | ||
</> : <> | ||
<label htmlFor={fields.largeProperty.name()}>Large property</label> | ||
<input {...fields.largeProperty.register()} id={fields.largeProperty.name()} type="text" /> | ||
<Errors field={fields.largeProperty} className="error" /> | ||
</>} | ||
|
||
<button>Save changes</button> | ||
</form> | ||
|
||
<Output isDirty={isDirty} fields={fields} /> | ||
</> | ||
} | ||
|
||
export default Intersection |
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