SearchField
검색 아이콘과 지우기 버튼이 있는 검색 입력 필드예요.
본문
사용법 (Usage)
import { SearchField } from '@heroui/react';
{"component": "search-field-basic"}
구조 (Anatomy)
import {SearchField, Label, Description, FieldError} from '@heroui/react';
export default () => (
<SearchField>
<Label />
<SearchField.Group>
<SearchField.SearchIcon />
<SearchField.Input />
<SearchField.ClearButton />
</SearchField.Group>
<Description />
<FieldError />
</SearchField>
)
SearchField은 사용자가 검색어를 입력하고 지울 수 있게 해줘요. 검색 아이콘과 쉽게 리셋할 수 있는 선택적 지우기 버튼을 포함해요.
예시 (Examples)
변형 (Variants)
SearchField 컴포넌트는 두 가지 시각적 변형을 지원해요:
primary(기본값) - 그림자가 있는 표준 스타일, 대부분의 사용 사례에 적합secondary- 그림자가 없는 낮은 강조 변형, Surface 컴포넌트에서 사용하기 적합
{"component": "search-field-variants"}
서피스 내에서 (In Surface)
Surface 컴포넌트 안에서 사용할 때는 variant="secondary" 를 사용해서 서피스 배경에 어울리는 낮은 강조 변형을 적용하세요.
{"component": "search-field-on-surface"}
설명 포함 (With Description)
{"component": "search-field-with-description"}
필수 필드 (Required Field)
{"component": "search-field-required"}
비활성화 상태 (Disabled State)
{"component": "search-field-disabled"}
전체 너비 (Full Width)
{"component": "search-field-full-width"}
검증 (Validation)
isInvalid 을 FieldError 와 함께 사용해서 검증 메시지를 표시하세요.
{"component": "search-field-validation"}
제어 (Controlled)
다른 컴포넌트와 동기화하거나 커스텀 형식을 적용하려면 값을 제어하세요.
{"component": "search-field-controlled"}
폼 예시 (Form Example)
검증과 제출 처리를 포함한 완전한 폼 통합 예시예요.
{"component": "search-field-form-example"}
검증 포함 (With Validation)
제어 값으로 커스텀 검증 로직을 구현하세요.
{"component": "search-field-with-validation"}
커스텀 아이콘 (Custom Icons)
검색 아이콘과 지우기 버튼 아이콘을 커스터마이징하세요.
{"component": "search-field-custom-icons"}
키보드 단축키 포함 (With Keyboard Shortcut)
검색 필드를 빠르게 포커스하는 키보드 단축키를 추가하세요.
{"component": "search-field-with-keyboard-shortcut"}
렌더 함수 (Render Function)
{"component": "search-field-render-function"}
커스터마이징 (Customization)
Tailwind CSS
{"component": "search-field-custom-styles"}
전역 CSS (Global CSS)
SearchField는 커스터마이징 가능한 CSS 클래스를 사용해요. 디자인 시스템에 맞게 컴포넌트 클래스를 재정의하세요.
@layer components {
.search-field {
@apply flex flex-col gap-1;
}
/* When invalid, the description is hidden automatically */
.search-field[data-invalid],
.search-field[aria-invalid] {
[data-slot="description"] {
@apply hidden;
}
}
.search-field__group {
@apply bg-field text-field-foreground shadow-field rounded-field inline-flex h-9 items-center overflow-hidden border;
}
.search-field__input {
@apply flex-1 rounded-none border-0 bg-transparent px-3 py-2 shadow-none outline-none;
}
.search-field__search-icon {
@apply text-field-placeholder pointer-events-none shrink-0 ml-3 mr-0 size-4;
}
.search-field__clear-button {
@apply mr-1 shrink-0;
}
}
스타일링 참고 (Styling Reference)
HeroUI는 BEM 방법론을 따라 컴포넌트 변형과 상태를 재사용하고 커스터마이징하기 쉽게 만들어요.
CSS 클래스 (CSS Classes)
기본 클래스 [!toc]
.search-field– 최소한의 스타일을 가진 루트 컨테이너 (flex flex-col gap-1).search-field__group– 테두리와 배경 스타일로 검색 아이콘, 입력, 지우기 버튼을 담는 컨테이너.search-field__input– 검색 입력 필드.search-field__search-icon– 왼쪽에 표시되는 검색 아이콘.search-field__clear-button– 검색 필드를 지우는 버튼
변형 클래스 [!toc]
.search-field--primary– 그림자가 있는 기본 변형.search-field--secondary– 그림자가 없는 보조 변형, 서피스에서 사용하기 적합
참고: 자식 컴포넌트(Label, Description, FieldError)는 고유한 CSS 클래스와 스타일을 가져요. 커스터마이징 옵션은 각각의 문서를 참고하세요.
인터랙티브 상태 (Interactive States)
SearchField는 상태에 따라 다음 데이터 속성을 자동으로 관리해요:
- 유효하지 않음:
[data-invalid="true"]또는[aria-invalid="true"]- 유효하지 않을 때 설명 슬롯을 자동으로 숨겨요 - 비활성화:
[data-disabled="true"]-isDisabled가 true일 때 적용돼요 - 포커스 내부:
[data-focus-within="true"]- 입력에 포커스가 있을 때 적용돼요 - 포커스 표시:
[data-focus-visible="true"]- 포커스가 보일 때(키보드 탐색) 적용돼요 - 호버:
[data-hovered="true"]- 그룹 위에 호버할 때 적용돼요 - 빈 상태:
[data-empty="true"]- 필드가 비어 있을 때 적용돼요 (지우기 버튼 숨김)
추가 속성은 렌더 프롭스를 통해 사용할 수 있어요 (아래 SearchFieldRenderProps 참고).
API 참고 (API Reference)
SearchField
SearchField는 React Aria의 SearchField 컴포넌트에서 모든 프롭을 상속받아요.
기본 프롭 (Base Props)
| Prop | Type | Default | Description |
|---|---|---|---|
children |
React.ReactNode | (values: SearchFieldRenderProps) => React.ReactNode |
- | Child components (Label, Group, Input, etc.) or render function. |
className |
string | (values: SearchFieldRenderProps) => string |
- | CSS classes for styling, supports render props. |
style |
React.CSSProperties | (values: SearchFieldRenderProps) => React.CSSProperties |
- | Inline styles, supports render props. |
fullWidth |
boolean |
false |
Whether the search field should take full width of its container |
id |
string |
- | The element's unique identifier. |
variant |
"primary" | "secondary" |
"primary" |
Visual variant of the component. primary is the default style with shadow. secondary is a lower emphasis variant without shadow, suitable for use in surfaces. |
render |
DOMRenderFunction<keyof React.JSX.IntrinsicElements, SearchFieldRenderProps> |
- | Overrides the default DOM element with a custom render function. |
값 프롭 (Value Props)
| Prop | Type | Default | Description |
|---|---|---|---|
value |
string |
- | Current value (controlled). |
defaultValue |
string |
- | Default value (uncontrolled). |
onChange |
(value: string) => void |
- | Handler called when the value changes. |
검증 프롭 (Validation Props)
| Prop | Type | Default | Description |
|---|---|---|---|
isRequired |
boolean |
false |
Whether user input is required before form submission. |
isInvalid |
boolean |
- | Whether the value is invalid. |
validate |
(value: string) => ValidationError | true | null | undefined |
- | Custom validation function. |
validationBehavior |
'native' | 'aria' |
'native' |
Whether to use native HTML form validation or ARIA attributes. |
validationErrors |
string[] |
- | Server-side validation errors. |
상태 프롭 (State Props)
| Prop | Type | Default | Description |
|---|---|---|---|
isDisabled |
boolean |
- | Whether the input is disabled. |
isReadOnly |
boolean |
- | Whether the input can be selected but not changed. |
폼 프롭 (Form Props)
| Prop | Type | Default | Description |
|---|---|---|---|
name |
string |
- | Name of the input element, for HTML form submission. |
autoFocus |
boolean |
- | Whether the element should receive focus on render. |
이벤트 프롭 (Event Props)
| Prop | Type | Default | Description |
|---|---|---|---|
onSubmit |
(value: string) => void |
- | Handler called when the user submits the search (Enter key). |
onClear |
() => void |
- | Handler called when the clear button is pressed. |
접근성 프롭 (Accessibility Props)
| Prop | Type | Default | Description |
|---|---|---|---|
aria-label |
string |
- | Accessibility label when no visible label is present. |
aria-labelledby |
string |
- | ID of elements that label this field. |
aria-describedby |
string |
- | ID of elements that describe this field. |
aria-details |
string |
- | ID of elements with additional details. |
구성 컴포넌트 (Composition Components)
SearchField은 다음 분리된 컴포넌트들과 함께 동작하며, 이들을 직접 임포트해서 사용해야 해요:
- SearchField.Group - 검색 아이콘, 입력, 지우기 버튼을 담는 컨테이너
- SearchField.Input - 검색 입력 필드
- SearchField.SearchIcon - 왼쪽에 표시되는 검색 아이콘
- SearchField.ClearButton - 검색 필드를 지우는 버튼
- Label -
@heroui/react의 필드 라벨 컴포넌트 - Description -
@heroui/react의 도움말 텍스트 컴포넌트 - FieldError -
@heroui/react의 검증 오류 메시지
이 컴포넌트 각각은 고유한 프롭 API를 가져요. 합성을 위해 SearchField 안에서 직접 사용하세요:
<SearchField isRequired isInvalid={hasError} value={value} onChange={setValue}>
<Label>Search</Label>
<SearchField.Group>
<SearchField.SearchIcon />
<SearchField.Input placeholder="Search..." />
<SearchField.ClearButton />
</SearchField.Group>
<Description>Enter keywords to search</Description>
<FieldError>Search query is required</FieldError>
</SearchField>
SearchField.Group 프롭 (Props)
SearchField.Group은 React Aria의 Group 컴포넌트에서 프롭을 상속받아요.
| Prop | Type | Default | Description |
|---|---|---|---|
children |
React.ReactNode | (values: GroupRenderProps) => React.ReactNode |
- | Child components (SearchIcon, Input, ClearButton) or render function. |
className |
string | (values: GroupRenderProps) => string |
- | CSS classes for styling. |
SearchField.Input 프롭 (Props)
SearchField.Input은 React Aria의 Input 컴포넌트에서 프롭을 상속받아요.
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
- | CSS classes for styling. |
variant |
"primary" | "secondary" |
"primary" |
Visual variant of the input. primary is the default style with shadow. secondary is a lower emphasis variant without shadow, suitable for use in surfaces. |
placeholder |
string |
- | Placeholder text displayed when the input is empty. |
type |
string |
"search" |
Input type (automatically set to "search"). |
SearchField.SearchIcon 프롭 (Props)
SearchField.SearchIcon은 검색 아이콘을 렌더링하는 커스텀 컴포넌트예요.
| Prop | Type | Default | Description |
|---|---|---|---|
children |
React.ReactNode |
<IconSearch /> |
Custom icon element. Defaults to search icon. |
className |
string |
- | CSS classes for styling. |
SearchField.ClearButton 프롭 (Props)
SearchField.ClearButton은 React Aria의 Button 컴포넌트에서 프롭을 상속받아요.
| Prop | Type | Default | Description |
|---|---|---|---|
children |
React.ReactNode |
<CloseButton icon /> |
Icon or content for the button. Defaults to close icon. |
className |
string |
- | CSS classes for styling. |
slot |
"clear" |
"clear" |
Must be set to "clear" (automatically set). |
렌더 프롭스 (Render Props)
className, style, children과 함께 렌더 프롭스를 사용하면 다음 값이 제공돼요:
| Prop | Type | Description |
|---|---|---|
isDisabled |
boolean |
Whether the field is disabled. |
isInvalid |
boolean |
Whether the field is currently invalid. |
isReadOnly |
boolean |
Whether the field is read-only. |
isRequired |
boolean |
Whether the field is required. |
isFocused |
boolean |
Whether the field is currently focused (DEPRECATED - use isFocusWithin). |
isFocusWithin |
boolean |
Whether any child element is focused. |
isFocusVisible |
boolean |
Whether focus is visible (keyboard navigation). |
value |
string |
Current value. |
isEmpty |
boolean |
Whether the field is empty. |