--- title: NumberField 数字输入框 description: 数字输入字段,包含增减按钮、校验与国际化格式化能力。 component: number-field group: 组件 --- ## 用法 ```vue ``` ## 组件结构 ```vue ``` > **NumberField** 允许用户输入数值,并可选择是否显示增减按钮。它支持国际化格式化、校验与键盘导航。 ## 示例 ### 变体 NumberField 组件支持两种视觉变体: - **`primary`**(默认)— 带阴影的标准样式,适用于大多数场景 - **`secondary`** — 低强调、无阴影,适合在 Surface 等表面背景上使用 ### 表面样式 在 [Surface](/components/surface) 内使用时,请使用 `variant="secondary"`,以应用适合表面背景的低强调变体。 ### 带描述 ### 必填字段 ### 禁用状态 ### 宽度充满 ### 表单校验 将 `isInvalid` 与 `FieldError` 配合使用,以展示校验消息。 ### 受控组件 控制 value 以与其他组件同步或执行自定义格式化。 ### 步进值 配置增减步进值以实现精确控制。 ### 格式化选项 支持将数字格式化为货币、百分比、小数或单位,并具备国际化能力。 ### 表单示例 完整的表单集成示例,包含校验与提交处理。 ### 带校验 通过受控 value 实现自定义校验逻辑。 ### 自定义图标 自定义增减按钮图标。 ### 步进箭头 使用垂直布局的 chevron 图标,呈现不同的视觉风格。 ### 渲染函数 ## 自定义样式 ### Tailwind CSS ### 全局 CSS 若要自定义组件类,可使用 `@layer components` 指令。[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。 ```css @layer components { .hv-number-field { @apply flex flex-col gap-1; } /* When invalid, the description is hidden automatically */ .hv-number-field[data-invalid="true"] [data-slot="description"], .hv-number-field[aria-invalid="true"] [data-slot="description"] { @apply hidden; } .hv-number-field__group { @apply bg-field text-field-foreground shadow-field rounded-field inline-flex h-9 items-center overflow-hidden border; } .hv-number-field__input { @apply flex-1 rounded-none border-0 bg-transparent px-3 py-2 tabular-nums; } .hv-number-field__increment-button, .hv-number-field__decrement-button { @apply flex h-full w-10 items-center justify-center rounded-none bg-transparent; } } ``` ## 样式参考 HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。 ### CSS 类 #### 基础类 [!toc] - `.hv-number-field` – 根容器,样式非常克制(`flex flex-col gap-1`) - `.hv-number-field__group` – 输入与按钮的容器,包含边框与背景样式 - `.hv-number-field__input` – 数字输入字段 - `.hv-number-field__increment-button` – 用于增加数值的按钮 - `.hv-number-field__decrement-button` – 用于减少数值的按钮 #### 变体类 [!toc] - `.hv-number-field--primary` – 带阴影的主变体(默认) - `.hv-number-field--secondary` – 无阴影的次变体,适合用在 surface 上 > **说明:** 子组件([Label](/components/label)、[Description](/components/description)、[FieldError](/components/field-error))拥有各自的 CSS 类与样式。自定义方式请参见对应文档。 ### 交互状态 NumberField 会根据状态自动管理以下 data 属性: - **Invalid**:`[data-invalid="true"]` 或 `[aria-invalid="true"]` – 无效时会自动隐藏 description 插槽 - **Disabled**:`[data-disabled="true"]` – 当 `isDisabled` 为 true 时应用 - **Focus Within**:`[data-focus-within="true"]` – 当输入框或按钮聚焦时应用 - **Focus Visible**:`[data-focus-visible="true"]` – 当焦点可见(键盘导航)时应用 - **Hovered**:`[data-hovered="true"]` – 当悬停在输入组或按钮上时应用 更多属性可通过作用域插槽获得(见下方的 NumberFieldRenderProps)。 ## API 参考 ### NumberField NumberField 使用 Vue 具名部件与 `v-model`,数值解析、格式化和步进基于 Reka NumberField,接入本库字段与表单协议。React 专属机制的对应关系见下方 Vue 适配说明。 #### Base Props | Prop | 类型 | 默认值 | 描述 | | -------------- | ----------------------------------------------- | ----------- | --------------------------------------------------------------------------------------------------- | | `default` 插槽 | `NumberFieldRenderProps` | - | 子组件(Label、Group、Input 等)或渲染函数。 | | `class` | `string \| string[] \| Record` | - | 用于样式的 CSS 类,支持 Vue 原生绑定。 | | `style` | `CSSProperties` | - | 行内样式,支持 Vue 原生绑定。 | | `fullWidth` | `boolean` | `false` | 数字字段是否占满容器宽度 | | `id` | `string` | - | 元素的唯一标识符。 | | `variant` | `"primary" \| "secondary"` | `"primary"` | 组件的视觉变体。`primary` 为默认带阴影样式。`secondary` 为低强调、无阴影变体,适合用在 surface 上。 | | `as` | `string` | `div` | 自定义根标签;原生 data-* 属性直接透传。 | #### Value Props | Prop | 类型 | 默认值 | 描述 | | ------------------------------ | --------------------------------- | ------ | ---------------------------- | | `modelValue` / `v-model` | `number \| null` | - | 当前值(受控)。 | | `defaultValue` | `number` | - | 默认值(非受控)。 | | `update:modelValue` / `change` | `(value: number \| null) => void` | - | 值变化时触发的事件处理函数。 | #### Formatting Props | Prop | 类型 | 默认值 | 描述 | | --------------- | -------------------------- | ------ | ---------------------------------------------- | | `formatOptions` | `Intl.NumberFormatOptions` | - | 数字格式化选项(货币、百分比、小数、单位等)。 | | `locale` | `string` | - | 数字格式化的区域设置。 | #### Validation Props | Prop | 类型 | 默认值 | 描述 | | -------------------- | ---------------------------------------------------------------------------- | ---------- | ------------------------------------ | | `isRequired` | `boolean` | `false` | 提交表单前是否要求用户输入。 | | `isInvalid` | `boolean` | - | 当前值是否无效。 | | `validate` | `(value: number \| null) => string \| string[] \| true \| null \| undefined` | - | 自定义校验函数。 | | `validationBehavior` | `'native' \| 'aria'` | `'native'` | 使用原生 HTML 表单校验或 ARIA 属性。 | | `validationErrors` | `string[]` | - | 服务端校验错误。 | #### Range Props | Prop | 类型 | 默认值 | 描述 | | ---------- | -------- | ------ | ------------------ | | `minValue` | `number` | - | 允许的最小值。 | | `maxValue` | `number` | - | 允许的最大值。 | | `step` | `number` | `1` | 增减操作的步进值。 | #### State Props | Prop | 类型 | 默认值 | 描述 | | ------------ | --------- | ------ | ---------------------- | | `isDisabled` | `boolean` | - | 是否禁用输入。 | | `isReadOnly` | `boolean` | - | 是否可选中但不可修改。 | #### Form Props | Prop | 类型 | 默认值 | 描述 | | ----------- | --------- | ------ | -------------------------------------- | | `name` | `string` | - | input 元素的名称,用于 HTML 表单提交。 | | `autoFocus` | `boolean` | - | 元素渲染后是否应获得焦点。 | #### Accessibility Props | Prop | 类型 | 默认值 | 描述 | | ------------------ | -------- | ------ | ---------------------------- | | `aria-label` | `string` | - | 没有可见标签时的无障碍标签。 | | `aria-labelledby` | `string` | - | 用于标注该字段的元素 ID。 | | `aria-describedby` | `string` | - | 用于描述该字段的元素 ID。 | | `aria-details` | `string` | - | 包含更多详情的元素 ID。 | ### Composition Components NumberField 需要与以下独立组件组合使用,请分别导入并直接使用: - **NumberFieldGroup** – 输入与按钮的容器 - **NumberFieldInput** – 数字输入字段 - **NumberFieldIncrementButton** – 用于增加数值的按钮 - **NumberFieldDecrementButton** – 用于减少数值的按钮 - **Label** – 字段标签组件(`@heroui-vue/components`) - **Description** – 辅助说明文本组件(`@heroui-vue/components`) - **FieldError** – 校验错误信息组件(`@heroui-vue/components`) 这些组件各自拥有 props API。请直接在 NumberField 内组合使用: ```vue ``` #### NumberFieldGroup Props NumberFieldGroup 支持原生 div 属性、事件与默认作用域插槽。 | Prop | 类型 | 默认值 | 描述 | | -------------- | ------------------ | ------ | ------------------------------------ | | `default` 插槽 | `GroupRenderProps` | - | 子组件(Input、Buttons)或渲染函数。 | | `class` | `string` | - | 用于样式的 CSS 类。 | #### NumberFieldInput Props NumberFieldInput 支持原生 input 属性与事件,表单 name/值、禁用和字段状态由根组件管理。 | Prop | 类型 | 默认值 | 描述 | | ------- | -------- | ------ | ------------------- | | `class` | `string` | - | 用于样式的 CSS 类。 | #### NumberFieldIncrementButton Props NumberFieldIncrementButton 支持原生 button 属性、事件、`isDisabled` 和唯一原生根 `asChild`。 | Prop | 类型 | 默认值 | 描述 | | -------------- | ------------- | -------------- | ---------------------------------------- | | `default` 插槽 | 插槽内容 | `` | 按钮的图标或内容。默认为加号图标。 | | `class` | `string` | - | 用于样式的 CSS 类。 | | `slot` | `"increment"` | `"increment"` | 必须设置为 `"increment"`(会自动设置)。 | #### NumberFieldDecrementButton Props NumberFieldDecrementButton 支持原生 button 属性、事件、`isDisabled` 和唯一原生根 `asChild`。 | Prop | 类型 | 默认值 | 描述 | | -------------- | ------------- | --------------- | ---------------------------------------- | | `default` 插槽 | 插槽内容 | `` | 按钮的图标或内容。默认为减号图标。 | | `class` | `string` | - | 用于样式的 CSS 类。 | | `slot` | `"decrement"` | `"decrement"` | 必须设置为 `"decrement"`(会自动设置)。 | ### NumberFieldRenderProps 默认作用域插槽提供以下值: | Prop | 类型 | 描述 | | ---------------- | --------------------- | ------------------------------------------------ | | `isDisabled` | `boolean` | 字段是否禁用。 | | `isInvalid` | `boolean` | 字段当前是否无效。 | | `isReadOnly` | `boolean` | 字段是否只读。 | | `isRequired` | `boolean` | 字段是否必填。 | | `isFocused` | `boolean` | 字段是否聚焦(已弃用,请使用 `isFocusWithin`)。 | | `isFocusWithin` | `boolean` | 是否有任意子元素聚焦。 | | `isFocusVisible` | `boolean` | 是否为可见焦点(键盘导航)。 | | `value` | `number \| null` | 当前值。 | | `minValue` | `number \| undefined` | 允许的最小值。 | | `maxValue` | `number \| undefined` | 允许的最大值。 | | `step` | `number` | 增减步进值。 | ## 相关案例 相关示例已包含在上方文档预览中。 ## 相关组件 [Label 标签](./label) · [Description 描述](./description) · [FieldError 字段错误](./field-error) ## Vue 适配说明 - 根 `v-model` 使用 `number | null`,空值为 `null`;传入 `modelValue`(包括显式 undefined)即受控,父级拒绝更新时保持父值。未传时使用 `defaultValue`。`update:modelValue` 与 `change` 同时发出规范数值,键盘步进立即提交,文本草稿在失焦或 Enter 时解析提交。 - `minValue` / `maxValue` / `step` 映射到底座;`locale` 继承 ConfigProvider,未提供时为 en-US。`isWheelDisabled` 默认 true,可设 false 开启聚焦后的滚轮步进。 - `isDisabled` / `isReadOnly` / `isRequired` 也支持原生别名 disabled / readonly / required。Fieldset 禁用不能被子项 false 解除。readonly 保留表单值但阻止改值和必填校验。 - 根 class/style、data-* 和 DOM 事件归根容器;根 aria-*、form、autoFocus 关联真实 input。Input 自身 aria-label 等属性优先,描述 ID 与内部 Description/FieldError 合并。根 id 或 Input 显式 id 用于 Label/for;Input 显式 id 优先。 - FormData 只提交一份未格式化的完整数值,显示货币或百分比不改变提交值。native 校验阻止无效提交;aria 只展示状态。reset 尊重取消,非受控恢复初值,受控保留父值。validate 为同步函数;不提供 RAC validationDetails。 - React render prop 对应默认作用域插槽;class/style 函数改用 Vue 绑定,不支持 React PressEvent。渲染函数示例用 data-custom 透传表达相同默认 div 的自定义属性,不宣称支持任意 React render 回调。 - 增减按钮的 click 可取消;支持指针按住重复、释放/取消/移出清理及键盘激活。默认图标移植上游公共图标,自定义图标示例使用 Tabler Vue。任意非原生交互 asChild 根的键盘语义由调用方负责。 - 校验示例的百分比模型按 0–1 判断(原演示将模型与 0–100 混用);表单示例与基线一致,仅本地模拟提交并将提交值输出到控制台,定时器清理不代表真实业务请求。 - SSR 关联预规划支持直接部件、原生包装及 NumberFieldGroup。自定义 Vue 包装内部的 ID 请显式指定;完整读屏、触屏及 Nuxt 验收以验证报告为准。 - 上游 Input API 表还列有 variant,但固定实现只从根生成样式;Vue 同样使用根 variant,自定义 Input 外观通过 class/style,不暴露无效的叶子 variant 参数。