TextField (TextField)
TextField는 라벨, 설명, 오류 처리를 갖춘 완전한 폼 입력 구조를 제공하는 컴포넌트예요. Label, Input, Description, FieldError 컴포넌트를 조합해 일관된 폼 필드를 만들 수 있답니다. 필수 표시, 검증, 비활성 상태 등 폼 개발에 필요한 기능을 갖추고 있어요.
출처: 문서
본문
Import
import { TextField } from 'heroui-native';
Anatomy
<TextField>
<Label>...</Label>
<Input />
<Description>...</Description>
<FieldError>...</FieldError>
</TextField>
- TextField: 간격과 상태 관리를 제공하는 루트 컨테이너예요.
- Label: 필수 필드에 선택적 별표(*)가 붙는 라벨이에요. (Label 컴포넌트에서 가져옴)
- Input: 애니메이션 테두리와 배경이 있는 입력 컨테이너예요. (Input 컴포넌트에서 가져옴)
- Description: 보조 설명 도우미 텍스트예요. (Description 컴포넌트에서 가져옴)
- FieldError: 검증 오류 메시지 표시예요. (FieldError 컴포넌트에서 가져옴)
Usage
기본 사용법
TextField는 라벨과 설명이 있는 완전한 폼 입력 구조를 제공해요.
<TextField>
<Label>Email</Label>
<Input placeholder="Enter your email" />
<Description>We'll never share your email</Description>
</TextField>
필수 필드와 함께
필드를 필수로 표시하면 라벨에 별표가 나타나요.
<TextField isRequired>
<Label>Username</Label>
<Input placeholder="Choose a username" />
</TextField>
검증과 함께
필드가 invalid일 때 오류 메시지를 표시할 수 있어요.
import { FieldError, Input, Label, TextField } from 'heroui-native';
<TextField isRequired isInvalid={true}>
<Label>Email</Label>
<Input placeholder="Enter your email" />
<FieldError>Please enter a valid email</FieldError>
</TextField>;
로컬 invalid 상태 오버라이드
개별 컴포넌트에 대해 컨텍스트의 invalid 상태를 오버라이드할 수 있어요.
import {
Description,
FieldError,
Input,
Label,
TextField,
} from 'heroui-native';
<TextField isInvalid={true}>
<Label isInvalid={false}>Email</Label>
<Input placeholder="Enter your email" isInvalid={false} />
<Description isInvalid={false}>
This shows despite input being invalid
</Description>
<FieldError>Email format is incorrect</FieldError>
</TextField>;
여러 줄 입력 (Multiline Input)
더 긴 콘텐츠를 위한 텍스트 영역을 만들 수 있어요.
<TextField>
<Label>Message</Label>
<Input placeholder="Type your message..." multiline numberOfLines={4} />
<Description>Maximum 500 characters</Description>
</TextField>
비활성 상태 (Disabled State)
상호작용을 막으려면 전체 필드를 비활성화할 수 있어요.
<TextField isDisabled>
<Label>Disabled Field</Label>
<Input placeholder="Cannot edit" value="Read only value" />
</TextField>
Variant 사용
컨텍스트에 따라 입력 스타일을 지정하는 다른 variant를 사용할 수 있어요.
<TextField>
<Label>Primary Variant</Label>
<Input placeholder="Primary style" variant="primary" />
</TextField>
<TextField>
<Label>Secondary Variant</Label>
<Input placeholder="Secondary style" variant="secondary" />
</TextField>
커스텀 스타일링
className을 사용해 입력 외형을 커스터마이즈할 수 있어요.
<TextField>
<Label>Custom Styled</Label>
<Input
placeholder="Custom colors"
className="bg-blue-50 border-blue-500 focus:border-blue-700"
/>
</TextField>
Example
import { Ionicons } from '@expo/vector-icons';
import { Description, Input, Label, TextField } from 'heroui-native';
import { useState } from 'react';
import { Pressable, View } from 'react-native';
import { withUniwind } from 'uniwind';
const StyledIonicons = withUniwind(Ionicons);
export const TextInputContent = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [isPasswordVisible, setIsPasswordVisible] = useState(false);
return (
<View className="gap-4">
<TextField isRequired>
<Label>Email</Label>
<Input
placeholder="Enter your email"
keyboardType="email-address"
autoCapitalize="none"
value={email}
onChangeText={setEmail}
/>
<Description>
We'll never share your email with anyone else.
</Description>
</TextField>
<TextField isRequired>
<Label>New password</Label>
<View className="w-full flex-row items-center">
<Input
value={password}
onChangeText={setPassword}
className="flex-1 px-10"
placeholder="Enter your password"
secureTextEntry={!isPasswordVisible}
/>
<StyledIonicons
name="lock-closed-outline"
size={16}
className="absolute left-3.5 text-muted"
pointerEvents="none"
/>
<Pressable
className="absolute right-4"
onPress={() => setIsPasswordVisible(!isPasswordVisible)}
>
<StyledIonicons
name={isPasswordVisible ? 'eye-off-outline' : 'eye-outline'}
size={16}
className="text-muted"
/>
</Pressable>
</View>
<Description>Password must be at least 6 characters</Description>
</TextField>
</View>
);
};
더 많은 예시는 GitHub 저장소에서 찾을 수 있어요.
API Reference
TextField
| prop | type | default | description |
|---|---|---|---|
| children | React.ReactNode |
- | 텍스트 필드 내부에 렌더링할 콘텐츠 |
| isDisabled | boolean |
false |
전체 텍스트 필드가 비활성화되었는지 여부 |
| isInvalid | boolean |
false |
텍스트 필드가 invalid 상태인지 여부 |
| isRequired | boolean |
false |
텍스트 필드가 필수인지(별표 표시) 여부 |
| className | string |
- | 루트 요소의 커스텀 클래스 이름 |
| animation | "disable-all" | undefined |
undefined |
애니메이션 구성. 자식을 포함한 모든 애니메이션을 비활성화하려면 "disable-all" 사용 |
| ...ViewProps | ViewProps |
- | 모든 표준 React Native View props 지원 |
Note: Label, Input, Description, FieldError 컴포넌트는 각각의 문서를 참고하세요:
이 컴포넌트들은 form-item-state 컨텍스트를 통해 TextField의 폼 상태를 자동으로 소비해요.
Hooks
useTextField
TextField 컨텍스트 값에 접근하기 위한 훅이에요. TextField 컴포넌트 내부에서 사용해야 해요.
import { TextField, useTextField } from 'heroui-native';
function CustomComponent() {
const { isDisabled, isInvalid, isRequired } = useTextField();
// Use the context values...
}
반환 값 (Returns)
| property | type | description |
|---|---|---|
| isDisabled | boolean |
전체 텍스트 필드가 비활성화되었는지 여부 |
| isInvalid | boolean |
텍스트 필드가 invalid 상태인지 여부 |
| isRequired | boolean |
텍스트 필드가 필수인지 여부 |