Fieldset — 공통 이름으로 묶는 폼 컨트롤 그룹
공통된 이름 아래 선택적으로 그룹화된 폼 컨트롤 집합이에요. 폼 요소를 위한 invalid와 disabled 같은 맥락을 제공해 줘요.
출처: 문서
본문
구조 (Anatomy)
<Fieldset.Root>
<Fieldset.Legend />
<Fieldset.HelperText />
<Fieldset.ErrorText />
</Fieldset.Root>
예제 (Examples)
Fieldset 컴포넌트는 폼 요소를 위한 invalid와 disabled 같은 맥락을 제공해요. 대부분의 Ark UI 컴포넌트가 이런 맥락을 기본적으로 지원하지만, 표준 HTML 폼 요소와 함께 Field 컴포넌트를 사용할 수도 있어요.
기본 (Basic)
import { Field } from '@ark-ui/react/field'
import { Fieldset } from '@ark-ui/react/fieldset'
import field from 'styles/field.module.css'
import styles from 'styles/fieldset.module.css'
export const Basic = () => {
return (
<Fieldset.Root className={styles.Root}>
<Fieldset.Legend className={styles.Legend}>Contact Details</Fieldset.Legend>
<Field.Root className={field.Root}>
<Field.Label className={field.Label}>Name</Field.Label>
<Field.Input className={field.Input} placeholder="John Doe" />
</Field.Root>
<Field.Root className={field.Root}>
<Field.Label className={field.Label}>Email</Field.Label>
<Field.Input className={field.Input} type="email" placeholder="john@example.com" />
</Field.Root>
</Fieldset.Root>
)
}
Field
이 예제는 Fieldset 안에서 표준 input 필드와 함께 Field 컴포넌트를 사용하는 방법을 보여 줘요.
import { Field } from '@ark-ui/react/field'
import { Fieldset } from '@ark-ui/react/fieldset'
import field from 'styles/field.module.css'
import styles from 'styles/fieldset.module.css'
export const WithField = () => {
return (
<Fieldset.Root className={styles.Root}>
<Fieldset.Legend className={styles.Legend}>Personal Information</Fieldset.Legend>
<Field.Root className={field.Root}>
<Field.Label className={field.Label}>First Name</Field.Label>
<Field.Input className={field.Input} placeholder="Enter your first name" />
<Field.HelperText className={field.HelperText}>As it appears on your ID</Field.HelperText>
</Field.Root>
<Field.Root className={field.Root}>
<Field.Label className={field.Label}>Last Name</Field.Label>
<Field.Input className={field.Input} placeholder="Enter your last name" />
</Field.Root>
</Fieldset.Root>
)
}
Checkbox
이 예제는 Checkbox 같은 다른 Ark UI 폼 요소와 함께 Fieldset 컴포넌트를 사용하는 방법을 보여 줘요.
import { Checkbox } from '@ark-ui/react/checkbox'
import { Fieldset } from '@ark-ui/react/fieldset'
import { CheckIcon } from 'lucide-react'
import checkbox from 'styles/checkbox.module.css'
import styles from 'styles/fieldset.module.css'
export const WithCheckbox = () => {
return (
<Fieldset.Root className={styles.Root}>
<Fieldset.Legend className={styles.Legend}>Email Preferences</Fieldset.Legend>
<Checkbox.Root className={checkbox.Root} defaultChecked>
<Checkbox.Control className={checkbox.Control}>
<Checkbox.Indicator className={checkbox.Indicator}>
<CheckIcon />
</Checkbox.Indicator>
</Checkbox.Control>
<Checkbox.Label className={checkbox.Label}>Product updates</Checkbox.Label>
<Checkbox.HiddenInput />
</Checkbox.Root>
<Checkbox.Root className={checkbox.Root}>
<Checkbox.Control className={checkbox.Control}>
<Checkbox.Indicator className={checkbox.Indicator}>
<CheckIcon />
</Checkbox.Indicator>
</Checkbox.Control>
<Checkbox.Label className={checkbox.Label}>Marketing emails</Checkbox.Label>
<Checkbox.HiddenInput />
</Checkbox.Root>
</Fieldset.Root>
)
}
루트 프로바이더 (Root Provider)
다른 방식으로 fieldset을 제어하려면 RootProvider 컴포넌트와 useFieldset 훅을 사용할 수 있어요. 이렇게 하면 컴포넌트 밖에서도 상태와 메서드에 접근할 수 있어요.
import { Field } from '@ark-ui/react/field'
import { Fieldset, useFieldset } from '@ark-ui/react/fieldset'
import field from 'styles/field.module.css'
import styles from 'styles/fieldset.module.css'
export const RootProvider = () => {
const fieldset = useFieldset()
return (
<Fieldset.RootProvider className={styles.Root} value={fieldset}>
<Fieldset.Legend className={styles.Legend}>Contact Details</Fieldset.Legend>
<Field.Root className={field.Root}>
<Field.Label className={field.Label}>Name</Field.Label>
<Field.Input className={field.Input} placeholder="John Doe" />
</Field.Root>
<Field.Root className={field.Root}>
<Field.Label className={field.Label}>Email</Field.Label>
<Field.Input className={field.Input} type="email" placeholder="john@example.com" />
</Field.Root>
</Fieldset.RootProvider>
)
}
Select가 있는 입력 (Input with Select)
이 예제는 Field.Input과 Select와 함께 Fieldset 컴포넌트를 사용해 상호작용하는 전화번호 입력 컴포넌트를 만드는 방법을 보여 줘요.
import { Field } from '@ark-ui/react/field'
import { Fieldset } from '@ark-ui/react/fieldset'
import { Portal } from '@ark-ui/react/portal'
import { Select, createListCollection } from '@ark-ui/react/select'
import { ChevronsUpDownIcon } from 'lucide-react'
import { useRef } from 'react'
import field from 'styles/field.module.css'
import styles from 'styles/fieldset.module.css'
import select from 'styles/select.module.css'
export const PhoneInput = () => {
const extensions = createListCollection({
items: [
{ label: '+1', value: '+1' },
{ label: '+44', value: '+44' },
{ label: '+49', value: '+49' },
{ label: '+41', value: '+41' },
],
})
const inputRef = useRef<HTMLInputElement | null>(null)
const focusInput = () => {
setTimeout(() => {
inputRef.current?.focus()
})
}
return (
<Fieldset.Root className={styles.Root}>
<Fieldset.Legend className={styles.Legend} onClick={focusInput}>
Mobile Number
</Fieldset.Legend>
<div style={{ display: 'flex', alignItems: 'flex-start', gap: '0.5rem' }}>
<Field.Root>
<Select.Root className={select.Root} collection={extensions} defaultValue={['+1']} onValueChange={focusInput}>
<Select.Control className={select.Control}>
<Select.Trigger className={select.Trigger}>
<Select.ValueText placeholder="Select" />
<ChevronsUpDownIcon />
</Select.Trigger>
</Select.Control>
<Portal>
<Select.Positioner>
<Select.Content className={select.Content}>
{extensions.items.map((item) => (
<Select.Item className={select.Item} key={item.value} item={item}>
<Select.ItemText className={select.ItemText}>{item.label}</Select.ItemText>
</Select.Item>
))}
</Select.Content>
</Select.Positioner>
</Portal>
<Select.HiddenSelect />
</Select.Root>
</Field.Root>
<Field.Root className={field.Root}>
<Field.Input className={field.Input} ref={inputRef} />
</Field.Root>
</div>
</Fieldset.Root>
)
}
API 참조 (API Reference)
Root
프롭 (Props)
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하고, 그 프롭과 동작을 결합해요.
invalid
Type: boolean
Required: false
Default Value: undefined
Description: fieldset이 유효하지 않은지 여부를 나타내요.
ErrorText
프롭 (Props)
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하고, 그 프롭과 동작을 결합해요.
HelperText
프롭 (Props)
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하고, 그 프롭과 동작을 결합해요.
Legend
프롭 (Props)
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하고, 그 프롭과 동작을 결합해요.
RootProvider
프롭 (Props)
value
Type: { refs: { rootRef: RefObject<HTMLFieldSetElement | null>; }; ids: { legend: string; errorText: string; helperText: string; }; disabled: boolean; invalid: boolean; getRootProps: () => Omit<...>; getLegendProps: () => Omit<...>; getHelperTextProps: () => Omit<...>; getErrorTextProps: () => Omit<...>; }
Required: true
Default Value: undefined
Description: undefined
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하고, 그 프롭과 동작을 결합해요.