Field — 폼 입력을 감싸는 유연한 컨테이너
폼 입력, 라벨, 헬퍼 텍스트를 위한 유연한 컨테이너를 제공해 주는 컴포넌트예요. 폼 요소에 invalid, disabled, required, readOnly 같은 맥락을 공급해 줘요.
출처: 문서
본문
구조 (Anatomy)
<Field.Root>
<Field.Label />
<Field.Input />
<Field.Textarea />
<Field.Select />
<Field.HelperText />
<Field.ErrorText />
</Field.Root>
예제 (Examples)
Field 컴포넌트는 폼 요소를 위한 invalid, disabled, required, readOnly 같은 맥락을 제공해요. 대부분의 Ark UI 컴포넌트가 이런 맥락을 기본적으로 지원하지만, 표준 HTML 폼 요소와 함께 Field 컴포넌트를 사용할 수도 있어요.
Input
이 예제는 표준 input 필드와 함께 Field 컴포넌트를 사용하는 방법을 보여 줘요.
import { Field } from '@ark-ui/react/field'
import styles from 'styles/field.module.css'
export const Input = () => (
<Field.Root className={styles.Root}>
<Field.Label className={styles.Label}>Label</Field.Label>
<Field.Input className={styles.Input} />
<Field.HelperText className={styles.HelperText}>Some additional Info</Field.HelperText>
<Field.ErrorText className={styles.ErrorText}>Error Info</Field.ErrorText>
</Field.Root>
)
Textarea
이 예제는 textarea 요소와 함께 Field 컴포넌트를 사용하는 방법을 보여 줘요.
import { Field } from '@ark-ui/react/field'
import styles from 'styles/field.module.css'
export const Textarea = () => (
<Field.Root className={styles.Root}>
<Field.Label className={styles.Label}>Label</Field.Label>
<Field.Textarea className={styles.Textarea} />
<Field.HelperText className={styles.HelperText}>Some additional Info</Field.HelperText>
<Field.ErrorText className={styles.ErrorText}>Error Info</Field.ErrorText>
</Field.Root>
)
Textarea 자동 크기 조절 (Textarea Autoresize)
Textarea 컴포넌트에 autoresize 프롭을 넘기면 사용자가 입력할 때 자동으로 크기가 조절되게 할 수 있어요.
import { Field } from '@ark-ui/react/field'
import styles from 'styles/field.module.css'
export const TextareaAutoresize = () => (
<Field.Root className={styles.Root}>
<Field.Label className={styles.Label}>Label</Field.Label>
<Field.Textarea className={styles.Textarea} autoresize />
<Field.HelperText className={styles.HelperText}>Some additional Info</Field.HelperText>
<Field.ErrorText className={styles.ErrorText}>Error Info</Field.ErrorText>
</Field.Root>
)
Select
이 예제는 Field 컴포넌트를 select 드롭다운과 통합하는 방법을 보여 줘요.
import { Field } from '@ark-ui/react/field'
import styles from 'styles/field.module.css'
export const Select = () => (
<Field.Root className={styles.Root}>
<Field.Label className={styles.Label}>Label</Field.Label>
<Field.Select className={styles.Select}>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</Field.Select>
<Field.HelperText className={styles.HelperText}>Some additional Info</Field.HelperText>
<Field.ErrorText className={styles.ErrorText}>Error Info</Field.ErrorText>
</Field.Root>
)
Checkbox
이 예제는 Field 컴포넌트와 Checkbox 컴포넌트를 통합하는 방법을 보여 줘요.
import { Checkbox } from '@ark-ui/react/checkbox'
import { Field } from '@ark-ui/react/field'
import { CheckIcon, MinusIcon } from 'lucide-react'
import styles from 'styles/checkbox.module.css'
import field from 'styles/field.module.css'
export const WithField = () => (
<Field.Root className={field.Root} data-inline>
<Checkbox.Root className={styles.Root}>
<Checkbox.Control className={styles.Control}>
<Checkbox.Indicator className={styles.Indicator}>
<CheckIcon />
</Checkbox.Indicator>
<Checkbox.Indicator className={styles.Indicator} indeterminate>
<MinusIcon />
</Checkbox.Indicator>
</Checkbox.Control>
<Checkbox.Label className={styles.Label}>Label</Checkbox.Label>
<Checkbox.HiddenInput />
</Checkbox.Root>
<Field.HelperText className={field.HelperText}>Additional Info</Field.HelperText>
<Field.ErrorText className={field.ErrorText}>Error Info</Field.ErrorText>
</Field.Root>
)
루트 프로바이더 (Root Provider)
다른 방식으로 field를 제어하려면 RootProvider 컴포넌트와 useField 훅을 사용할 수 있어요. 이렇게 하면 컴포넌트 밖에서도 상태와 메서드에 접근할 수 있어요.
import { Field, useField } from '@ark-ui/react/field'
import { useState } from 'react'
import styles from 'styles/field.module.css'
import button from 'styles/button.module.css'
export const RootProvider = () => {
const [invalid, setInvalid] = useState(false)
const field = useField({ invalid })
return (
<>
<button className={button.Root} style={{ marginBottom: '1rem' }} onClick={() => setInvalid((prev) => !prev)}>
Toggle Invalid
</button>
<Field.RootProvider className={styles.Root} value={field}>
<Field.Label className={styles.Label}>Label</Field.Label>
<Field.Input className={styles.Input} />
<Field.HelperText className={styles.HelperText}>Some additional Info</Field.HelperText>
<Field.ErrorText className={styles.ErrorText}>Error Info</Field.ErrorText>
</Field.RootProvider>
</>
)
}
커스텀 컨트롤 (Custom Control)
Field.Context나 useFieldContext 훅을 사용해 field의 내부 상태에 접근할 수 있어요. 이렇게 하면 커스텀 컨트롤을 Field 컴포넌트와 연결하는 데 도움이 돼요.
import { Field } from '@ark-ui/react/field'
import styles from 'styles/field.module.css'
export const CustomControl = () => (
<Field.Root className={styles.Root} invalid>
<Field.Label className={styles.Label}>Any Control</Field.Label>
<Field.Context>{(context) => <input {...context.getInputProps()} />}</Field.Context>
<Field.HelperText className={styles.HelperText}>Uses getInputProps() for maximum flexibility</Field.HelperText>
<Field.ErrorText className={styles.ErrorText}>This field has an error</Field.ErrorText>
</Field.Root>
)
API 참조 (API Reference)
Root
프롭 (Props)
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하고, 그 프롭과 동작을 결합해요.
disabled
Type: boolean
Required: false
Default Value: undefined
Description: field가 비활성화되었는지 여부를 나타내요.
ids
Type: ElementIds
Required: false
Default Value: undefined
Description: field 파트들의 id예요.
invalid
Type: boolean
Required: false
Default Value: undefined
Description: field가 유효하지 않은지 여부를 나타내요.
readOnly
Type: boolean
Required: false
Default Value: undefined
Description: field가 읽기 전용인지 여부를 나타내요.
required
Type: boolean
Required: false
Default Value: undefined
Description: field가 필수인지 여부를 나타내요.
target
Type: string
Required: false
Default Value: undefined
Description: 라벨이 가리켜야 할 대상 field 항목 값이에요.
ErrorText
프롭 (Props)
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하고, 그 프롭과 동작을 결합해요.
HelperText
프롭 (Props)
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하고, 그 프롭과 동작을 결합해요.
Input
프롭 (Props)
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하고, 그 프롭과 동작을 결합해요.
Item
프롭 (Props)
value
Type: string
Required: true
Default Value: undefined
Description: undefined
Label
프롭 (Props)
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하고, 그 프롭과 동작을 결합해요.
RequiredIndicator
프롭 (Props)
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하고, 그 프롭과 동작을 결합해요.
fallback
Type: string | number | bigint | boolean | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode> | ReactPortal | Promise<...>
Required: false
Default Value: undefined
Description: undefined
RootProvider
프롭 (Props)
value
Type: { ariaDescribedby: string | undefined; ids: { root: string; control: string; label: string; errorText: string; helperText: string; }; refs: { rootRef: RefObject<HTMLDivElement | null>; }; ... 11 more ...; getRequiredIndicatorProps: () => Omit<...>; }
Required: true
Default Value: undefined
Description: undefined
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하고, 그 프롭과 동작을 결합해요.
Select
프롭 (Props)
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하고, 그 프롭과 동작을 결합해요.
Textarea
프롭 (Props)
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하고, 그 프롭과 동작을 결합해요.
autoresize
Type: boolean
Required: false
Default Value: false
Description: textarea가 자동으로 크기를 조절할지 여부예요.