-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add examples, fix imports in stories
- Loading branch information
Showing
18 changed files
with
590 additions
and
91 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,66 @@ | ||
"use client"; | ||
|
||
import { useController, useForm } from "react-hook-form"; | ||
import { Checkbox } from "seed-design/ui/checkbox"; | ||
import { ActionButton } from "seed-design/ui/action-button"; | ||
import { useCallback, type FormEvent } from "react"; | ||
|
||
const POSSIBLE_FRUIT_VALUES = ["apple", "melon", "mango"] as const; | ||
|
||
type FormValues = Record<(typeof POSSIBLE_FRUIT_VALUES)[number], boolean>; | ||
|
||
export default function CheckboxReactHookForm() { | ||
const { handleSubmit, reset, setValue, control } = useForm<FormValues>({ | ||
defaultValues: { | ||
apple: false, | ||
melon: true, | ||
mango: false, | ||
}, | ||
}); | ||
|
||
const onValid = useCallback((data: FormValues) => { | ||
window.alert(JSON.stringify(data, null, 2)); | ||
}, []); | ||
|
||
const onReset = useCallback( | ||
(event: FormEvent) => { | ||
event.preventDefault(); | ||
reset(); | ||
}, | ||
[reset], | ||
); | ||
|
||
return ( | ||
<form className="flex flex-col gap-3 w-96" onSubmit={handleSubmit(onValid)} onReset={onReset}> | ||
<div className="flex flex-col"> | ||
{POSSIBLE_FRUIT_VALUES.map((name) => { | ||
const { | ||
field: { value, ...restProps }, | ||
fieldState: { invalid }, | ||
} = useController({ name, control }); | ||
|
||
return ( | ||
<Checkbox | ||
key={name} | ||
label={name} | ||
checked={value} | ||
inputProps={restProps} | ||
invalid={invalid} | ||
/> | ||
); | ||
})} | ||
</div> | ||
<div className="flex gap-2"> | ||
<ActionButton type="submit" className="grow"> | ||
제출 | ||
</ActionButton> | ||
<ActionButton type="button" variant="neutralWeak" onClick={() => setValue("mango", true)}> | ||
mango 선택 | ||
</ActionButton> | ||
<ActionButton type="reset" variant="neutralWeak"> | ||
초기화 | ||
</ActionButton> | ||
</div> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
"use client"; | ||
|
||
import { ActionButton } from "seed-design/ui/action-button"; | ||
import { TextField, TextFieldTextarea } from "seed-design/ui/text-field"; | ||
import { useState, useCallback, type FormEvent } from "react"; | ||
|
||
interface FormValues { | ||
bio: string; | ||
address: string; | ||
} | ||
|
||
type FieldErrors = Record<keyof FormValues, string | null>; | ||
|
||
export default function MultilineTextFieldForm() { | ||
const [formValues, setFormValues] = useState<FormValues>({ | ||
bio: "", | ||
address: "", | ||
}); | ||
|
||
const [fieldErrors, setFieldStates] = useState<FieldErrors>({ | ||
bio: null, | ||
address: null, | ||
}); | ||
|
||
const validateForm = useCallback((): boolean => { | ||
let isValid = true; | ||
|
||
const newFieldErrors: FieldErrors = { | ||
bio: null, | ||
address: null, | ||
}; | ||
|
||
// Name validation | ||
if (!formValues.bio) { | ||
newFieldErrors.bio = "필수 입력 항목입니다"; | ||
isValid = false; | ||
} | ||
|
||
if (!formValues.address.startsWith("대한민국")) { | ||
newFieldErrors.address = "대한민국으로 시작해주세요"; | ||
isValid = false; | ||
} | ||
|
||
if (!formValues.address) { | ||
newFieldErrors.address = "필수 입력 항목입니다"; | ||
isValid = false; | ||
} | ||
|
||
setFieldStates(newFieldErrors); | ||
|
||
return isValid; | ||
}, [formValues]); | ||
|
||
const handleSubmit = useCallback( | ||
(event: FormEvent) => { | ||
event.preventDefault(); | ||
|
||
if (validateForm()) { | ||
window.alert(JSON.stringify(formValues, null, 2)); | ||
} | ||
}, | ||
[formValues, validateForm], | ||
); | ||
|
||
const handleReset = useCallback((event: FormEvent) => { | ||
event.preventDefault(); | ||
|
||
setFormValues({ bio: "", address: "" }); | ||
setFieldStates({ bio: null, address: null }); | ||
}, []); | ||
|
||
const handleNameChange = (value: string) => { | ||
setFormValues((prev) => ({ ...prev, bio: value })); | ||
setFieldStates((prev) => ({ ...prev, name: null })); | ||
}; | ||
|
||
const handleAddressChange = (value: string) => { | ||
setFormValues((prev) => ({ ...prev, address: value })); | ||
setFieldStates((prev) => ({ ...prev, address: null })); | ||
}; | ||
|
||
return ( | ||
<form className="grid grid-cols-2 gap-3 w-full" onSubmit={handleSubmit} onReset={handleReset}> | ||
<TextField | ||
label="자기소개" | ||
indicator="(필수)" | ||
description="자기소개를 써주세요" | ||
required | ||
value={formValues.bio} | ||
onValueChange={({ value }) => handleNameChange(value)} | ||
{...(fieldErrors.bio && { invalid: true, errorMessage: fieldErrors.bio })} | ||
> | ||
<TextFieldTextarea placeholder="저는…" /> | ||
</TextField> | ||
<TextField | ||
label="주소" | ||
indicator="(필수)" | ||
description="주소를 써주세요" | ||
maxGraphemeCount={30} | ||
required | ||
value={formValues.address} | ||
onValueChange={({ slicedValue }) => handleAddressChange(slicedValue)} | ||
{...(fieldErrors.address && { invalid: true, errorMessage: fieldErrors.address })} | ||
> | ||
<TextFieldTextarea placeholder="대한민국" /> | ||
</TextField> | ||
<div className="col-span-2 flex gap-2"> | ||
<ActionButton type="submit" className="grow"> | ||
제출 | ||
</ActionButton> | ||
<ActionButton type="reset" variant="neutralWeak"> | ||
초기화 | ||
</ActionButton> | ||
</div> | ||
</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
96 changes: 65 additions & 31 deletions
96
docs/components/example/multiline-text-field-react-hook-form.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
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.