-
Notifications
You must be signed in to change notification settings - Fork 3
Uncontrolled Component
J Design System์ ๊ตฌ์ถํ๋ ๊ณผ์ ์์ ๊ฐ์ฅ ์ค์ํ๊ฒ ์๊ฐํ ๊ณผ์ ๋ '์ฌ์ฉ์ ๊ฒฝํ'์ด์์ต๋๋ค. UI ์ปดํฌ๋ํธ๋ฅผ ํค๋ณด๋ ์ ์ด๋ฅผ ํตํด ์กฐ์ํ ์ ์๋๋ก ๊ตฌํํ์์ผ๋ฉฐ, ์ด๋ฅผ ์ํด ๋น์ ์ด ์ปดํฌ๋ํธ ๋ฐฉ์์ ์ฌ์ฉํ์ฌ React์ ์ํ ๊ด๋ฆฌ ํ๋ฆ์์ ๋ฒ์ด๋ DOM ์์์ ์ง์ ์ํธ์์ฉํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ์์ต๋๋ค.
React Hook Form์ ์ธ๊ธฐ๊ฐ ๋ง์ React Form ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค. J Design System์ ์ฌ์ฉํ๋ฉด React Hook Form์์๋ ์ฌ์ฉํ ์ ์๋๋ก register
ref Ojbect๋ฅผ ์์ฉํ๊ธฐ ์ํด forwardRef
๋ก ๊ฐ์ธ์ฃผ์ด React Hook Form๊ณผ ์ฐ๊ณ๋ ์ ์๋๋ก ์ฌ์ฉ์ ๊ฒฝํ์ ๊ณ ๋ คํ์์ต๋๋ค.
//Textarea.tsx
type ExtendedTextAreaProps = TextAreaProps & { Label?: typeof TextareaLabel };
export const Textarea = Object.assign(
forwardRef<HTMLTextAreaElement, ExtendedTextAreaProps>((textAreaProps, ref) => {
const { children, ...propsWithoutChildren } = textAreaProps;
const textareaLabel = filterComponent(children, TextareaLabel, true);
return (
<TextareaProvider textareaProps={propsWithoutChildren}>
<TextareaContainer children={textareaLabel} ref={ref} />
</TextareaProvider>
);
}),
{
Label: TextareaLabel
}
);
//TextareaContainer
export const TextareaContainer = forwardRef(
(props: { children: React.ReactNode }, externalRef: ForwardedRef<any> | null) => {
const { styleProps, textareaProps, textareaId } = useContext<ReturnContext>(TextareaContext);
const { onChange, ...attributesWithoutOnChange } = textareaProps;
const textareaStyle = createTextareaStyle(styleProps, TEXTAREA_PLACEHOLDER_COLOR);
const wrapperStyle = createWrapperStyle(styleProps.width);
const { textareaRef, handleChange } = useSmart(onChange, styleProps.resize);
const { getLabelId } = useTextarea();
const labelId = getLabelId(props.children);
return (
<div css={wrapperStyle}>
{props.children}
<textarea
role="textbox"
aria-multiline="true"
id={textareaId}
css={textareaStyle}
ref={e => {
if (typeof externalRef === 'function') {
externalRef(e);
}
textareaRef.current = e;
}}
onChange={handleChange()}
{...(textareaProps.placeholder && { 'aria-placeholder': textareaProps.placeholder })}
{...(labelId && { 'aria-labelledby': labelId })}
{...attributesWithoutOnChange}
/>
</div>
);
}
);
์์ ์์๋ ์ค์ ์์ฑ๋ Textarea
์ฝ๋ ์ด๋ฉฐ, Input, Radio, Checkbox ๋ฑ Form์ ํ์ํ ์์๋ค๋ ์ฌ์ฉ์ด ๊ฐ๋ฅํฉ๋๋ค.