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

SearchField (검색 필드)

원문 보기 위키 갱신

SearchField는 콘텐츠를 필터링하고 검색하기 위한 복합(compound) 검색 입력 컴포넌트예요. 검색 아이콘, 입력, 지우기 버튼을 조합하는 방식으로 구성되어 있어 검색 전용으로 특화된 동작을 제공해요.

출처: 문서

본문

가져오기 (Import)

import { SearchField } from 'heroui-native';

구조 (Anatomy)

<SearchField value={value} onChange={onChange}>
  <SearchField.Group>
    <SearchField.SearchIcon />
    <SearchField.Input />
    <SearchField.ClearButton />
  </SearchField.Group>
</SearchField>
  • SearchField: value와 onChange를 받아 자식에게 컨텍스트로 전달하는 루트 컨테이너. 폼 필드 상태(isDisabled, isInvalid, isRequired)와 애니메이션 설정도 제공해요.
  • SearchField.Group: 검색 아이콘, 입력, 지우기 버튼을 가로로 배치하는 Flex-row 컨테이너.
  • SearchField.SearchIcon: (선택) 입력 왼쪽에 절대 위치로 배치되는 돋보기 아이콘. 커스텀 자식을 전달해 기본 아이콘을 대체할 수 있어요. 생략하면 Input이 앞쪽 공간을 예약하지 않아요.
  • SearchField.Input: 검색 전용 기본값이 적용된 Input 컴포넌트 래퍼. SearchField 컨텍스트에서 value와 onChangeText를 자동으로 읽어요. SearchIcon과 ClearButton이 조합될 때만 앞뒤 공간을 예약해요.
  • SearchField.ClearButton: (선택) 검색 입력을 지우는 작은 아이콘 전용 버튼. 값이 비어 있으면 자동으로 숨겨져요. 누르면 컨텍스트의 onChange("")를 호출해요. 생략하면 Input이 뒷쪽 공간을 예약하지 않고, 조합됐지만 숨겨진 상태(값이 비었을 때)에는 첫 글자를 입력해도 텍스트가 튀지 않도록 뒷쪽 패딩이 유지돼요.

사용법 (Usage)

기본 사용법 (Basic Usage)

SearchField 컴포넌트는 복합 파트로 검색 입력을 만들어요. 루트에 value와 onChange를 전달하면 Input과 ClearButton이 컨텍스트를 통해 이를 소비해요.

<SearchField value={searchValue} onChange={setSearchValue}>
  <SearchField.Group>
    <SearchField.SearchIcon />
    <SearchField.Input />
    <SearchField.ClearButton />
  </SearchField.Group>
</SearchField>

검색 아이콘 없이 (Without Search Icon)

SearchField.SearchIcon을 생략하면 앞쪽 돋보기와 예약 패딩이 사라져요. Input이 그 공간을 자동으로 되찾아요.

<SearchField value={searchValue} onChange={setSearchValue}>
  <SearchField.Group>
    <SearchField.Input />
    <SearchField.ClearButton />
  </SearchField.Group>
</SearchField>

ClearButton도 함께 생략하면 뒷쪽 공간도 사라져요. ClearButton이 조합됐지만 숨겨진(값이 비어 있는) 경우에는 뒷쪽 패딩이 유지되어 첫 글자를 입력할 때 텍스트가 튀지 않아요.

검색 필드에 맥락을 제공하려면 Group 밖에 Label과 Description을 추가해요.

<SearchField value={searchValue} onChange={setSearchValue}>
  <Label>Find products</Label>
  <SearchField.Group>
    <SearchField.SearchIcon />
    <SearchField.Input />
    <SearchField.ClearButton />
  </SearchField.Group>
  <Description>Search by name, category, or SKU</Description>
</SearchField>

검증과 함께 (With Validation)

루트에 isInvalid와 isRequired를 사용해 검증 상태를 제어해요. FieldError와 함께 사용해 오류 메시지를 표시해요.

<SearchField
  value={searchValue}
  onChange={setSearchValue}
  isRequired
  isInvalid={isInvalid}
>
  <Label>Search users</Label>
  <SearchField.Group>
    <SearchField.SearchIcon />
    <SearchField.Input />
    <SearchField.ClearButton />
  </SearchField.Group>
  <Description hideOnInvalid>Enter at least 3 characters to search</Description>
  <FieldError>No results found. Please try a different search term.</FieldError>
