Number Field
支持步进调节、本地化格式化、空值处理与标签拖动调值的 Shadcn 数值输入组件。
Number Field 提供符合 spinbutton 语义的数值输入框,支持加减步进按钮、键盘步进、基于 Intl 的本地化格式化、可空值以及标签拖动调值。本页示例使用 Shadcn Rhea 风格。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<div class="grid w-full max-w-xs gap-4">
<proto-ui-shadcn-number-field-root id="quantity-field">
<proto-ui-shadcn-number-field-label>
Quantity
</proto-ui-shadcn-number-field-label>
<proto-ui-shadcn-number-field-decrement>
<span aria-hidden="true">−</span>
<span class="sr-only">Decrease Quantity</span>
</proto-ui-shadcn-number-field-decrement>
<proto-ui-shadcn-number-field-input></proto-ui-shadcn-number-field-input>
<proto-ui-shadcn-number-field-increment>
<span aria-hidden="true">+</span>
<span class="sr-only">Increase Quantity</span>
</proto-ui-shadcn-number-field-increment>
</proto-ui-shadcn-number-field-root>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const root = document.querySelector('#quantity-field');
if (root instanceof HTMLElement) {
setElementProps(root, {
styleProfile: 'rhea',
defaultValue: 12,
min: 0,
max: 100,
step: 1,
ariaLabel: 'Quantity',
});
}
</script>将于 0.3.0-rc.1 及后续版本中提供。可直接从 @proto.ui/prototypes-shadcn/number-field 导入各部件原型:
import { numberFieldRoot, numberFieldLabel, numberFieldInput, numberFieldControl, numberFieldIncrement, numberFieldDecrement,} from '@proto.ui/prototypes-shadcn/number-field';Number Field 由 Root、Label、Input、Control、Increment 和 Decrement 组成:
<ShadcnNumberFieldRoot defaultValue={12} min={0} max={100} step={1} ariaLabel="Quantity"> <ShadcnNumberFieldLabel>Quantity</ShadcnNumberFieldLabel> <ShadcnNumberFieldDecrement> <span aria-hidden="true">−</span> <span className="sr-only">Decrease Quantity</span> </ShadcnNumberFieldDecrement> <ShadcnNumberFieldInput /> <ShadcnNumberFieldIncrement> <span aria-hidden="true">+</span> <span className="sr-only">Increase Quantity</span> </ShadcnNumberFieldIncrement></ShadcnNumberFieldRoot>在 Field 或 FormField 内使用时,请将 Input 替换为 Control 以自动联动 FieldLabel、校验错误状态、表单序列化与重置:
<ShadcnFieldRoot required> <ShadcnFieldLabel>Quantity</ShadcnFieldLabel> <ShadcnNumberFieldRoot defaultValue={1} min={1} max={99}> <ShadcnNumberFieldDecrement>−</ShadcnNumberFieldDecrement> <ShadcnNumberFieldControl /> <ShadcnNumberFieldIncrement>+</ShadcnNumberFieldIncrement> </ShadcnNumberFieldRoot> <ShadcnFieldError /></ShadcnFieldRoot>- 键盘与编辑:输入期间保留未完成草稿,按
Enter或失焦时提交合法数值或恢复上次有效值;ArrowUp/ArrowDown按step步进(按住Shift以 10 倍步长调整),Home/End跳至min/max边界。IME 组合输入期间自动暂停解析、提交、步进与拖动。 - 本地化格式化:通过
locale和formatOptions支持十进制、货币和百分比展示及解析;展示舍入不会修改底层数值精度,格式配置非法时标记formatInvalid并回退无分组十进制。 - 空值与拖动:开启
allowEmpty允许清空输入框提交null(通过getValue()读取精确的number | null,通过empty判空);开启scrub可在Label上水平拖动连续调值。
本地化货币与百分比
Section titled “本地化货币与百分比” Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<div class="grid w-full max-w-xs gap-4">
<proto-ui-shadcn-number-field-root id="budget-field">
<proto-ui-shadcn-number-field-label>
Budget (USD)
</proto-ui-shadcn-number-field-label>
<proto-ui-shadcn-number-field-decrement>
<span aria-hidden="true">−</span>
<span class="sr-only">Decrease Budget</span>
</proto-ui-shadcn-number-field-decrement>
<proto-ui-shadcn-number-field-input></proto-ui-shadcn-number-field-input>
<proto-ui-shadcn-number-field-increment>
<span aria-hidden="true">+</span>
<span class="sr-only">Increase Budget</span>
</proto-ui-shadcn-number-field-increment>
</proto-ui-shadcn-number-field-root>
<proto-ui-shadcn-number-field-root id="discount-field">
<proto-ui-shadcn-number-field-label>
Discount rate
</proto-ui-shadcn-number-field-label>
<proto-ui-shadcn-number-field-decrement>
<span aria-hidden="true">−</span>
<span class="sr-only">Decrease Discount rate</span>
</proto-ui-shadcn-number-field-decrement>
<proto-ui-shadcn-number-field-input></proto-ui-shadcn-number-field-input>
<proto-ui-shadcn-number-field-increment>
<span aria-hidden="true">+</span>
<span class="sr-only">Increase Discount rate</span>
</proto-ui-shadcn-number-field-increment>
</proto-ui-shadcn-number-field-root>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const budget = document.querySelector('#budget-field');
const discount = document.querySelector('#discount-field');
if (budget instanceof HTMLElement) {
setElementProps(budget, {
styleProfile: 'rhea',
defaultValue: 1250,
min: 0,
max: 100000,
step: 25,
locale: 'en-US',
formatOptions: { style: 'currency', currency: 'USD' },
ariaLabel: 'Budget (USD)',
});
}
if (discount instanceof HTMLElement) {
setElementProps(discount, {
styleProfile: 'rhea',
defaultValue: 0.25,
min: 0,
max: 1,
step: 0.05,
locale: 'en-US',
formatOptions: { style: 'percent' },
ariaLabel: 'Discount rate',
});
}
</script>标签拖动调值与可空值
Section titled “标签拖动调值与可空值” Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<div class="grid w-full max-w-xs gap-4">
<proto-ui-shadcn-number-field-root id="opacity-field">
<proto-ui-shadcn-number-field-label>
Opacity (drag label)
</proto-ui-shadcn-number-field-label>
<proto-ui-shadcn-number-field-decrement>
<span aria-hidden="true">−</span>
<span class="sr-only">Decrease Opacity</span>
</proto-ui-shadcn-number-field-decrement>
<proto-ui-shadcn-number-field-input></proto-ui-shadcn-number-field-input>
<proto-ui-shadcn-number-field-increment>
<span aria-hidden="true">+</span>
<span class="sr-only">Increase Opacity</span>
</proto-ui-shadcn-number-field-increment>
</proto-ui-shadcn-number-field-root>
<proto-ui-shadcn-number-field-root id="seats-field">
<proto-ui-shadcn-number-field-label>
Optional seat limit
</proto-ui-shadcn-number-field-label>
<proto-ui-shadcn-number-field-decrement>
<span aria-hidden="true">−</span>
<span class="sr-only">Decrease Optional seat limit</span>
</proto-ui-shadcn-number-field-decrement>
<proto-ui-shadcn-number-field-input></proto-ui-shadcn-number-field-input>
<proto-ui-shadcn-number-field-increment>
<span aria-hidden="true">+</span>
<span class="sr-only">Increase Optional seat limit</span>
</proto-ui-shadcn-number-field-increment>
</proto-ui-shadcn-number-field-root>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const opacity = document.querySelector('#opacity-field');
const seats = document.querySelector('#seats-field');
if (opacity instanceof HTMLElement) {
setElementProps(opacity, {
styleProfile: 'rhea',
defaultValue: 60,
min: 0,
max: 100,
step: 1,
scrub: true,
scrubSteps: 50,
ariaLabel: 'Opacity',
});
}
if (seats instanceof HTMLElement) {
setElementProps(seats, {
styleProfile: 'rhea',
defaultValue: null,
min: 1,
max: 500,
step: 1,
allowEmpty: true,
ariaLabel: 'Optional seat limit',
});
}
</script>禁用、只读与无边界自由步进
Section titled “禁用、只读与无边界自由步进” Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<div class="grid w-full max-w-xs gap-4">
<proto-ui-shadcn-number-field-root id="disabled-field">
<proto-ui-shadcn-number-field-label>
Disabled
</proto-ui-shadcn-number-field-label>
<proto-ui-shadcn-number-field-decrement>
<span aria-hidden="true">−</span>
<span class="sr-only">Decrease Disabled</span>
</proto-ui-shadcn-number-field-decrement>
<proto-ui-shadcn-number-field-input></proto-ui-shadcn-number-field-input>
<proto-ui-shadcn-number-field-increment>
<span aria-hidden="true">+</span>
<span class="sr-only">Increase Disabled</span>
</proto-ui-shadcn-number-field-increment>
</proto-ui-shadcn-number-field-root>
<proto-ui-shadcn-number-field-root id="readonly-field">
<proto-ui-shadcn-number-field-label>
Read-only
</proto-ui-shadcn-number-field-label>
<proto-ui-shadcn-number-field-decrement>
<span aria-hidden="true">−</span>
<span class="sr-only">Decrease Read-only</span>
</proto-ui-shadcn-number-field-decrement>
<proto-ui-shadcn-number-field-input></proto-ui-shadcn-number-field-input>
<proto-ui-shadcn-number-field-increment>
<span aria-hidden="true">+</span>
<span class="sr-only">Increase Read-only</span>
</proto-ui-shadcn-number-field-increment>
</proto-ui-shadcn-number-field-root>
<proto-ui-shadcn-number-field-root id="unbounded-field">
<proto-ui-shadcn-number-field-label>
Unbounded offset
</proto-ui-shadcn-number-field-label>
<proto-ui-shadcn-number-field-decrement>
<span aria-hidden="true">−</span>
<span class="sr-only">Decrease Unbounded offset</span>
</proto-ui-shadcn-number-field-decrement>
<proto-ui-shadcn-number-field-input></proto-ui-shadcn-number-field-input>
<proto-ui-shadcn-number-field-increment>
<span aria-hidden="true">+</span>
<span class="sr-only">Increase Unbounded offset</span>
</proto-ui-shadcn-number-field-increment>
</proto-ui-shadcn-number-field-root>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const disabled = document.querySelector('#disabled-field');
const readOnly = document.querySelector('#readonly-field');
const unbounded = document.querySelector('#unbounded-field');
if (disabled instanceof HTMLElement)
setElementProps(disabled, {
styleProfile: 'rhea',
defaultValue: 42,
disabled: true,
});
if (readOnly instanceof HTMLElement)
setElementProps(readOnly, {
styleProfile: 'rhea',
defaultValue: 24,
readOnly: true,
});
if (unbounded instanceof HTMLElement) {
setElementProps(unbounded, {
styleProfile: 'rhea',
defaultValue: 3.14,
min: null,
max: null,
step: 0.5,
snapToStep: false,
});
}
</script> Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<div class="grid w-full max-w-xs gap-4">
<proto-ui-shadcn-number-field-root id="controlled-field">
<proto-ui-shadcn-number-field-label>
Controlled volume
</proto-ui-shadcn-number-field-label>
<proto-ui-shadcn-number-field-decrement>
<span aria-hidden="true">−</span>
<span class="sr-only">Decrease Controlled volume</span>
</proto-ui-shadcn-number-field-decrement>
<proto-ui-shadcn-number-field-input></proto-ui-shadcn-number-field-input>
<proto-ui-shadcn-number-field-increment>
<span aria-hidden="true">+</span>
<span class="sr-only">Increase Controlled volume</span>
</proto-ui-shadcn-number-field-increment>
</proto-ui-shadcn-number-field-root>
<div
id="controlled-status"
role="status"
aria-live="polite"
class="text-xs text-muted-foreground tabular-nums"
>
Accepted value: 25 · Commits: 0
</div>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const root = document.querySelector('#controlled-field');
const status = document.querySelector('#controlled-status');
let value = 25;
let commits = 0;
const sync = () => {
if (root instanceof HTMLElement) {
setElementProps(root, {
styleProfile: 'rhea',
value,
min: 0,
max: 100,
step: 5,
allowEmpty: true,
});
}
if (status)
status.textContent =
'Accepted value: ' +
(value ?? 'empty') +
' · Commits: ' +
commits;
};
sync();
root?.addEventListener('valueChange', (event) => {
value = event.detail.value;
sync();
});
root?.addEventListener('valueCommit', (event) => {
value = event.detail.value;
commits += 1;
sync();
});
</script>RTL 布局与反向拖动
Section titled “RTL 布局与反向拖动” Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<div dir="rtl" class="grid w-full max-w-xs gap-4">
<proto-ui-shadcn-number-field-root id="rtl-field">
<proto-ui-shadcn-number-field-label>
الكمية (اسحب التسمية)
</proto-ui-shadcn-number-field-label>
<proto-ui-shadcn-number-field-decrement>
<span aria-hidden="true">−</span>
<span class="sr-only">إنقاص الكمية</span>
</proto-ui-shadcn-number-field-decrement>
<proto-ui-shadcn-number-field-input></proto-ui-shadcn-number-field-input>
<proto-ui-shadcn-number-field-increment>
<span aria-hidden="true">+</span>
<span class="sr-only">زيادة الكمية</span>
</proto-ui-shadcn-number-field-increment>
</proto-ui-shadcn-number-field-root>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const root = document.querySelector('#rtl-field');
if (root instanceof HTMLElement) {
setElementProps(root, {
styleProfile: 'rhea',
defaultValue: 40,
min: 0,
max: 100,
step: 5,
direction: 'rtl',
scrub: true,
scrubSteps: 20,
ariaLabel: 'الكمية',
});
}
</script>Root 属性
Section titled “Root 属性”传入 value(包括 null)进入受控模式。Label、Input、Control、Increment 和 Decrement 同样接受可选的 styleProfile 以覆盖外层 Style Scope 预设。
| 属性 | 类型 | 默认值 |
|---|---|---|
value | number null | — |
defaultValue | number null | 0 |
min | number null | 0 |
max | number null | 100 |
step | number | 1 |
snapToStep | boolean | true |
disabled | boolean | false |
readOnly | boolean | false |
required | boolean | false |
ariaLabel | string | '' |
name | string | '' |
allowEmpty | boolean | false |
locale | string | — |
formatOptions | NumberFieldFormatOptions | — |
scrub | boolean | false |
scrubSteps | number | 100 |
direction | 'ltr' 'rtl' | 'ltr' |
styleProfile | ShadcnStyleId | 继承 Scope 或 'nova' |
事件、方法与状态
Section titled “事件、方法与状态”| 成员 | 签名 / 类型 | 所属部件 |
|---|---|---|
valueChange | Event<{ value: number null }> | Root |
valueCommit | Event<{ value: number null }> | Root |
getValue() | () => number null | Root / Input / Control |
requestValue(next) | (value: number null) => boolean | Root |
commitValue() | () => void | Root |
stepBy(direction) | (direction: number) => boolean | Root |
resetValue() | () => boolean | Root / Input / Control |
empty / dragging / formatInvalid / draft | ExposeState | Root |
disabled / focusVisible | ExposeState<boolean> | Increment / Decrement |