ControlField (컨트롤 필드)
ControlField는 라벨, 설명(또는 다른 콘텐츠), 그리고 컨트롤 컴포넌트(Switch, Checkbox)를 하나의 눌러지는(pressable) 영역으로 결합하는 필드 컴포넌트예요. 라벨과 컨트롤 사이를 자연스럽게 연결해 일관된 레이아웃과 상태 관리를 제공해요.
출처: 문서
본문
Import
import { ControlField } from 'heroui-native';
Anatomy
<ControlField>
<Label>...</Label>
<Description>...</Description>
<ControlField.Indicator>...</ControlField.Indicator>
<FieldError>...</FieldError>
</ControlField>
- ControlField: 레이아웃과 상태 전파를 관리하는 루트 컨테이너예요.
- Label: 컨트롤의 기본 텍스트 라벨이에요 (Label 컴포넌트에서 가져와요).
- Description: 보조 설명용 헬퍼 텍스트예요 (Description 컴포넌트에서 가져와요).
- ControlField.Indicator: 폼 컨트롤 컴포넌트(Switch, Checkbox, Radio)를 담는 컨테이너예요.
- FieldError: 검증 오류 메시지 표시예요 (FieldError 컴포넌트에서 가져와요).
Usage
Basic Usage
ControlField는 폼 컨트롤을 감싸 일관된 레이아웃과 상태 관리를 제공해요.
<ControlField isSelected={value} onSelectedChange={setValue}>
<Label className="flex-1">Label text</Label>
<ControlField.Indicator />
</ControlField>
With Description
Description 컴포넌트를 사용해 라벨 아래에 헬퍼 텍스트를 추가해요.
<ControlField isSelected={value} onSelectedChange={setValue}>
<View className="flex-1">
<Label>Enable notifications</Label>
<Description>
Receive push notifications about your account activity
</Description>
</View>
<ControlField.Indicator />
</ControlField>
With Error Message
ErrorMessage 컴포넌트를 사용해 검증 오류를 표시해요.
<ControlField
isSelected={value}
onSelectedChange={setValue}
isInvalid={!value}
className="flex-col items-start gap-1"
>
<View className="flex-row items-center gap-2">
<View className="flex-1">
<Label>I agree to the terms</Label>
<Description>
By checking this box, you agree to our Terms of Service
</Description>
</View>
<ControlField.Indicator variant="checkbox" />
</View>
<FieldError>This field is required</FieldError>
</ControlField>
Disabled State
disabled prop으로 상호작용을 제어해요.
<ControlField isSelected={value} onSelectedChange={setValue} isDisabled>
<View className="flex-1">
<Label>Disabled field</Label>
<Description>This field is disabled</Description>
</View>
<ControlField.Indicator />
</ControlField>
Disabling All Animations
"disable-all"을 사용해 자식을 포함한 모든 애니메이션을 비활성화해요. 이 설정은 모든 자식 컴포넌트로 전파돼요.
<ControlField
isSelected={value}
onSelectedChange={setValue}
animation="disable-all"
>
<View className="flex-1">
<Label>Label text</Label>
<Description>Description text</Description>
</View>
<ControlField.Indicator />
</ControlField>
Example
import {
Checkbox,
Description,
FieldError,
ControlField,
Label,
Switch,
} from 'heroui-native';
import React from 'react';
import { ScrollView, View } from 'react-native';
export default function ControlFieldExample() {
const [notifications, setNotifications] = React.useState(false);
const [terms, setTerms] = React.useState(false);
const [newsletter, setNewsletter] = React.useState(true);
return (
<ScrollView className="bg-background p-4">
<View className="gap-4">
<ControlField
isSelected={notifications}
onSelectedChange={setNotifications}
>
<View className="flex-1">
<Label>Enable notifications</Label>
<Description>
Receive push notifications about your account activity
</Description>
</View>
<ControlField.Indicator />
</ControlField>
<ControlField
isSelected={terms}
onSelectedChange={setTerms}
isInvalid={!terms}
className="flex-col items-start gap-1"
>
<View className="flex-row items-center gap-2">
<View className="flex-1">
<Label>I agree to the terms and conditions</Label>
<Description>
By checking this box, you agree to our Terms of Service
</Description>
</View>
<ControlField.Indicator className="mt-0.5">
<Checkbox />
</ControlField.Indicator>
</View>
<FieldError>This field is required</FieldError>
</ControlField>
<ControlField isSelected={newsletter} onSelectedChange={setNewsletter}>
<View className="flex-1">
<Label>Subscribe to newsletter</Label>
</View>
<ControlField.Indicator>
<Checkbox color="warning" />
</ControlField.Indicator>
</ControlField>
</View>
</ScrollView>
);
}
더 많은 예시는 GitHub repository에서 찾을 수 있어요.
API Reference
ControlField
| prop | type | default | 설명 |
|---|---|---|---|
| children | React.ReactNode | ((props: ControlFieldRenderProps) => React.ReactNode) |
- | 폼 컨트롤 안에 렌더링할 콘텐츠 또는 렌더 함수 |
| isSelected | boolean |
undefined |
컨트롤이 선택/체크되었는지 여부 |
| isDisabled | boolean |
false |
폼 컨트롤이 비활성화되었는지 여부 |
| isInvalid | boolean |
false |
폼 컨트롤이 유효하지 않은지 여부 |
| isRequired | boolean |
false |
폼 컨트롤이 필수인지 여부 |
| className | string |
- | 루트 요소의 커스텀 클래스 이름 |
| onSelectedChange | (isSelected: boolean) => void |
- | 선택 상태가 변경될 때 호출되는 콜백 |
| animation | "disable-all" | undefined |
undefined |
애니메이션 설정. "disable-all"을 사용하면 자식을 포함한 모든 애니메이션이 비활성화돼요 |
| ...PressableProps | PressableProps |
- | 모든 React Native Pressable props가 지원돼요 |
Label
Label 컴포넌트는 ControlField 컨텍스트에서 폼 상태(isDisabled, isInvalid)를 자동으로 소비해요.
Note: 전체 prop 문서는 Label 컴포넌트 문서를 참고해요.
Description
Description 컴포넌트는 ControlField 컨텍스트에서 폼 상태(isDisabled, isInvalid)를 자동으로 소비해요.
Note: 전체 prop 문서는 Description 컴포넌트 문서를 참고해요.
ControlField.Indicator
| prop | type | default | 설명 |
|---|---|---|---|
| children | React.ReactNode |
- | 렌더링할 컨트롤 컴포넌트 (Switch, Checkbox, Radio) |
| variant | 'checkbox' | 'radio' | 'switch' |
'switch' |
자식이 없을 때 렌더링할 컨트롤의 variant |
| className | string |
- | 인디케이터 요소의 커스텀 클래스 이름 |
| ...ViewProps | ViewProps |
- | 모든 React Native View props가 지원돼요 |
Note: 자식이 제공되면 자식 컴포넌트에 이미 존재하지 않는 경우 ControlField 컨텍스트의 isSelected, onSelectedChange, isDisabled, isInvalid props가 자동으로 전달돼요. radio variant를 사용하면 Radio 컴포넌트가 독립(standalone) 모드(RadioGroup 밖)로 렌더링돼요.
FieldError
FieldError 컴포넌트는 ControlField 컨텍스트에서 폼 상태(isInvalid)를 자동으로 소비해요.
Note: 전체 prop 문서는 FieldError 컴포넌트 문서를 참고해요. 오류 메시지 표시 여부는 부모 ControlField의 isInvalid 상태로 제어돼요.
Hooks
useControlField
Returns:
| property | type | 설명 |
|---|---|---|
isSelected |
boolean | undefined |
컨트롤이 선택/체크되었는지 여부 |
onSelectedChange |
((isSelected: boolean) => void) | undefined |
선택 상태가 변경될 때 호출되는 콜백 |
isDisabled |
boolean |
폼 컨트롤이 비활성화되었는지 여부 |
isInvalid |
boolean |
폼 컨트롤이 유효하지 않은지 여부 |
isPressed |
SharedValue<boolean> |
누름(press) 상태를 나타내는 Reanimated 공유 값 |