</SearchField>

커스텀 검색 아이콘 (Custom Search Icon)

SearchField.SearchIcon에 자식을 전달해 기본 돋보기 아이콘을 대체해요.

<SearchField value={searchValue} onChange={setSearchValue}>
  <SearchField.Group>
    <SearchField.SearchIcon>
      <Text className="text-base">🔍</Text>
    </SearchField.SearchIcon>
    <SearchField.Input className="pl-10" />
    <SearchField.ClearButton />
  </SearchField.Group>
</SearchField>

비활성화 (Disabled)

루트에 isDisabled를 설정하면 컨텍스트를 통해 모든 자식 컴포넌트가 비활성화돼요.

<SearchField value="Previous query" isDisabled>
  <Label>Disabled search</Label>
  <SearchField.Group>
    <SearchField.SearchIcon />
    <SearchField.Input />
  </SearchField.Group>
  <Description>Search is temporarily unavailable</Description>
</SearchField>

예제 (Example)

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

export default function SearchFieldExample() {
  const [searchValue, setSearchValue] = useState('');

  return (
    <View className="px-5">
      <SearchField value={searchValue} onChange={setSearchValue}>
        <Label>Find products</Label>
        <SearchField.Group>
          <SearchField.SearchIcon />
          <SearchField.Input />
          <SearchField.ClearButton />
        </SearchField.Group>
        <Description>Search by name, category, or SKU</Description>
      </SearchField>
    </View>
  );
}

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

API 참고 (API Reference)

SearchField

prop type default description
children React.ReactNode - Children elements to be rendered inside the search field
value string - Controlled search text value
onChange (value: string) => void - Callback fired when the search text changes
isDisabled boolean false Whether the search field is disabled
isInvalid boolean false Whether the search field is in an invalid state
isRequired boolean false Whether the search field is required
className string - Additional CSS classes
animation AnimationRootDisableAll - Animation configuration for the search field
...ViewProps ViewProps - All standard React Native View props are supported

AnimationRootDisableAll

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

  • "disable-all": 자식을 포함한 모든 애니메이션 비활성화 (하위로 전파됨)
  • undefined: 기본 애니메이션 사용

SearchField.Group

prop type default description
children React.ReactNode - Children elements to be rendered inside the group
className string - Additional CSS classes
...ViewProps ViewProps - All standard React Native View props are supported

SearchField.SearchIcon

prop type default description
children React.ReactNode - Custom content to replace the default search icon
className string - Additional CSS classes
iconProps SearchFieldSearchIconIconProps - Props for customizing the default search icon (ignored when children are provided)
...ViewProps ViewProps - All standard React Native View props are supported

SearchFieldSearchIconIconProps

prop type default description
size number 16 Size of the icon
color string Theme muted color Color of the icon

SearchField.Input

Input 프롭에 검색 전용 기본값(placeholder="Search...", returnKeyType="search", accessibilityRole="search")을 더해 확장한 것이에요. value와 onChangeText는 SearchField 컨텍스트에서 제공되므로 생략돼요.

SearchField.ClearButton

제어되는 value가 빈 문자열이면 자동으로 숨겨져요. 누르면 컨텍스트의 onChange("")를 호출해요. 프롭으로 전달한 추가 onPress 핸들러는 지운 뒤에 호출돼요.

prop type default description
children React.ReactNode - Custom content to replace the default close icon
iconProps SearchFieldClearButtonIconProps - Props for customizing the clear button icon
className string - Additional CSS classes
...ButtonProps ButtonRootProps - All Button root props are supported

SearchFieldClearButtonIconProps

prop type default description
size number 14 Size of the icon
color string Theme muted color Color of the icon

훅 (Hooks)

useSearchField

컨텍스트에서 검색 필드 상태에 접근하는 훅이에요. SearchField 컴포넌트 안에서 사용해야 해요.

import { useSearchField } from 'heroui-native';

const { value, onChange, isDisabled, isInvalid, isRequired } = useSearchField();

반환값 (Returns)

property type description
value string | undefined Current controlled search text value
onChange ((value: string) => void) | undefined Callback to update the search text
isDisabled boolean Whether the search field is disabled
isInvalid boolean Whether the search field is in an invalid state
isRequired boolean Whether the search field is required

더 알아보기 (Learn more)

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