-
Notifications
You must be signed in to change notification settings - Fork 2
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
30507e6
commit 39e3d58
Showing
2 changed files
with
82 additions
and
0 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,76 @@ | ||
'use client'; | ||
|
||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { useForm } from 'react-hook-form'; | ||
import { z } from 'zod'; | ||
|
||
import { Button } from '../button'; | ||
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from '../form'; | ||
import { Input } from '../input'; | ||
|
||
import { Meta, StoryObj } from '@storybook/react'; | ||
|
||
const formSchema = z.object({ | ||
username: z.string().min(2, { | ||
message: 'Username must be at least 2 characters.', | ||
}), | ||
}); | ||
|
||
function ProfileForm() { | ||
// 1. Define your form. | ||
const form = useForm<z.infer<typeof formSchema>>({ | ||
resolver: zodResolver(formSchema), | ||
defaultValues: { | ||
username: '', | ||
}, | ||
}); | ||
|
||
// 2. Define a submit handler. | ||
function onSubmit(values: z.infer<typeof formSchema>) { | ||
// Do something with the form values. | ||
// ✅ This will be type-safe and validated. | ||
console.log(values); | ||
} | ||
|
||
return ( | ||
<Form {...form}> | ||
<form | ||
onSubmit={form.handleSubmit(onSubmit)} | ||
className='space-y-8' | ||
> | ||
<FormField | ||
control={form.control} | ||
name='username' | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Username</FormLabel> | ||
<FormControl> | ||
<Input | ||
placeholder='shadcn' | ||
{...field} | ||
/> | ||
</FormControl> | ||
<FormDescription>This is your public display name.</FormDescription> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<Button type='submit'>Submit</Button> | ||
</form> | ||
</Form> | ||
); | ||
} | ||
|
||
const meta = { | ||
title: 'CODERUM/Form', | ||
component: ProfileForm, | ||
parameters: { | ||
// Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/configure/story-layout | ||
layout: 'centered', | ||
}, | ||
} satisfies Meta<typeof ProfileForm>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const FormExample: Story = {}; |
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