본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

FieldError (필드 오류)

원문 보기 위키 갱신

FieldError는 검증 실패 시 오류 메시지를 부드러운 애니메이션과 함께 표시해 주는 컴포넌트예요. 폼 필드 안에서 isInvalid 프롭으로 표시 여부를 제어하며, 필요하면 커스텀 콘텐츠나 커스텀 애니메이션도 지정할 수 있어요.

출처: 문서

본문

가져오기 (Import)

import { FieldError } from 'heroui-native';

구조 (Anatomy)

<FieldError>Error message content</FieldError>
  • FieldError: 부드러운 애니메이션과 함께 오류 메시지를 표시하는 메인 컨테이너. 문자열 자식을 받으면 자동으로 Text 컴포넌트로 감싸고, 더 복잡한 레이아웃이 필요하면 커스텀 React 컴포넌트도 받을 수 있어요. isInvalid 프롭으로 표시 여부를 제어하고 커스텀 진입/퇴장 애니메이션을 지원해요.

사용법 (Usage)

기본 사용법 (Basic Usage)

FieldError 컴포넌트는 검증에 실패했을 때 오류 메시지를 표시해요.

<FieldError isInvalid={true}>This field is required</FieldError>

표시 여부 제어 (Controlled Visibility)

isInvalid 프롭을 사용해 오류가 언제 나타날지 제어해요. TextField 같은 폼 필드 컴포넌트 안에서 사용하면 FieldError가 form-item-state 컨텍스트를 자동으로 소비해요.

const [isInvalid, setIsInvalid] = useState(false);

<FieldError isInvalid={isInvalid}>Please enter a valid email address</FieldError>;

폼 필드와 함께 사용하기 (With Form Fields)

FieldError는 form-item-state 컨텍스트를 통해 TextField에서 폼 상태를 자동으로 소비해요.

import { FieldError, Label, TextField } from 'heroui-native';

<TextField isRequired isInvalid={true}>
  <Label>Email</Label>
  <Input placeholder="Enter your email" />
  <FieldError>Please enter a valid email address</FieldError>
</TextField>

커스텀 콘텐츠 (Custom Content)

문자열 대신 커스텀 React 컴포넌트를 자식으로 전달해요.

<FieldError isInvalid={true}>
  <View className="flex-row items-center">
    <Icon name="alert-circle" />
    <Text className="ml-2 text-danger">Invalid input</Text>
  </View>
</FieldError>

커스텀 애니메이션 (Custom Animations)

animation 프롭으로 기본 진입/퇴장 애니메이션을 재정의해요.

import { SlideInDown, SlideOutUp } from 'react-native-reanimated';

<FieldError
  isInvalid={true}
  animation={{
    entering: { value: SlideInDown.duration(200) },
    exiting: { value: SlideOutUp.duration(150) },
  }}
>
  Field validation failed
</FieldError>;

애니메이션을 완전히 비활성화하려면:

<FieldError isInvalid={true} animation={false}>
  Field validation failed
</FieldError>

커스텀 스타일링 (Custom Styling)

컨테이너와 텍스트 요소에 커스텀 스타일을 적용해요.

<FieldError
  isInvalid={true}
  className="mt-2"
  classNames={{
    container: 'bg-danger/10 p-2 rounded',
    text: 'text-xs font-medium',
  }}
>
  Password must be at least 8 characters
</FieldError>

커스텀 Text 프롭 (Custom Text Props)

children이 문자열일 때 Text 컴포넌트에 추가 프롭을 전달해요.

<FieldError
  isInvalid={true}
  textProps={{
    numberOfLines: 1,
    ellipsizeMode: 'tail',
    style: { letterSpacing: 0.5 },
  }}
>
  This is a very long error message that might need to be truncated
</FieldError>

예제 (Example)

import { Description, FieldError, Label, TextField } from 'heroui-native';
import { useState } from 'react';
import { View } from 'react-native';

export default function FieldErrorExample() {
  const [email, setEmail] = useState('');
  const [isInvalid, setIsInvalid] = useState(false);

  const isValidEmail = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);

  const handleBlur = () => {
    setIsInvalid(email !== '' && !isValidEmail);
  };

  return (
    <View className="p-4">
      <TextField isInvalid={isInvalid}>
        <Label>Email Address</Label>
        <Input
          placeholder="Enter your email"
          value={email}
          onChangeText={setEmail}
          onBlur={handleBlur}
          keyboardType="email-address"
          autoCapitalize="none"
        />
        <Description>
          We'll use this to contact you
        </Description>
        <FieldError>Please enter a valid email address</FieldError>
      </TextField>
    </View>
  );
}

더 많은 예제는 GitHub 저장소에서 찾을 수 있어요.

API 참고 (API Reference)

FieldError

prop type default description
children React.ReactNode undefined The content of the error field. String children are wrapped with Text
isInvalid boolean undefined Controls the visibility of the error field (overrides form-item-state context). When used inside TextField, automatically consumes form state
animation FieldErrorRootAnimation - Animation configuration
className string undefined Additional CSS classes for the container
classNames ElementSlots<FieldErrorSlots> undefined Additional CSS classes for different parts of the component
styles { container?: ViewStyle; text?: TextStyle } undefined Styles for different parts of the field error
textProps TextProps undefined Additional props to pass to the Text component when children is a string
...AnimatedViewProps AnimatedProps<ViewProps> - All Reanimated Animated.View props are supported

classNames prop: ElementSlots<FieldErrorSlots>는 필드 오류 컴포넌트의 각 부분에 타입 안전한 CSS 클래스를 제공해요. 사용 가능한 슬롯: container, text.

styles

prop type description
container ViewStyle Styles for the container
text TextStyle Styles for the text content

FieldErrorRootAnimation

필드 오류 루트 컴포넌트의 애니메이션 설정이에요. 값은 다음과 같아요:

  • false 또는 "disabled": 루트 애니메이션만 비활성화
  • "disable-all": 자식을 포함한 모든 애니메이션 비활성화
  • true 또는 undefined: 기본 애니메이션 사용
  • object: 커스텀 애니메이션 설정
prop type default description
state 'disabled' | 'disable-all' | boolean - Disable animations while customizing properties
entering.value EntryOrExitLayoutType FadeIn
.duration(150)
.easing(Easing.out(Easing.ease))
Custom entering animation for field error
exiting.value EntryOrExitLayoutType FadeOut
.duration(100)
.easing(Easing.out(Easing.ease))
Custom exiting animation for field error

더 알아보기 (Learn more)

  • TextField와 함께 사용하는 폼 검증 패턴을 확인해 보세요.
  • 공식 HeroUI Native FieldError 문서에서 최신 정보를 확인할 수 있어요.