Skip to content

Commit fc401b6

Browse files
committed
[#98] ✨ Add email validation logic and Update variable names in form
1 parent 404c153 commit fc401b6

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/components/shared/form/Form.tsx

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,20 @@ export const Form = <TFieldValues extends FieldValues>({
4545
)
4646
}
4747

48-
const Text = ({
48+
const FormText = ({
4949
name,
5050
...props
5151
}: {
5252
name: FormField
5353
} & TextInputProps): JSX.Element => {
5454
const {
5555
register,
56-
formState: { errors },
56+
formState: { errors, touchedFields },
57+
watch,
5758
} = useFormContext()
59+
const value = watch(name)
60+
const isError = touchedFields[name] && errors[name]
61+
const isSuccess = touchedFields[name] && !errors[name] && value
5862

5963
return (
6064
<>
@@ -63,16 +67,22 @@ const Text = ({
6367
error={Boolean(errors[name])}
6468
{...props}
6569
/>
66-
{errors[name] && (
70+
{isError && (
6771
<StatusMessage hasError={true}>
6872
{String(errors[name]?.message as string)}
6973
</StatusMessage>
7074
)}
75+
{/* TODO: 이미 가입된 이메일 확인하는 로직 추가하면서 수정 필요 */}
76+
{isSuccess && (
77+
<StatusMessage hasError={false}>
78+
가입 가능한 이메일입니다.
79+
</StatusMessage>
80+
)}
7181
</>
7282
)
7383
}
7484

75-
const Password = ({
85+
const FormPassword = ({
7686
name,
7787
...props
7888
}: {
@@ -101,7 +111,7 @@ const Password = ({
101111
)
102112
}
103113

104-
const Introduce = ({
114+
const FormTextArea = ({
105115
name,
106116
...props
107117
}: { name: FormField } & TextAreaProps): JSX.Element => {
@@ -122,7 +132,7 @@ const Introduce = ({
122132
)
123133
}
124134

125-
const Checkbox = ({
135+
const FormCheckbox = ({
126136
name,
127137
rules,
128138
options,
@@ -162,7 +172,7 @@ const Checkbox = ({
162172
)
163173
}
164174

165-
const Radio = ({
175+
const FormRadio = ({
166176
name,
167177
rules,
168178
options,
@@ -196,7 +206,7 @@ const Radio = ({
196206
)
197207
}
198208

199-
const Tag = ({
209+
const FormTag = ({
200210
name,
201211
rules,
202212
...props
@@ -237,9 +247,9 @@ const StatusMessage = ({
237247
)
238248
}
239249

240-
Form.Text = Text
241-
Form.Password = Password
242-
Form.Introduce = Introduce
243-
Form.Checkbox = Checkbox
244-
Form.Radio = Radio
245-
Form.TagInput = Tag
250+
Form.Text = FormText
251+
Form.Password = FormPassword
252+
Form.TextArea = FormTextArea
253+
Form.Checkbox = FormCheckbox
254+
Form.Radio = FormRadio
255+
Form.TagInput = FormTag

0 commit comments

Comments
 (0)