Skip to content

Commit 60eb73f

Browse files
committed
[#98] ✨ Update options type and add TagInput component to Form
1 parent ecb4144 commit 60eb73f

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/components/shared/form/Form.tsx

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import {
66
useFormContext,
77
} from 'react-hook-form'
88

9-
import { CheckboxInput, RadioInput } from '@/components/common/input'
9+
import { CheckboxInput, RadioInput, TagInput } from '@/components/common/input'
1010
import { CheckboxInputProps } from '@/components/common/input/CheckboxInput'
1111
import { RadioInputProps } from '@/components/common/input/RadioInput'
12+
import { TagInputProps } from '@/components/common/input/TagInput'
1213

1314
interface FormProps<TFieldValues extends FieldValues>
1415
extends React.FormHTMLAttributes<HTMLFormElement> {
@@ -36,7 +37,7 @@ const Checkbox = ({
3637
}: {
3738
name: string
3839
rules?: Record<string, unknown>
39-
options: string[]
40+
options: { label: string; value: string }[]
4041
} & CheckboxInputProps): JSX.Element => {
4142
const { control } = useFormContext()
4243
return (
@@ -46,16 +47,17 @@ const Checkbox = ({
4647
rules={rules}
4748
render={({ field }) => (
4849
<>
49-
{options.map(value => (
50+
{options.map(option => (
5051
<CheckboxInput
51-
key={value}
52+
key={option.value}
5253
{...props}
53-
value={value}
54-
checked={field.value?.includes(value)}
54+
value={option.value}
55+
label={option.label}
56+
checked={field.value?.includes(option.value)}
5557
onChange={e => {
5658
const newValue = e.target.checked
57-
? [...field.value, value]
58-
: field.value.filter((v: string) => v !== value)
59+
? [...field.value, option.value]
60+
: field.value.filter((v: string) => v !== option.value)
5961
field.onChange(newValue)
6062
}}
6163
/>
@@ -74,7 +76,7 @@ const Radio = ({
7476
}: {
7577
name: string
7678
rules?: Record<string, unknown>
77-
options: string[]
79+
options: { label: string; value: string }[]
7880
} & RadioInputProps): JSX.Element => {
7981
const { control } = useFormContext()
8082
return (
@@ -84,13 +86,14 @@ const Radio = ({
8486
rules={rules}
8587
render={({ field }) => (
8688
<>
87-
{options.map(value => (
89+
{options.map(option => (
8890
<RadioInput
89-
key={value}
91+
key={option.value}
9092
{...props}
91-
value={value}
92-
checked={field.value === value}
93-
onChange={() => field.onChange(value)}
93+
label={option.label}
94+
value={option.value}
95+
checked={field.value === option.value}
96+
onChange={() => field.onChange(option.value)}
9497
/>
9598
))}
9699
</>
@@ -99,5 +102,17 @@ const Radio = ({
99102
)
100103
}
101104

105+
const Tag = ({ name, ...props }: TagInputProps): JSX.Element => {
106+
const { control } = useFormContext()
107+
return (
108+
<Controller
109+
name={name}
110+
control={control}
111+
render={() => <TagInput name={name} {...props} />}
112+
/>
113+
)
114+
}
115+
102116
Form.Checkbox = Checkbox
103117
Form.Radio = Radio
118+
Form.TagInput = Tag

0 commit comments

Comments
 (0)