숫자 입력
숫자 입력 (InputNumber)
커스터마이즈 가능한 범위를 가진 숫자 값을 입력하는 컴포넌트예요. 최소/최대값, 단계(step), 소수점·통화·백분율 형식, 방향 전환 등을 지원합니다.
출처: Nuxt UI
본문
커스터마이즈 가능한 범위를 가진 숫자 값을 입력하는 입력 요소예요.
사용 방법 (Usage)
v-model 디렉티브를 사용하면 InputNumber의 값을 제어할 수 있어요.
<script setup lang="ts">
const value = ref(5)
</script>
<template>
<UInputNumber v-model="value" />
</template>
상태를 제어할 필요가 없을 때는 default-value prop으로 초기값을 설정할 수 있어요.
<template>
<UInputNumber :default-value="5" />
</template>
[!NOTE]
이 컴포넌트는 로케일과 숫자 체계에 걸쳐 숫자를 포맷하고 파싱하는 유틸리티를 제공하는
@internationalized/number패키지에 의존해요.
최소 / 최대 (Min / Max)
min과 max prop을 사용하면 InputNumber의 최소 및 최대값을 설정할 수 있어요.
<script setup lang="ts">
const value = ref(5)
</script>
<template>
<UInputNumber v-model="value" :min="0" :max="10" />
</template>
단계 (Step)
step prop을 사용하면 InputNumber의 단계 값을 설정할 수 있어요.
<script setup lang="ts">
const value = ref(5)
</script>
<template>
<UInputNumber v-model="value" :step="2" />
</template>
방향 (Orientation)
orientation prop을 사용하면 InputNumber의 방향을 바꿀 수 있어요.
<script setup lang="ts">
const value = ref(5)
</script>
<template>
<UInputNumber v-model="value" orientation="vertical" />
</template>
플레이스홀더 (Placeholder)
placeholder prop을 사용하면 플레이스홀더 텍스트를 설정할 수 있어요.
<template>
<UInputNumber placeholder="Enter a number" />
</template>
색상 (Color)
color prop을 사용하면 InputNumber가 포커스될 때의 링 색상을 바꿀 수 있어요.
<script setup lang="ts">
const value = ref(5)
</script>
<template>
<UInputNumber v-model="value" color="neutral" highlight />
</template>
변형 (Variant)
variant prop을 사용하면 InputNumber의 변형을 바꿀 수 있어요.
<script setup lang="ts">
const value = ref(5)
</script>
<template>
<UInputNumber v-model="value" variant="subtle" color="neutral" :highlight="false" />
</template>
사이즈 (Size)
size prop을 사용하면 InputNumber의 크기를 바꿀 수 있어요.
<script setup lang="ts">
const value = ref(5)
</script>
<template>
<UInputNumber v-model="value" size="xl" />
</template>
비활성화 (Disabled)
disabled prop을 사용하면 InputNumber를 비활성화할 수 있어요.
<script setup lang="ts">
const value = ref(5)
</script>
<template>
<UInputNumber v-model="value" disabled />
</template>
증가 / 감소 (Increment / Decrement)
increment과 decrement prop을 사용하면 증가/감소 버튼을 Button의 어떤 props로든 커스터마이즈할 수 있어요. 기본값은 { variant: 'link' }예요.
<script setup lang="ts">
const value = ref(5)
</script>
<template>
<UInputNumber v-model="value" :increment="{
color: 'neutral',
variant: 'solid',
size: 'xs'
}" :decrement="{
color: 'neutral',
variant: 'solid',
size: 'xs'
}" />
</template>
증가 / 감소 아이콘 (Increment / Decrement Icons)
increment-icon과 decrement-icon prop을 사용하면 버튼 Icon을 커스터마이즈할 수 있어요. 기본값은 i-lucide-plus / i-lucide-minus예요.
<script setup lang="ts">
const value = ref(5)
</script>
<template>
<UInputNumber v-model="value" increment-icon="i-lucide-arrow-right" decrement-icon="i-lucide-arrow-left" />
</template>
예시 (Examples)
소수점 형식 (With decimal format)
format-options prop을 사용하면 값의 형식을 커스터마이즈할 수 있어요.
<script setup lang="ts">
const value = ref(5)
</script>
<template>
<UInputNumber
v-model="value"
:format-options="{
signDisplay: 'exceptZero',
minimumFractionDigits: 1
}"
/>
</template>
백분율 형식 (With percentage format)
format-options prop을 style: 'percent'와 함께 사용하면 값의 형식을 커스터마이즈할 수 있어요.
<script setup lang="ts">
const value = ref(0.05)
</script>
<template>
<UInputNumber
v-model="value"
:step="0.01"
:format-options="{
style: 'percent'
}"
/>
</template>
통화 형식 (With currency format)
format-options prop을 style: 'currency'와 함께 사용하면 값의 형식을 커스터마이즈할 수 있어요.
<script setup lang="ts">
const value = ref(1500)
</script>
<template>
<UInputNumber
v-model="value"
:format-options="{
style: 'currency',
currency: 'EUR',
currencyDisplay: 'code',
currencySign: 'accounting'
}"
/>
</template>
버튼 없이 (Without buttons)
increment과 decrement prop을 사용해 버튼의 표시 여부를 제어할 수 있어요.
<script setup lang="ts">
const value = ref(5)
</script>
<template>
<UInputNumber
v-model="value"
:increment="false"
:decrement="false"
/>
</template>
FormField 안에서 (Within a FormField)
InputNumber를 FormField 컴포넌트 안에서 사용하면 라벨, 도움말 텍스트, 필수 표시기 등을 표시할 수 있어요.
<script setup lang="ts">
const retries = ref(0)
</script>
<template>
<UFormField label="Retries" help="Specify number of attempts" required>
<UInputNumber v-model="retries" placeholder="Enter retries" />
</UFormField>
</template>
슬롯 사용하기 (With slots)
#increment과 #decrement 슬롯을 사용하면 버튼을 커스터마이즈할 수 있어요.
<script setup lang="ts">
const value = ref(5)
</script>
<template>
<UInputNumber v-model="value">
<template #decrement>
<UButton size="xs" icon="i-lucide-minus" />
</template>
<template #increment>
<UButton size="xs" icon="i-lucide-plus" />
</template>
</UInputNumber>
</template>
API
Props
/**
* Props for the InputNumber component
*/
interface InputNumberProps {
/**
* The element or component this component should render as.
* @default 'div'
*/
as?: any;
/**
* The placeholder text when the input is empty.
*/
placeholder?: string | undefined;
/**
* @default 'primary'
*/
color?: "primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral" | undefined;
/**
* @default 'outline'
*/
variant?: "outline" | "soft" | "subtle" | "ghost" | "none" | undefined;
/**
* @default 'md'
*/
size?: "xs" | "sm" | "md" | "lg" | "xl" | undefined;
/**
* Highlight the ring color like a focus state.
*/
highlight?: boolean | undefined;
/**
* Keep the mobile text size on all breakpoints.
*/
fixed?: boolean | undefined;
/**
* The orientation of the input number.
* @default 'horizontal'
*/
orientation?: "horizontal" | "vertical" | undefined;
/**
* Configure the increment button. The `color` and `size` are inherited.
* @default true
*/
increment?: boolean | Omit<ButtonProps, LinkPropsKeys> | undefined;
/**
* The icon displayed to increment the value.
* @default appConfig.ui.icons.plus
*/
incrementIcon?: any;
/**
* Disable the increment button.
*/
incrementDisabled?: boolean | undefined;
/**
* Configure the decrement button. The `color` and `size` are inherited.
* @default true
*/
decrement?: boolean | Omit<ButtonProps, LinkPropsKeys> | undefined;
/**
* The icon displayed to decrement the value.
* @default appConfig.ui.icons.minus
*/
decrementIcon?: any;
/**
* Disable the decrement button.
*/
decrementDisabled?: boolean | undefined;
autofocus?: boolean | undefined;
autofocusDelay?: number | undefined;
defaultValue?: NonNullable<T> | undefined;
modelValue?: T | Mod extends { optional: true; } ? undefined : never | undefined;
modelModifiers?: Mod | undefined;
ui?: { root?: SlotClass; base?: SlotClass; increment?: SlotClass; decrement?: SlotClass; } | undefined;
/**
* The smallest value allowed for the input.
*/
min?: number | undefined;
/**
* The largest value allowed for the input.
*/
max?: number | undefined;
/**
* The amount that the input value changes with each increment or decrement "tick".
*/
step?: number | undefined;
/**
* When `false`, prevents the value from snapping to the nearest increment of the step value
*/
stepSnapping?: boolean | undefined;
/**
* When `true`, prevents the user from interacting with the Number Field.
*/
disabled?: boolean | undefined;
/**
* When `true`, indicates that the user must set the value before the owning form can be submitted.
*/
required?: boolean | undefined;
/**
* Id of the element
*/
id?: string | undefined;
/**
* The name of the field. Submitted with its owning form as part of a name/value pair.
*/
name?: string | undefined;
/**
* Formatting options for the value displayed in the number field. This also affects what characters are allowed to be typed by the user.
*/
formatOptions?: Intl.NumberFormatOptions | undefined;
/**
* When `true`, prevents the value from changing on wheel scroll.
*/
disableWheelChange?: boolean | undefined;
/**
* When `true`, inverts the direction of the wheel change.
*/
invertWheelChange?: boolean | undefined;
/**
* When `true`, the Number Field is read-only.
*/
readonly?: boolean | undefined;
/**
* When `true`, the input will be focused when the value changes.
*/
focusOnChange?: boolean | undefined;
/**
* The locale to use for formatting and currencies
*/
locale?: string | undefined;
enterKeyHint?: "enter" | "done" | "go" | "next" | "previous" | "search" | "send" | undefined;
form?: string | undefined;
formaction?: string | undefined;
formenctype?: string | undefined;
formmethod?: string | undefined;
formnovalidate?: false | true | "true" | "false" | undefined;
formtarget?: string | undefined;
list?: string | undefined;
autocomplete?: "on" | "off" | string & {} | undefined;
}
[!NOTE] See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attributes
이 컴포넌트는 모든 기본
<input>HTML 속성도 지원해요.
Slots
/**
* Slots for the InputNumber component
*/
interface InputNumberSlots {
increment(): any;
decrement(): any;
}
Emits
/**
* Emitted events for the InputNumber component
*/
interface InputNumberEmits {
update:modelValue: (payload: [value: ApplyModifiers<T, Mod>]) => void;
blur: (payload: [event: FocusEvent]) => void;
change: (payload: [event: Event]) => void;
}
Expose
템플릿 ref로 컴포넌트에 접근할 때 다음을 사용할 수 있어요:
| 이름 | 타입 |
|---|---|
inputRef |
Ref<HTMLInputElement | null> |
테마 (Theme)
export default defineAppConfig({
ui: {
inputNumber: {
slots: {
root: 'relative inline-flex items-center',
base: [
'w-full rounded-md border-0 placeholder:text-dimmed disabled:cursor-not-allowed disabled:opacity-75',
'transition-colors'
],
increment: 'absolute flex items-center',
decrement: 'absolute flex items-center'
},
variants: {
fieldGroup: {
horizontal: {
root: 'group has-focus-visible:z-[1]',
base: 'group-not-only:group-first:rounded-e-none group-not-only:group-last:rounded-s-none group-not-last:group-not-first:rounded-none'
},
vertical: {
root: 'group has-focus-visible:z-[1]',
base: 'group-not-only:group-first:rounded-b-none group-not-only:group-last:rounded-t-none group-not-last:group-not-first:rounded-none'
}
},
color: {
primary: '',
secondary: '',
success: '',
info: '',
warning: '',
error: '',
neutral: ''
},
size: {
xs: 'px-2 py-1 text-sm/4 gap-1',
sm: 'px-2.5 py-1.5 text-sm/4 gap-1.5',
md: 'px-2.5 py-1.5 text-base/5 gap-1.5',
lg: 'px-3 py-2 text-base/5 gap-2',
xl: 'px-3 py-2 text-base gap-2'
},
variant: {
outline: 'text-highlighted bg-default ring ring-inset ring-accented',
soft: 'text-highlighted bg-elevated/50 hover:bg-elevated focus:bg-elevated disabled:bg-elevated/50',
subtle: 'text-highlighted bg-elevated ring ring-inset ring-accented',
ghost: 'text-highlighted bg-transparent hover:bg-elevated focus:bg-elevated disabled:bg-transparent dark:disabled:bg-transparent',
none: 'text-highlighted bg-transparent focus:outline-none'
},
disabled: {
true: {
increment: 'opacity-75 cursor-not-allowed',
decrement: 'opacity-75 cursor-not-allowed'
}
},
orientation: {
horizontal: {
base: 'text-center',
increment: 'inset-y-0 end-0 pe-1',
decrement: 'inset-y-0 start-0 ps-1'
},
vertical: {
increment: 'top-0 end-0 pe-1 [&>button]:py-0 scale-80',
decrement: 'bottom-0 end-0 pe-1 [&>button]:py-0 scale-80'
}
},
highlight: {
true: ''
},
fixed: {
false: ''
},
increment: {
false: ''
},
decrement: {
false: ''
}
},
compoundVariants: [
{
color: 'primary',
variant: [
'outline',
'subtle'
],
class: 'outline-primary/25 focus-visible:outline-3 focus-visible:ring-primary'
},
{
color: 'secondary',
variant: [
'outline',
'subtle'
],
class: 'outline-secondary/25 focus-visible:outline-3 focus-visible:ring-secondary'
},
{
color: 'success',
variant: [
'outline',
'subtle'
],
class: 'outline-success/25 focus-visible:outline-3 focus-visible:ring-success'
},
{
color: 'info',
variant: [
'outline',
'subtle'
],
class: 'outline-info/25 focus-visible:outline-3 focus-visible:ring-info'
},
{
color: 'warning',
variant: [
'outline',
'subtle'
],
class: 'outline-warning/25 focus-visible:outline-3 focus-visible:ring-warning'
},
{
color: 'error',
variant: [
'outline',
'subtle'
],
class: 'outline-error/25 focus-visible:outline-3 focus-visible:ring-error'
},
{
color: 'primary',
variant: [
'soft',
'ghost'
],
class: 'outline-primary/25 focus-visible:outline-3'
},
{
color: 'secondary',
variant: [
'soft',
'ghost'
],
class: 'outline-secondary/25 focus-visible:outline-3'
},
{
color: 'success',
variant: [
'soft',
'ghost'
],
class: 'outline-success/25 focus-visible:outline-3'
},
{
color: 'info',
variant: [
'soft',
'ghost'
],
class: 'outline-info/25 focus-visible:outline-3'
},
{
color: 'warning',
variant: [
'soft',
'ghost'
],
class: 'outline-warning/25 focus-visible:outline-3'
},
{
color: 'error',
variant: [
'soft',
'ghost'
],
class: 'outline-error/25 focus-visible:outline-3'
},
{
color: 'primary',
highlight: true,
class: 'ring ring-inset ring-primary'
},
{
color: 'secondary',
highlight: true,
class: 'ring ring-inset ring-secondary'
},
{
color: 'success',
highlight: true,
class: 'ring ring-inset ring-success'
},
{
color: 'info',
highlight: true,
class: 'ring ring-inset ring-info'
},
{
color: 'warning',
highlight: true,
class: 'ring ring-inset ring-warning'
},
{
color: 'error',
highlight: true,
class: 'ring ring-inset ring-error'
},
{
color: 'neutral',
variant: [
'outline',
'subtle'
],
class: 'outline-inverted/25 focus-visible:outline-3 focus-visible:ring-inverted'
},
{
color: 'neutral',
variant: [
'soft',
'ghost'
],
class: 'outline-inverted/25 focus-visible:outline-3'
},
{
color: 'neutral',
highlight: true,
class: 'ring ring-inset ring-inverted'
},
{
orientation: 'horizontal',
decrement: false,
class: 'text-start'
},
{
decrement: true,
size: 'xs',
class: 'ps-7'
},
{
decrement: true,
size: 'sm',
class: 'ps-8'
},
{
decrement: true,
size: 'md',
class: 'ps-9'
},
{
decrement: true,
size: 'lg',
class: 'ps-10'
},
{
decrement: true,
size: 'xl',
class: 'ps-11'
},
{
increment: true,
size: 'xs',
class: 'pe-7'
},
{
increment: true,
size: 'sm',
class: 'pe-8'
},
{
increment: true,
size: 'md',
class: 'pe-9'
},
{
increment: true,
size: 'lg',
class: 'pe-10'
},
{
increment: true,
size: 'xl',
class: 'pe-11'
},
{
fixed: false,
size: 'xs',
class: 'md:text-xs'
},
{
fixed: false,
size: 'sm',
class: 'md:text-xs'
},
{
fixed: false,
size: 'md',
class: 'md:text-sm'
},
{
fixed: false,
size: 'lg',
class: 'md:text-sm'
}
],
defaultVariants: {
size: 'md',
color: 'primary',
variant: 'outline'
}
}
}
})