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
c5e7d3c
commit 145099f
Showing
5 changed files
with
83 additions
and
5 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
12 changes: 12 additions & 0 deletions
12
packages/lib/examples/implementations/ValidateOnZodImpl/index.stories.tsx
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,12 @@ | ||
import type { Meta } from 'storybook-solidjs'; | ||
|
||
import { ValidateOnZodImpl } from '.'; | ||
|
||
const meta = { | ||
title: 'BS5 Implementations', | ||
component: ValidateOnZodImpl, | ||
} satisfies Meta<typeof ValidateOnZodImpl>; | ||
|
||
export default meta; | ||
|
||
export const ValidateOnZod = {}; |
53 changes: 53 additions & 0 deletions
53
packages/lib/examples/implementations/ValidateOnZodImpl/index.tsx
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,53 @@ | ||
import { Component } from 'solid-js'; | ||
import { useFormHandler } from '@hooks'; | ||
import { zodSchema } from '@adapters'; | ||
import { TextInput } from '@example-components'; | ||
import { z } from 'zod'; | ||
|
||
export type Schema = { name: string; email: string }; | ||
|
||
export const schema = z.object({ | ||
name: z.string().min(1, 'Required field'), | ||
email: z.string().email().min(1, 'Required field'), | ||
}); | ||
|
||
export const ValidateOnZodImpl: Component = () => { | ||
const formHandler = useFormHandler(zodSchema(schema), { | ||
validateOn: ['input'], | ||
}); | ||
const { formData } = formHandler; | ||
|
||
const submit = async (event: Event) => { | ||
event.preventDefault(); | ||
try { | ||
await formHandler.validateForm(); | ||
alert(JSON.stringify(formData())); | ||
formHandler.resetForm(); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<form onSubmit={submit}> | ||
<div class="row gy-3"> | ||
<div class="col-sm-12 col-md-6"> | ||
<TextInput label="Name" name="name" formHandler={formHandler} /> | ||
</div> | ||
<div class="col-sm-12 col-md-6"> | ||
<TextInput label="Email" name="email" formHandler={formHandler} /> | ||
</div> | ||
</div> | ||
<div class="mb-3 w-100"> | ||
<button disabled={formHandler.isFormInvalid()} class="btn btn-primary me-2 mt-2"> | ||
Submit | ||
</button> | ||
</div> | ||
<pre> | ||
<code>{JSON.stringify(formHandler._.getFormState(), null, 2)}</code> | ||
</pre> | ||
</form> | ||
</> | ||
); | ||
}; |
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