숫자 필드 (NumberField)
NumberField는 증가/감소 버튼, 검증, 국제화된 포맷팅을 갖춘 숫자 입력 필드예요.
본문
사용 방법 (Usage)
import { NumberField } from '@heroui/react';
{
"preview": "number-field-basic"
}
구조 (Anatomy)
import {NumberField, Label, Description, FieldError} from '@heroui/react';
export default () => (
<NumberField>
<Label />
<NumberField.Group>
<NumberField.DecrementButton />
<NumberField.Input />
<NumberField.IncrementButton />
</NumberField.Group>
<Description />
<FieldError />
</NumberField>
)
NumberField는 사용자가 선택적으로 증가/감소 버튼과 함께 숫자 값을 입력할 수 있게 해줘요. 국제화된 포맷팅, 검증, 키보드 내비게이션을 지원해요.
예제 (Examples)
변형 (Variants)
NumberField 컴포넌트는 두 가지 시각적 변형을 지원해요:
primary(기본값) - 그림자가 있는 표준 스타일로, 대부분의 사용 사례에 적합해요secondary- 그림자가 없는 낮은 강조 변형으로, Surface 컴포넌트에 적합해요
{
"preview": "number-field-variants"
}
Surface에서 사용 (In Surface)
Surface 컴포넌트 안에서 사용할 때는 variant="secondary"를 사용해 표면 배경에 적합한 낮은 강조 변형을 적용해요.
{
"preview": "number-field-on-surface"
}
설명 포함 (With Description)
{
"preview": "number-field-with-description"
}
필수 필드 (Required Field)
{
"preview": "number-field-required"
}
비활성 상태 (Disabled State)
{
"preview": "number-field-disabled"
}
전체 너비 (Full Width)
{
"preview": "number-field-full-width"
}
검증 (Validation)
isInvalid를 FieldError와 함께 사용해 검증 메시지를 표시할 수 있어요.
{
"preview": "number-field-validation"
}
제어 (Controlled)
값을 제어해 다른 컴포넌트와 동기화하거나 사용자 지정 포맷팅을 수행할 수 있어요.
{
"preview": "number-field-controlled"
}
스텝 값 (Step Values)
정밀한 제어를 위해 증가/감소 스텝 값을 설정할 수 있어요.
{
"preview": "number-field-with-step"
}
포맷 옵션 (Format Options)
국제화 지원으로 숫자를 통화, 백분율, 소수, 단위로 포맷팅할 수 있어요.
{
"preview": "number-field-with-format-options"
}
폼 예제 (Form Example)
검증과 제출 처리가 포함된 완전한 폼 통합이에요.
{
"preview": "number-field-form-example"
}
사용자 지정 검증 포함 (With Validation)
제어된 값으로 사용자 지정 검증 로직을 구현할 수 있어요.
{
"preview": "number-field-with-validation"
}
사용자 지정 아이콘 (Custom Icons)
증가/감소 버튼 아이콘을 커스터마이징할 수 있어요.
{
"preview": "number-field-custom-icons"
}
셰브론 사용 (With Chevrons)
세로 레이아웃에서 셰브론 아이콘을 사용해 다른 시각적 스타일을 만들 수 있어요.
{
"preview": "number-field-with-chevrons"
}
렌더 함수 (Render Function)
{
"preview": "number-field-render-function"
}
커스터마이징 (Customization)
Tailwind CSS
{
"preview": "number-field-custom-styles"
}
글로벌 CSS (Global CSS)
NumberField는 커스터마이징할 수 있는 CSS 클래스를 사용해요. 디자인 시스템에 맞게 컴포넌트 클래스를 재정의할 수 있어요.
@layer components {
.number-field {
@apply flex flex-col gap-1;
}
/* When invalid, the description is hidden automatically */
.number-field[data-invalid="true"] [data-slot="description"],
.number-field[aria-invalid="true"] [data-slot="description"] {
@apply hidden;
}
.number-field__group {
@apply bg-field text-field-foreground shadow-field rounded-field inline-flex h-9 items-center overflow-hidden border;
}
.number-field__input {
@apply flex-1 rounded-none border-0 bg-transparent px-3 py-2 tabular-nums;
}
.number-field__increment-button,
.number-field__decrement-button {
@apply flex h-full w-10 items-center justify-center rounded-none bg-transparent;
}
}
스타일 참조 (Styling Reference)
HeroUI는 컴포넌트 변형과 상태를 재사용하고 커스터마이징하기 쉽도록 BEM 방법론을 따르고 있어요.
CSS 클래스 (CSS Classes)
기본 클래스 (Base Classes)
.number-field– 최소한의 스타일(flex flex-col gap-1)이 적용된 루트 컨테이너.number-field__group– 테두리와 배경 스타일이 적용된 입력·버튼용 컨테이너.number-field__input– 숫자 입력 필드.number-field__increment-button– 값을 증가시키는 버튼.number-field__decrement-button– 값을 감소시키는 버튼
변형 클래스 (Variant Classes)
.number-field--primary– 그림자가 있는 기본 변형 (기본값).number-field--secondary– 그림자가 없는 보조 변형, 표면에서 사용하기 적합해요
참고: 하위 컴포넌트 (Label, Description, FieldError)는 각각 고유한 CSS 클래스와 스타일을 가져요. 커스터마이징 옵션은 각 문서를 참고하세요.
인터랙티브 상태 (Interactive States)
NumberField는 상태에 따라 다음 데이터 속성을 자동으로 관리해요:
- 비정상 (Invalid):
[data-invalid="true"]또는[aria-invalid="true"]- 비정상일 때 설명 슬롯을 자동으로 숨겨요 - 비활성 (Disabled):
[data-disabled="true"]-isDisabled가 true일 때 적용돼요 - 포커스 포함 (Focus Within):
[data-focus-within="true"]- 입력 또는 버튼에 포커스가 있을 때 적용돼요 - 포커스 표시 (Focus Visible):
[data-focus-visible="true"]- 포커스가 보일 때(키보드 내비게이션) 적용돼요 - 호버 (Hovered):
[data-hovered="true"]- 버튼 위에 마우스를 올려놓을 때 적용돼요
추가 속성은 렌더 props를 통해 사용할 수 있어요 (아래 NumberFieldRenderProps 참고).
API 참조 (API Reference)
NumberField
NumberField는 React Aria의 NumberField 컴포넌트의 모든 props를 상속해요.
기본 props (Base Props)
| Prop | Type | Default | Description |
|---|---|---|---|
children |
React.ReactNode | (values: NumberFieldRenderProps) => React.ReactNode |
- | Child components (Label, Group, Input, etc.) or render function. |
className |
string | (values: NumberFieldRenderProps) => string |
- | CSS classes for styling, supports render props. |
style |
React.CSSProperties | (values: NumberFieldRenderProps) => React.CSSProperties |
- | Inline styles, supports render props. |
fullWidth |
boolean |
false |
Whether the number 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, NumberFieldRenderProps> |
- | Overrides the default DOM element with a custom render function. |
값 props (Value Props)
| Prop | Type | Default | Description |
|---|---|---|---|
value |
number |
- | Current value (controlled). |
defaultValue |
number |
- | Default value (uncontrolled). |
onChange |
(value: number | undefined) => void |
- | Handler called when the value changes. |
포맷팅 props (Formatting Props)
| Prop | Type | Default | Description |
|---|---|---|---|
formatOptions |
Intl.NumberFormatOptions |
- | Options for formatting numbers (currency, percent, decimal, unit). |
locale |
string |
- | Locale for number formatting. |
검증 props (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: number) => 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. |
범위 props (Range Props)
| Prop | Type | Default | Description |
|---|---|---|---|
minValue |
number |
- | Minimum allowed value. |
maxValue |
number |
- | Maximum allowed value. |
step |
number |
1 |
Step value for increment/decrement operations. |
상태 props (State Props)
| Prop | Type | Default | Description |
|---|---|---|---|
isDisabled |
boolean |
- | Whether the input is disabled. |
isReadOnly |
boolean |
- | Whether the input can be selected but not changed. |
폼 props (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. |
접근성 props (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)
NumberField는 다음과 같은 별도 컴포넌트와 함께 동작하며, 이들은 직접 임포트해서 사용해야 해요:
- NumberField.Group - 입력과 버튼용 컨테이너
- NumberField.Input - 숫자 입력 필드
- NumberField.IncrementButton - 값을 증가시키는 버튼
- NumberField.DecrementButton - 값을 감소시키는 버튼
- Label -
@heroui/react의 필드 라벨 컴포넌트 - Description -
@heroui/react의 도움말 텍스트 컴포넌트 - FieldError -
@heroui/react의 검증 오류 메시지
이 각각의 컴포넌트는 고유한 props API를 가져요. NumberField 안에서 직접 사용해 구성할 수 있어요:
<NumberField isRequired isInvalid={hasError} minValue={0} maxValue={100}>
<Label>Quantity</Label>
<NumberField.Group>
<NumberField.DecrementButton />
<NumberField.Input />
<NumberField.IncrementButton />
</NumberField.Group>
<Description>Enter a value between 0 and 100</Description>
<FieldError>Value must be between 0 and 100</FieldError>
</NumberField>
NumberField.Group Props
NumberField.Group는 React Aria의 Group 컴포넌트의 props를 상속해요.
| Prop | Type | Default | Description |
|---|---|---|---|
children |
React.ReactNode | (values: GroupRenderProps) => React.ReactNode |
- | Child components (Input, Buttons) or render function. |
className |
string | (values: GroupRenderProps) => string |
- | CSS classes for styling. |
NumberField.Input Props
NumberField.Input는 React Aria의 Input 컴포넌트의 props를 상속해요.
| 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. |
NumberField.IncrementButton Props
NumberField.IncrementButton는 React Aria의 Button 컴포넌트의 props를 상속해요.
| Prop | Type | Default | Description |
|---|---|---|---|
children |
React.ReactNode |
<IconPlus /> |
Icon or content for the button. Defaults to plus icon. |
className |
string |
- | CSS classes for styling. |
slot |
"increment" |
"increment" |
Must be set to "increment" (automatically set). |
NumberField.DecrementButton Props
NumberField.DecrementButton는 React Aria의 Button 컴포넌트의 props를 상속해요.
| Prop | Type | Default | Description |
|---|---|---|---|
children |
React.ReactNode |
<IconMinus /> |
Icon or content for the button. Defaults to minus icon. |
className |
string |
- | CSS classes for styling. |
slot |
"decrement" |
"decrement" |
Must be set to "decrement" (automatically set). |
렌더 props (Render Props)
className, style, children에 렌더 props를 사용할 때 다음 값이 제공돼요:
| 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 |
number | undefined |
Current value. |
minValue |
number | undefined |
Minimum allowed value. |
maxValue |
number | undefined |
Maximum allowed value. |
step |
number |
Step value for increment/decrement. |
더 알아보기 (Learn more)
HeroUI NumberField에 대해 더 알아보려면 공식 문서를 확인해 주세요.