-
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.
Merge pull request #44 from igor-tech/QUIZ-29
Add Controlled Components
- Loading branch information
Showing
18 changed files
with
318 additions
and
39 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
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,2 @@ | ||
export * from './path' | ||
export * from './zod-schemes' |
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,11 @@ | ||
import { z } from 'zod' | ||
|
||
const email = z.string({ required_error: 'Email is required' }).email({ message: 'Invalid email' }) | ||
const password = z | ||
.string({ required_error: 'Password is required' }) | ||
.min(3, { message: 'Password must be at least 3 characters' }) | ||
const rememberMe = z.boolean().default(false).optional() | ||
|
||
const loginSchema = z.object({ email, password, rememberMe }) | ||
|
||
export type LoginFormValues = z.infer<typeof loginSchema> |
50 changes: 50 additions & 0 deletions
50
src/components/controlled/controlled-checkbox/controlled-checkbox.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,50 @@ | ||
import type { Meta, StoryObj } from '@storybook/react' | ||
|
||
import { CSSProperties } from 'react' | ||
import { useForm } from 'react-hook-form' | ||
|
||
import { ControlledCheckbox } from '@/components/controlled/controlled-checkbox/controlled-checkbox' | ||
import { Button } from '@/components/ui/button' | ||
import { Typography } from '@/components/ui/typography' | ||
|
||
type FormValues = Partial<Record<'check1' | 'check2' | 'check3', boolean>> | ||
|
||
const meta = { | ||
component: ControlledCheckbox, | ||
tags: ['autodocs'], | ||
title: 'Components/Controlled/Checkbox', | ||
} satisfies Meta<typeof ControlledCheckbox> | ||
|
||
export default meta | ||
type Story = StoryObj<typeof meta> | ||
|
||
export const Default: Story = { | ||
args: { | ||
name: '', | ||
}, | ||
render: () => { | ||
const { control, handleSubmit } = useForm<FormValues>() | ||
|
||
const onSubmit = (data: FormValues) => { | ||
alert(JSON.stringify(data)) | ||
} | ||
|
||
const style: CSSProperties = { | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: '20px', | ||
} | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)} style={style}> | ||
<Typography variant={'h1'}>Form with controlled checkbox</Typography> | ||
<div style={style}> | ||
<ControlledCheckbox control={control} label={'Checkbox 1'} name={'check1'} /> | ||
<ControlledCheckbox control={control} label={'Checkbox 2'} name={'check2'} /> | ||
<ControlledCheckbox control={control} label={'Checkbox 3'} name={'check3'} /> | ||
</div> | ||
<Button type={'submit'}>Submit</Button> | ||
</form> | ||
) | ||
}, | ||
} |
30 changes: 30 additions & 0 deletions
30
src/components/controlled/controlled-checkbox/controlled-checkbox.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,30 @@ | ||
import { FieldValues, UseControllerProps, useController } from 'react-hook-form' | ||
|
||
import { Checkbox, CheckboxProps } from '@/components/ui/checkbox' | ||
|
||
export type ControlledCheckboxProps<TFieldValues extends FieldValues> = Omit< | ||
CheckboxProps, | ||
'id' | 'value' | ||
> & | ||
UseControllerProps<TFieldValues> | ||
|
||
export const ControlledCheckbox = <TFieldValues extends FieldValues>({ | ||
control, | ||
defaultValue, | ||
name, | ||
rules, | ||
shouldUnregister, | ||
...checkboxProps | ||
}: ControlledCheckboxProps<TFieldValues>) => { | ||
const { | ||
field: { onChange, value }, | ||
} = useController({ | ||
control, | ||
defaultValue, | ||
name, | ||
rules, | ||
shouldUnregister, | ||
}) | ||
|
||
return <Checkbox checked={value} id={name} onCheckedChange={onChange} {...checkboxProps} /> | ||
} |
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 @@ | ||
export * from './controlled-checkbox' |
59 changes: 59 additions & 0 deletions
59
src/components/controlled/controlled-radio-group/controlled-radio-group.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,59 @@ | ||
import type { Meta, StoryObj } from '@storybook/react' | ||
|
||
import { CSSProperties } from 'react' | ||
import { useForm } from 'react-hook-form' | ||
|
||
import { ControlledRadioGroup } from '@/components/controlled/controlled-radio-group/controlled-radio-group' | ||
import { Button } from '@/components/ui/button' | ||
import { OptionType } from '@/components/ui/radio-group' | ||
import { Typography } from '@/components/ui/typography' | ||
|
||
type FormValues = Partial<Record<'value', OptionType>> | ||
|
||
const meta = { | ||
component: ControlledRadioGroup, | ||
tags: ['autodocs'], | ||
title: 'Components/Controlled/Radio Group', | ||
} satisfies Meta<typeof ControlledRadioGroup> | ||
|
||
export default meta | ||
type Story = StoryObj<typeof meta> | ||
|
||
const options: OptionType[] = [ | ||
{ label: 'Option 1', value: '1' }, | ||
{ label: 'Option 2', value: '2' }, | ||
{ label: 'Option 3', value: '3' }, | ||
] | ||
|
||
export const Default: Story = { | ||
args: { | ||
name: '', | ||
options: options, | ||
}, | ||
render: () => { | ||
const { control, handleSubmit } = useForm<FormValues>() | ||
|
||
const onSubmit = (data: FormValues) => { | ||
alert(JSON.stringify(data)) | ||
} | ||
|
||
const style: CSSProperties = { | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: '20px', | ||
} | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)} style={style}> | ||
<Typography variant={'h1'}>Form with controlled radio group</Typography> | ||
<ControlledRadioGroup | ||
control={control} | ||
defaultValue={'1'} | ||
name={'value'} | ||
options={options} | ||
/> | ||
<Button type={'submit'}>Submit</Button> | ||
</form> | ||
) | ||
}, | ||
} |
33 changes: 33 additions & 0 deletions
33
src/components/controlled/controlled-radio-group/controlled-radio-group.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,33 @@ | ||
import { FieldValues, UseControllerProps, useController } from 'react-hook-form' | ||
|
||
import { RadioGroup, RadioGroupProps } from '@/components/ui/radio-group' | ||
|
||
export type ControlledRadioGroupProps<TFieldValues extends FieldValues> = Omit< | ||
RadioGroupProps, | ||
'id' | 'onChange' | 'value' | ||
> & | ||
UseControllerProps<TFieldValues> | ||
|
||
export const ControlledRadioGroup = <TFieldValues extends FieldValues>({ | ||
control, | ||
name, | ||
...props | ||
}: ControlledRadioGroupProps<TFieldValues>) => { | ||
const { | ||
field: { onChange, ...filed }, | ||
fieldState: { error }, | ||
} = useController({ | ||
control, | ||
name, | ||
}) | ||
|
||
return ( | ||
<RadioGroup | ||
{...props} | ||
{...filed} | ||
errorMessage={error?.message} | ||
id={name} | ||
onValueChange={onChange} | ||
/> | ||
) | ||
} |
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 @@ | ||
export * from './controlled-radio-group' |
60 changes: 60 additions & 0 deletions
60
src/components/controlled/controlled-text-field/controlled-text-field.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,60 @@ | ||
import type { Meta, StoryObj } from '@storybook/react' | ||
|
||
import { CSSProperties } from 'react' | ||
import { useForm } from 'react-hook-form' | ||
|
||
import { ControlledTextField } from '@/components/controlled/controlled-text-field/controlled-text-field' | ||
import { Button } from '@/components/ui/button' | ||
import { Typography } from '@/components/ui/typography' | ||
|
||
type FormValues = Partial<Record<'email' | 'password', string>> | ||
|
||
const meta = { | ||
component: ControlledTextField, | ||
tags: ['autodocs'], | ||
title: 'Components/Controlled/Text field', | ||
} satisfies Meta<typeof ControlledTextField> | ||
|
||
export default meta | ||
type Story = StoryObj<typeof meta> | ||
|
||
export const Default: Story = { | ||
args: { | ||
name: '', | ||
}, | ||
render: () => { | ||
const { control, handleSubmit } = useForm<FormValues>() | ||
|
||
const onSubmit = (data: FormValues) => { | ||
alert(JSON.stringify(data)) | ||
} | ||
|
||
const style: CSSProperties = { | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: '20px', | ||
} | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)} style={style}> | ||
<Typography variant={'h1'}>Form with controlled Text field</Typography> | ||
<ControlledTextField | ||
control={control} | ||
label={'Email'} | ||
name={'email'} | ||
placeholder={'email@example.com'} | ||
/> | ||
|
||
<ControlledTextField | ||
control={control} | ||
label={'Password'} | ||
name={'password'} | ||
placeholder={'Password'} | ||
type={'password'} | ||
/> | ||
|
||
<Button type={'submit'}>Submit</Button> | ||
</form> | ||
) | ||
}, | ||
} |
22 changes: 22 additions & 0 deletions
22
src/components/controlled/controlled-text-field/controlled-text-field.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,22 @@ | ||
import { FieldValues, UseControllerProps, useController } from 'react-hook-form' | ||
|
||
import { TextField, TextFieldProps } from '@/components/ui/textfield/TextField' | ||
|
||
export type ControlledTextFieldProps<TFieldValues extends FieldValues> = Omit< | ||
TextFieldProps, | ||
'id' | 'onChange' | 'value' | ||
> & | ||
UseControllerProps<TFieldValues> | ||
|
||
export const ControlledTextField = <TFieldValues extends FieldValues>({ | ||
control, | ||
name, | ||
...props | ||
}: ControlledTextFieldProps<TFieldValues>) => { | ||
const { | ||
field: { ...field }, | ||
fieldState: { error }, | ||
} = useController({ control, name }) | ||
|
||
return <TextField {...props} {...field} error={error?.message} id={name} /> | ||
} |
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 @@ | ||
export * from './controlled-text-field' |
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
Oops, something went wrong.