@teamix-evo/ui 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +336 -0
- package/_data.json +12 -0
- package/manifest.json +1688 -0
- package/package.json +90 -0
- package/src/components/accordion/accordion.meta.md +87 -0
- package/src/components/accordion/accordion.stories.tsx +67 -0
- package/src/components/accordion/accordion.tsx +58 -0
- package/src/components/affix/affix.meta.md +80 -0
- package/src/components/affix/affix.stories.tsx +57 -0
- package/src/components/affix/affix.tsx +97 -0
- package/src/components/alert/alert.meta.md +101 -0
- package/src/components/alert/alert.stories.tsx +93 -0
- package/src/components/alert/alert.tsx +132 -0
- package/src/components/alert-dialog/alert-dialog.meta.md +107 -0
- package/src/components/alert-dialog/alert-dialog.stories.tsx +81 -0
- package/src/components/alert-dialog/alert-dialog.tsx +136 -0
- package/src/components/anchor/anchor.meta.md +87 -0
- package/src/components/anchor/anchor.stories.tsx +74 -0
- package/src/components/anchor/anchor.tsx +130 -0
- package/src/components/app/app.meta.md +86 -0
- package/src/components/app/app.stories.tsx +62 -0
- package/src/components/app/app.tsx +58 -0
- package/src/components/aspect-ratio/aspect-ratio.meta.md +81 -0
- package/src/components/aspect-ratio/aspect-ratio.stories.tsx +59 -0
- package/src/components/aspect-ratio/aspect-ratio.tsx +22 -0
- package/src/components/auto-complete/auto-complete.meta.md +102 -0
- package/src/components/auto-complete/auto-complete.stories.tsx +93 -0
- package/src/components/auto-complete/auto-complete.tsx +205 -0
- package/src/components/avatar/avatar.meta.md +94 -0
- package/src/components/avatar/avatar.stories.tsx +80 -0
- package/src/components/avatar/avatar.tsx +126 -0
- package/src/components/badge/badge.meta.md +119 -0
- package/src/components/badge/badge.stories.tsx +153 -0
- package/src/components/badge/badge.tsx +210 -0
- package/src/components/breadcrumb/breadcrumb.meta.md +107 -0
- package/src/components/breadcrumb/breadcrumb.stories.tsx +84 -0
- package/src/components/breadcrumb/breadcrumb.tsx +122 -0
- package/src/components/button/button.meta.md +98 -0
- package/src/components/button/button.stories.tsx +235 -0
- package/src/components/button/button.tsx +160 -0
- package/src/components/button-group/button-group.meta.md +92 -0
- package/src/components/button-group/button-group.stories.tsx +90 -0
- package/src/components/button-group/button-group.tsx +75 -0
- package/src/components/calendar/calendar.meta.md +118 -0
- package/src/components/calendar/calendar.stories.tsx +68 -0
- package/src/components/calendar/calendar.tsx +107 -0
- package/src/components/card/card.meta.md +117 -0
- package/src/components/card/card.stories.tsx +112 -0
- package/src/components/card/card.tsx +222 -0
- package/src/components/carousel/carousel.meta.md +117 -0
- package/src/components/carousel/carousel.stories.tsx +84 -0
- package/src/components/carousel/carousel.tsx +224 -0
- package/src/components/cascader/cascader.meta.md +110 -0
- package/src/components/cascader/cascader.stories.tsx +108 -0
- package/src/components/cascader/cascader.tsx +198 -0
- package/src/components/checkbox/checkbox.meta.md +99 -0
- package/src/components/checkbox/checkbox.stories.tsx +130 -0
- package/src/components/checkbox/checkbox.tsx +125 -0
- package/src/components/collapsible/collapsible.meta.md +80 -0
- package/src/components/collapsible/collapsible.stories.tsx +35 -0
- package/src/components/collapsible/collapsible.tsx +18 -0
- package/src/components/color-picker/color-picker.meta.md +84 -0
- package/src/components/color-picker/color-picker.stories.tsx +80 -0
- package/src/components/color-picker/color-picker.tsx +160 -0
- package/src/components/combobox/combobox.meta.md +93 -0
- package/src/components/combobox/combobox.stories.tsx +55 -0
- package/src/components/combobox/combobox.tsx +130 -0
- package/src/components/command/command.meta.md +104 -0
- package/src/components/command/command.stories.tsx +59 -0
- package/src/components/command/command.tsx +147 -0
- package/src/components/context-menu/context-menu.meta.md +90 -0
- package/src/components/context-menu/context-menu.stories.tsx +46 -0
- package/src/components/context-menu/context-menu.tsx +191 -0
- package/src/components/data-table/data-table.meta.md +149 -0
- package/src/components/data-table/data-table.stories.tsx +125 -0
- package/src/components/data-table/data-table.tsx +185 -0
- package/src/components/date-picker/date-picker.meta.md +106 -0
- package/src/components/date-picker/date-picker.stories.tsx +58 -0
- package/src/components/date-picker/date-picker.tsx +156 -0
- package/src/components/descriptions/descriptions.meta.md +78 -0
- package/src/components/descriptions/descriptions.stories.tsx +60 -0
- package/src/components/descriptions/descriptions.tsx +129 -0
- package/src/components/dialog/dialog.meta.md +105 -0
- package/src/components/dialog/dialog.stories.tsx +93 -0
- package/src/components/dialog/dialog.tsx +128 -0
- package/src/components/drawer/drawer.meta.md +96 -0
- package/src/components/drawer/drawer.stories.tsx +54 -0
- package/src/components/drawer/drawer.tsx +114 -0
- package/src/components/dropdown-menu/dropdown-menu.meta.md +103 -0
- package/src/components/dropdown-menu/dropdown-menu.stories.tsx +112 -0
- package/src/components/dropdown-menu/dropdown-menu.tsx +195 -0
- package/src/components/empty/empty.meta.md +81 -0
- package/src/components/empty/empty.stories.tsx +46 -0
- package/src/components/empty/empty.tsx +47 -0
- package/src/components/field/field.meta.md +116 -0
- package/src/components/field/field.stories.tsx +117 -0
- package/src/components/field/field.tsx +164 -0
- package/src/components/flex/flex.meta.md +94 -0
- package/src/components/flex/flex.stories.tsx +112 -0
- package/src/components/flex/flex.tsx +122 -0
- package/src/components/float-button/float-button.meta.md +87 -0
- package/src/components/float-button/float-button.stories.tsx +78 -0
- package/src/components/float-button/float-button.tsx +143 -0
- package/src/components/form/form.meta.md +131 -0
- package/src/components/form/form.stories.tsx +122 -0
- package/src/components/form/form.tsx +194 -0
- package/src/components/grid/grid.meta.md +87 -0
- package/src/components/grid/grid.stories.tsx +99 -0
- package/src/components/grid/grid.tsx +130 -0
- package/src/components/hover-card/hover-card.meta.md +92 -0
- package/src/components/hover-card/hover-card.stories.tsx +68 -0
- package/src/components/hover-card/hover-card.tsx +29 -0
- package/src/components/image/image.meta.md +94 -0
- package/src/components/image/image.stories.tsx +55 -0
- package/src/components/image/image.tsx +138 -0
- package/src/components/input/input.meta.md +109 -0
- package/src/components/input/input.stories.tsx +117 -0
- package/src/components/input/input.tsx +213 -0
- package/src/components/input-group/input-group.meta.md +92 -0
- package/src/components/input-group/input-group.stories.tsx +88 -0
- package/src/components/input-group/input-group.tsx +107 -0
- package/src/components/input-number/input-number.meta.md +91 -0
- package/src/components/input-number/input-number.stories.tsx +87 -0
- package/src/components/input-number/input-number.tsx +210 -0
- package/src/components/input-otp/input-otp.meta.md +105 -0
- package/src/components/input-otp/input-otp.stories.tsx +65 -0
- package/src/components/input-otp/input-otp.tsx +97 -0
- package/src/components/item/item.meta.md +116 -0
- package/src/components/item/item.stories.tsx +113 -0
- package/src/components/item/item.tsx +171 -0
- package/src/components/kbd/kbd.meta.md +85 -0
- package/src/components/kbd/kbd.stories.tsx +70 -0
- package/src/components/kbd/kbd.tsx +81 -0
- package/src/components/label/label.meta.md +91 -0
- package/src/components/label/label.stories.tsx +87 -0
- package/src/components/label/label.tsx +66 -0
- package/src/components/masonry/masonry.meta.md +85 -0
- package/src/components/masonry/masonry.stories.tsx +66 -0
- package/src/components/masonry/masonry.tsx +59 -0
- package/src/components/mentions/mentions.meta.md +89 -0
- package/src/components/mentions/mentions.stories.tsx +75 -0
- package/src/components/mentions/mentions.tsx +237 -0
- package/src/components/menubar/menubar.meta.md +100 -0
- package/src/components/menubar/menubar.stories.tsx +81 -0
- package/src/components/menubar/menubar.tsx +232 -0
- package/src/components/native-select/native-select.meta.md +88 -0
- package/src/components/native-select/native-select.stories.tsx +80 -0
- package/src/components/native-select/native-select.tsx +54 -0
- package/src/components/navigation-menu/navigation-menu.meta.md +108 -0
- package/src/components/navigation-menu/navigation-menu.stories.tsx +112 -0
- package/src/components/navigation-menu/navigation-menu.tsx +125 -0
- package/src/components/notification/notification.meta.md +91 -0
- package/src/components/notification/notification.stories.tsx +96 -0
- package/src/components/notification/notification.tsx +84 -0
- package/src/components/pagination/pagination.meta.md +127 -0
- package/src/components/pagination/pagination.stories.tsx +62 -0
- package/src/components/pagination/pagination.tsx +285 -0
- package/src/components/popconfirm/popconfirm.meta.md +109 -0
- package/src/components/popconfirm/popconfirm.stories.tsx +76 -0
- package/src/components/popconfirm/popconfirm.tsx +134 -0
- package/src/components/popover/popover.meta.md +97 -0
- package/src/components/popover/popover.stories.tsx +82 -0
- package/src/components/popover/popover.tsx +55 -0
- package/src/components/progress/progress.meta.md +86 -0
- package/src/components/progress/progress.stories.tsx +75 -0
- package/src/components/progress/progress.tsx +195 -0
- package/src/components/radio-group/radio-group.meta.md +103 -0
- package/src/components/radio-group/radio-group.stories.tsx +77 -0
- package/src/components/radio-group/radio-group.tsx +78 -0
- package/src/components/rate/rate.meta.md +87 -0
- package/src/components/rate/rate.stories.tsx +81 -0
- package/src/components/rate/rate.tsx +153 -0
- package/src/components/resizable/resizable.meta.md +92 -0
- package/src/components/resizable/resizable.stories.tsx +104 -0
- package/src/components/resizable/resizable.tsx +56 -0
- package/src/components/result/result.meta.md +90 -0
- package/src/components/result/result.stories.tsx +71 -0
- package/src/components/result/result.tsx +91 -0
- package/src/components/scroll-area/scroll-area.meta.md +84 -0
- package/src/components/scroll-area/scroll-area.stories.tsx +41 -0
- package/src/components/scroll-area/scroll-area.tsx +51 -0
- package/src/components/segmented/segmented.meta.md +103 -0
- package/src/components/segmented/segmented.stories.tsx +101 -0
- package/src/components/segmented/segmented.tsx +138 -0
- package/src/components/select/select.meta.md +110 -0
- package/src/components/select/select.stories.tsx +100 -0
- package/src/components/select/select.tsx +188 -0
- package/src/components/separator/separator.meta.md +74 -0
- package/src/components/separator/separator.stories.tsx +71 -0
- package/src/components/separator/separator.tsx +104 -0
- package/src/components/sheet/sheet.meta.md +97 -0
- package/src/components/sheet/sheet.stories.tsx +82 -0
- package/src/components/sheet/sheet.tsx +139 -0
- package/src/components/sidebar/sidebar.meta.md +131 -0
- package/src/components/sidebar/sidebar.stories.tsx +82 -0
- package/src/components/sidebar/sidebar.tsx +351 -0
- package/src/components/skeleton/skeleton.meta.md +95 -0
- package/src/components/skeleton/skeleton.stories.tsx +79 -0
- package/src/components/skeleton/skeleton.tsx +144 -0
- package/src/components/slider/slider.meta.md +94 -0
- package/src/components/slider/slider.stories.tsx +69 -0
- package/src/components/slider/slider.tsx +86 -0
- package/src/components/sonner/sonner.meta.md +96 -0
- package/src/components/sonner/sonner.stories.tsx +91 -0
- package/src/components/sonner/sonner.tsx +40 -0
- package/src/components/space/space.meta.md +94 -0
- package/src/components/space/space.stories.tsx +94 -0
- package/src/components/space/space.tsx +106 -0
- package/src/components/spinner/spinner.meta.md +76 -0
- package/src/components/spinner/spinner.stories.tsx +71 -0
- package/src/components/spinner/spinner.tsx +64 -0
- package/src/components/statistic/statistic.meta.md +99 -0
- package/src/components/statistic/statistic.stories.tsx +71 -0
- package/src/components/statistic/statistic.tsx +197 -0
- package/src/components/steps/steps.meta.md +102 -0
- package/src/components/steps/steps.stories.tsx +75 -0
- package/src/components/steps/steps.tsx +170 -0
- package/src/components/switch/switch.meta.md +92 -0
- package/src/components/switch/switch.stories.tsx +75 -0
- package/src/components/switch/switch.tsx +101 -0
- package/src/components/table/table.meta.md +95 -0
- package/src/components/table/table.stories.tsx +75 -0
- package/src/components/table/table.tsx +122 -0
- package/src/components/tabs/tabs.meta.md +98 -0
- package/src/components/tabs/tabs.stories.tsx +70 -0
- package/src/components/tabs/tabs.tsx +119 -0
- package/src/components/tag/tag.meta.md +94 -0
- package/src/components/tag/tag.stories.tsx +77 -0
- package/src/components/tag/tag.tsx +185 -0
- package/src/components/textarea/textarea.meta.md +83 -0
- package/src/components/textarea/textarea.stories.tsx +63 -0
- package/src/components/textarea/textarea.tsx +113 -0
- package/src/components/time-picker/time-picker.meta.md +83 -0
- package/src/components/time-picker/time-picker.stories.tsx +59 -0
- package/src/components/time-picker/time-picker.tsx +94 -0
- package/src/components/timeline/timeline.meta.md +102 -0
- package/src/components/timeline/timeline.stories.tsx +104 -0
- package/src/components/timeline/timeline.tsx +147 -0
- package/src/components/toggle/toggle.meta.md +88 -0
- package/src/components/toggle/toggle.stories.tsx +66 -0
- package/src/components/toggle/toggle.tsx +53 -0
- package/src/components/toggle-group/toggle-group.meta.md +90 -0
- package/src/components/toggle-group/toggle-group.stories.tsx +83 -0
- package/src/components/toggle-group/toggle-group.tsx +78 -0
- package/src/components/tooltip/tooltip.meta.md +99 -0
- package/src/components/tooltip/tooltip.stories.tsx +71 -0
- package/src/components/tooltip/tooltip.tsx +93 -0
- package/src/components/tour/tour.meta.md +116 -0
- package/src/components/tour/tour.stories.tsx +66 -0
- package/src/components/tour/tour.tsx +242 -0
- package/src/components/transfer/transfer.meta.md +90 -0
- package/src/components/transfer/transfer.stories.tsx +68 -0
- package/src/components/transfer/transfer.tsx +251 -0
- package/src/components/tree/tree.meta.md +111 -0
- package/src/components/tree/tree.stories.tsx +109 -0
- package/src/components/tree/tree.tsx +367 -0
- package/src/components/tree-select/tree-select.meta.md +100 -0
- package/src/components/tree-select/tree-select.stories.tsx +80 -0
- package/src/components/tree-select/tree-select.tsx +171 -0
- package/src/components/typography/typography.meta.md +102 -0
- package/src/components/typography/typography.stories.tsx +115 -0
- package/src/components/typography/typography.tsx +245 -0
- package/src/components/upload/upload.meta.md +111 -0
- package/src/components/upload/upload.stories.tsx +75 -0
- package/src/components/upload/upload.tsx +265 -0
- package/src/components/watermark/watermark.meta.md +95 -0
- package/src/components/watermark/watermark.stories.tsx +78 -0
- package/src/components/watermark/watermark.tsx +165 -0
- package/src/utils/cn.ts +6 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: input-number
|
|
3
|
+
name: InputNumber
|
|
4
|
+
type: component
|
|
5
|
+
category: form
|
|
6
|
+
since: 0.1.0
|
|
7
|
+
package: "@teamix-evo/ui"
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# InputNumber
|
|
11
|
+
|
|
12
|
+
数字输入 — antd 独有补足。**等价 antd `InputNumber`**。区别于原生 `<input type="number">`:步进按钮显隐可配、范围裁剪与精度由组件保证、千分位展示、键盘 ↑↓ 步进、支持空状态(`value=null`)。
|
|
13
|
+
|
|
14
|
+
## When to use
|
|
15
|
+
|
|
16
|
+
- 价格 / 数量 / 度量等数值输入(配 `min` / `max` / `step` 范围限制)
|
|
17
|
+
- 需要精度控制的小数(`precision={2}`)
|
|
18
|
+
- 大数展示千分位(`groupSeparator`)
|
|
19
|
+
- 移动端友好的数字键盘(已设 `inputMode="decimal"`)
|
|
20
|
+
|
|
21
|
+
## When NOT to use
|
|
22
|
+
|
|
23
|
+
- 滑动选择 → `Slider`
|
|
24
|
+
- 多档枚举 → `Segmented` / `RadioGroup`
|
|
25
|
+
- 普通文本输入 → `Input`
|
|
26
|
+
|
|
27
|
+
<!-- auto:props:begin -->
|
|
28
|
+
| 名称 | 类型 | 默认值 | 必填 | 说明 |
|
|
29
|
+
| --- | --- | --- | --- | --- |
|
|
30
|
+
| `value` | `number \| null` | – | – | 受控值 — `null` 等价空输入。 |
|
|
31
|
+
| `defaultValue` | `number \| null` | – | – | uncontrolled 初值。 |
|
|
32
|
+
| `onChange` | `(value: number \| null) => void` | – | – | 值变化回调 — 输入清空时传 `null`,正常输入 / 步进传 `number`。 |
|
|
33
|
+
| `min` | `number` | – | – | 最小值(antd `min` 并集)。 |
|
|
34
|
+
| `max` | `number` | – | – | 最大值(antd `max` 并集)。 |
|
|
35
|
+
| `step` | `number` | `1` | – | 步长(antd `step` 并集) — 增 / 减按钮以及键盘 ↑↓ 都按此步长。 |
|
|
36
|
+
| `precision` | `number` | – | – | 数值精度(antd `precision` 并集) — 保留小数位数(超出会四舍五入)。 |
|
|
37
|
+
| `groupSeparator` | `boolean` | `false` | – | 千分位分隔展示(仅展示用,实际 value 仍是 number)。 |
|
|
38
|
+
| `controls` | `boolean` | `true` | – | 是否显示右侧上下步进按钮(antd `controls` 并集) — `false` 时隐藏。 |
|
|
39
|
+
| `size` | `'sm' \| 'default' \| 'lg'` | `"default"` | – | 尺寸。 |
|
|
40
|
+
<!-- auto:props:end -->
|
|
41
|
+
|
|
42
|
+
<!-- auto:deps:begin -->
|
|
43
|
+
### 同库依赖
|
|
44
|
+
|
|
45
|
+
> `teamix-evo ui add input-number` 时,以下 entry 会被自动连带安装(无需手动 add)。
|
|
46
|
+
|
|
47
|
+
| Entry | 类型 | 描述 |
|
|
48
|
+
| --- | --- | --- |
|
|
49
|
+
| `cn` | util | Tailwind className 合并工具(clsx + tailwind-merge) |
|
|
50
|
+
|
|
51
|
+
### npm 依赖
|
|
52
|
+
|
|
53
|
+
> 业务侧需要先 `pnpm add` / `npm install` 这些包。CLI 在 `ui add` 完成后会列出此提示。
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pnpm add lucide-react@^0.460.0
|
|
57
|
+
```
|
|
58
|
+
<!-- auto:deps:end -->
|
|
59
|
+
|
|
60
|
+
## AI 生成纪律
|
|
61
|
+
|
|
62
|
+
- **空状态 = `null`**(不要用 `0` 代表"空") — `onChange(null)` 时表单层应视为未填
|
|
63
|
+
- **`min` / `max` 自动夹紧**:用户输入越界值,blur 时组件会自动 clamp;**不要**在外层再做边界校验
|
|
64
|
+
- **`precision`** 控制保留小数位:超出位数会四舍五入展示,**实际 onChange 值也是 toFixed 后的 number**
|
|
65
|
+
- **`groupSeparator`** 仅展示用:实际 value 仍是裸 number,不会带逗号
|
|
66
|
+
- **`controls=false`** 仅在需要嵌入 InputGroup / 紧凑表格时关闭;默认带步进按钮更直观
|
|
67
|
+
- **键盘 ↑↓ 已实现** — 不要再监听 keydown 加自定义步进
|
|
68
|
+
- **不要传 `type="number"`** — 组件用 `type="text"` 才能支持千分位与精确控制
|
|
69
|
+
|
|
70
|
+
## Examples
|
|
71
|
+
|
|
72
|
+
```tsx
|
|
73
|
+
import { InputNumber } from '@/components/ui/input-number';
|
|
74
|
+
import * as React from 'react';
|
|
75
|
+
|
|
76
|
+
// 基础
|
|
77
|
+
<InputNumber defaultValue={10} min={0} max={100} />
|
|
78
|
+
|
|
79
|
+
// 精度 + 千分位
|
|
80
|
+
<InputNumber defaultValue={12345.67} precision={2} groupSeparator />
|
|
81
|
+
|
|
82
|
+
// 受控
|
|
83
|
+
const [v, setV] = React.useState<number | null>(1);
|
|
84
|
+
<InputNumber value={v} onChange={setV} step={0.5} min={0} max={5} />
|
|
85
|
+
|
|
86
|
+
// 隐藏步进按钮(配合 InputGroup 使用)
|
|
87
|
+
<InputNumber controls={false} defaultValue={1} className="rounded-none" />
|
|
88
|
+
|
|
89
|
+
// 小尺寸
|
|
90
|
+
<InputNumber size="sm" defaultValue={0} />
|
|
91
|
+
```
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
3
|
+
import { InputNumber } from './input-number';
|
|
4
|
+
|
|
5
|
+
const meta: Meta<typeof InputNumber> = {
|
|
6
|
+
title: '表单与输入 · Form/InputNumber',
|
|
7
|
+
component: InputNumber,
|
|
8
|
+
tags: ['autodocs'],
|
|
9
|
+
parameters: {
|
|
10
|
+
docs: {
|
|
11
|
+
description: {
|
|
12
|
+
component:
|
|
13
|
+
'数字输入框 — 区别于原生 type=number:步进按钮显隐可配、范围裁剪与精度由组件保证、千分位展示、键盘 ↑↓ 步进、支持空状态。等价 antd `InputNumber`。视觉走 OpenTrek tokens,所有样式来自 `@teamix-evo/design`,无 mock。',
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
argTypes: {
|
|
18
|
+
size: { control: 'inline-radio', options: ['sm', 'default', 'lg'] },
|
|
19
|
+
controls: { control: 'boolean' },
|
|
20
|
+
groupSeparator: { control: 'boolean' },
|
|
21
|
+
disabled: { control: 'boolean' },
|
|
22
|
+
},
|
|
23
|
+
args: { size: 'default', controls: true, groupSeparator: false, disabled: false },
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export default meta;
|
|
27
|
+
type Story = StoryObj<typeof InputNumber>;
|
|
28
|
+
|
|
29
|
+
export const Playground: Story = {
|
|
30
|
+
render: (args) => (
|
|
31
|
+
<InputNumber {...args} defaultValue={10} min={0} max={100} className="w-40" />
|
|
32
|
+
),
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const Precision: Story = {
|
|
36
|
+
parameters: { controls: { disable: true } },
|
|
37
|
+
render: () => (
|
|
38
|
+
<InputNumber
|
|
39
|
+
defaultValue={12345.67}
|
|
40
|
+
precision={2}
|
|
41
|
+
groupSeparator
|
|
42
|
+
step={0.01}
|
|
43
|
+
className="w-48"
|
|
44
|
+
/>
|
|
45
|
+
),
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const Controlled: Story = {
|
|
49
|
+
parameters: { controls: { disable: true } },
|
|
50
|
+
render: () => {
|
|
51
|
+
const [v, setV] = React.useState<number | null>(1.5);
|
|
52
|
+
return (
|
|
53
|
+
<div className="flex items-center gap-3">
|
|
54
|
+
<InputNumber
|
|
55
|
+
value={v}
|
|
56
|
+
onChange={setV}
|
|
57
|
+
step={0.5}
|
|
58
|
+
min={0}
|
|
59
|
+
max={5}
|
|
60
|
+
precision={1}
|
|
61
|
+
className="w-40"
|
|
62
|
+
/>
|
|
63
|
+
<span className="text-sm text-muted-foreground tabular-nums">
|
|
64
|
+
值: {v === null ? '空' : v}
|
|
65
|
+
</span>
|
|
66
|
+
</div>
|
|
67
|
+
);
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const NoControls: Story = {
|
|
72
|
+
parameters: { controls: { disable: true } },
|
|
73
|
+
render: () => (
|
|
74
|
+
<InputNumber controls={false} defaultValue={1} className="w-32" />
|
|
75
|
+
),
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const Sizes: Story = {
|
|
79
|
+
parameters: { controls: { disable: true } },
|
|
80
|
+
render: () => (
|
|
81
|
+
<div className="flex items-center gap-3">
|
|
82
|
+
<InputNumber size="sm" defaultValue={0} className="w-28" />
|
|
83
|
+
<InputNumber size="default" defaultValue={0} className="w-32" />
|
|
84
|
+
<InputNumber size="lg" defaultValue={0} className="w-36" />
|
|
85
|
+
</div>
|
|
86
|
+
),
|
|
87
|
+
};
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { ChevronDown, ChevronUp } from 'lucide-react';
|
|
3
|
+
|
|
4
|
+
import { cn } from '@/utils/cn';
|
|
5
|
+
|
|
6
|
+
export interface InputNumberProps
|
|
7
|
+
extends Omit<
|
|
8
|
+
React.InputHTMLAttributes<HTMLInputElement>,
|
|
9
|
+
'onChange' | 'value' | 'defaultValue' | 'min' | 'max' | 'step' | 'type' | 'size'
|
|
10
|
+
> {
|
|
11
|
+
/** 受控值 — `null` 等价空输入。 */
|
|
12
|
+
value?: number | null;
|
|
13
|
+
/** uncontrolled 初值。 */
|
|
14
|
+
defaultValue?: number | null;
|
|
15
|
+
/**
|
|
16
|
+
* 值变化回调 — 输入清空时传 `null`,正常输入 / 步进传 `number`。
|
|
17
|
+
*/
|
|
18
|
+
onChange?: (value: number | null) => void;
|
|
19
|
+
/** 最小值(antd `min` 并集)。 */
|
|
20
|
+
min?: number;
|
|
21
|
+
/** 最大值(antd `max` 并集)。 */
|
|
22
|
+
max?: number;
|
|
23
|
+
/**
|
|
24
|
+
* 步长(antd `step` 并集) — 增 / 减按钮以及键盘 ↑↓ 都按此步长。
|
|
25
|
+
* @default 1
|
|
26
|
+
*/
|
|
27
|
+
step?: number;
|
|
28
|
+
/**
|
|
29
|
+
* 数值精度(antd `precision` 并集) — 保留小数位数(超出会四舍五入)。
|
|
30
|
+
*/
|
|
31
|
+
precision?: number;
|
|
32
|
+
/**
|
|
33
|
+
* 千分位分隔展示(仅展示用,实际 value 仍是 number)。
|
|
34
|
+
* @default false
|
|
35
|
+
*/
|
|
36
|
+
groupSeparator?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* 是否显示右侧上下步进按钮(antd `controls` 并集) — `false` 时隐藏。
|
|
39
|
+
* @default true
|
|
40
|
+
*/
|
|
41
|
+
controls?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* 尺寸。
|
|
44
|
+
* @default "default"
|
|
45
|
+
*/
|
|
46
|
+
size?: 'sm' | 'default' | 'lg';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function format(
|
|
50
|
+
raw: number | null,
|
|
51
|
+
precision: number | undefined,
|
|
52
|
+
groupSeparator: boolean,
|
|
53
|
+
): string {
|
|
54
|
+
if (raw === null || Number.isNaN(raw)) return '';
|
|
55
|
+
const fixed = precision !== undefined ? raw.toFixed(precision) : String(raw);
|
|
56
|
+
if (!groupSeparator) return fixed;
|
|
57
|
+
const [intPart, decPart] = fixed.split('.');
|
|
58
|
+
const grouped = intPart!.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
59
|
+
return decPart !== undefined ? `${grouped}.${decPart}` : grouped;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const sizeMap = {
|
|
63
|
+
sm: 'h-8 text-xs',
|
|
64
|
+
default: 'h-9 text-sm',
|
|
65
|
+
lg: 'h-10 text-base',
|
|
66
|
+
} as const;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 数字输入框 — antd 独有补足。**等价 antd `InputNumber`**。区别于原生 `<input type="number">`:
|
|
70
|
+
* - 步进按钮可自定义显隐
|
|
71
|
+
* - 范围裁剪与 precision 由组件保证(超出 min/max 自动夹紧)
|
|
72
|
+
* - 千分位展示(仅 UI)
|
|
73
|
+
* - 支持空状态(value=null)
|
|
74
|
+
*/
|
|
75
|
+
const InputNumber = React.forwardRef<HTMLInputElement, InputNumberProps>(
|
|
76
|
+
(
|
|
77
|
+
{
|
|
78
|
+
value,
|
|
79
|
+
defaultValue,
|
|
80
|
+
onChange,
|
|
81
|
+
min,
|
|
82
|
+
max,
|
|
83
|
+
step = 1,
|
|
84
|
+
precision,
|
|
85
|
+
groupSeparator = false,
|
|
86
|
+
controls = true,
|
|
87
|
+
size = 'default',
|
|
88
|
+
disabled,
|
|
89
|
+
className,
|
|
90
|
+
onBlur,
|
|
91
|
+
onKeyDown,
|
|
92
|
+
...props
|
|
93
|
+
},
|
|
94
|
+
ref,
|
|
95
|
+
) => {
|
|
96
|
+
const isControlled = value !== undefined;
|
|
97
|
+
const [internal, setInternal] = React.useState<number | null>(
|
|
98
|
+
defaultValue ?? null,
|
|
99
|
+
);
|
|
100
|
+
const current = isControlled ? value! : internal;
|
|
101
|
+
const [editing, setEditing] = React.useState<string | null>(null);
|
|
102
|
+
|
|
103
|
+
const display = editing !== null ? editing : format(current, precision, groupSeparator);
|
|
104
|
+
|
|
105
|
+
const clamp = (n: number): number => {
|
|
106
|
+
let v = n;
|
|
107
|
+
if (typeof min === 'number') v = Math.max(min, v);
|
|
108
|
+
if (typeof max === 'number') v = Math.min(max, v);
|
|
109
|
+
if (typeof precision === 'number') v = Number(v.toFixed(precision));
|
|
110
|
+
return v;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const commit = (next: number | null) => {
|
|
114
|
+
if (!isControlled) setInternal(next);
|
|
115
|
+
onChange?.(next);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const handleInput = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
119
|
+
const raw = e.target.value;
|
|
120
|
+
// Allow free typing of intermediate states ("-", "1.").
|
|
121
|
+
setEditing(raw);
|
|
122
|
+
if (raw === '' || raw === '-' || raw === '.') return;
|
|
123
|
+
const parsed = Number(raw.replace(/,/g, ''));
|
|
124
|
+
if (!Number.isNaN(parsed)) commit(clamp(parsed));
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const handleBlur = (e: React.FocusEvent<HTMLInputElement>) => {
|
|
128
|
+
setEditing(null);
|
|
129
|
+
const raw = e.target.value;
|
|
130
|
+
if (raw === '' || raw === '-' || raw === '.') {
|
|
131
|
+
commit(null);
|
|
132
|
+
} else {
|
|
133
|
+
const parsed = Number(raw.replace(/,/g, ''));
|
|
134
|
+
commit(Number.isNaN(parsed) ? null : clamp(parsed));
|
|
135
|
+
}
|
|
136
|
+
onBlur?.(e);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const step1 = (sign: 1 | -1) => {
|
|
140
|
+
if (disabled) return;
|
|
141
|
+
const base = current ?? 0;
|
|
142
|
+
commit(clamp(base + sign * step));
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
146
|
+
if (e.key === 'ArrowUp') {
|
|
147
|
+
e.preventDefault();
|
|
148
|
+
step1(1);
|
|
149
|
+
} else if (e.key === 'ArrowDown') {
|
|
150
|
+
e.preventDefault();
|
|
151
|
+
step1(-1);
|
|
152
|
+
}
|
|
153
|
+
onKeyDown?.(e);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
return (
|
|
157
|
+
<div
|
|
158
|
+
className={cn(
|
|
159
|
+
'inline-flex items-stretch overflow-hidden rounded-md border border-input bg-background shadow-sm',
|
|
160
|
+
'focus-within:ring-1 focus-within:ring-ring',
|
|
161
|
+
disabled && 'cursor-not-allowed opacity-50',
|
|
162
|
+
className,
|
|
163
|
+
)}
|
|
164
|
+
>
|
|
165
|
+
<input
|
|
166
|
+
ref={ref}
|
|
167
|
+
type="text"
|
|
168
|
+
inputMode="decimal"
|
|
169
|
+
value={display}
|
|
170
|
+
disabled={disabled}
|
|
171
|
+
onChange={handleInput}
|
|
172
|
+
onBlur={handleBlur}
|
|
173
|
+
onKeyDown={handleKeyDown}
|
|
174
|
+
className={cn(
|
|
175
|
+
'w-full bg-transparent px-3 text-foreground placeholder:text-muted-foreground focus-visible:outline-none disabled:cursor-not-allowed',
|
|
176
|
+
sizeMap[size],
|
|
177
|
+
)}
|
|
178
|
+
{...props}
|
|
179
|
+
/>
|
|
180
|
+
{controls ? (
|
|
181
|
+
<div className="flex flex-col border-l">
|
|
182
|
+
<button
|
|
183
|
+
type="button"
|
|
184
|
+
tabIndex={-1}
|
|
185
|
+
disabled={disabled || (typeof max === 'number' && (current ?? -Infinity) >= max)}
|
|
186
|
+
onClick={() => step1(1)}
|
|
187
|
+
aria-label="增加"
|
|
188
|
+
className="flex h-1/2 items-center justify-center px-1.5 text-muted-foreground hover:bg-accent hover:text-foreground disabled:cursor-not-allowed disabled:opacity-30"
|
|
189
|
+
>
|
|
190
|
+
<ChevronUp className="size-3" />
|
|
191
|
+
</button>
|
|
192
|
+
<button
|
|
193
|
+
type="button"
|
|
194
|
+
tabIndex={-1}
|
|
195
|
+
disabled={disabled || (typeof min === 'number' && (current ?? Infinity) <= min)}
|
|
196
|
+
onClick={() => step1(-1)}
|
|
197
|
+
aria-label="减少"
|
|
198
|
+
className="flex h-1/2 items-center justify-center border-t px-1.5 text-muted-foreground hover:bg-accent hover:text-foreground disabled:cursor-not-allowed disabled:opacity-30"
|
|
199
|
+
>
|
|
200
|
+
<ChevronDown className="size-3" />
|
|
201
|
+
</button>
|
|
202
|
+
</div>
|
|
203
|
+
) : null}
|
|
204
|
+
</div>
|
|
205
|
+
);
|
|
206
|
+
},
|
|
207
|
+
);
|
|
208
|
+
InputNumber.displayName = 'InputNumber';
|
|
209
|
+
|
|
210
|
+
export { InputNumber };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: input-otp
|
|
3
|
+
name: InputOTP
|
|
4
|
+
type: component
|
|
5
|
+
category: form
|
|
6
|
+
since: 0.1.0
|
|
7
|
+
package: "@teamix-evo/ui"
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# InputOTP
|
|
11
|
+
|
|
12
|
+
OTP 验证码输入 — 基于 [`input-otp`](https://github.com/guilhermerodz/input-otp)。**shadcn-only**(antd 无对标组件)。
|
|
13
|
+
4~8 位短验证码常见场景:登录、二步验证、支付确认。
|
|
14
|
+
|
|
15
|
+
## When to use
|
|
16
|
+
|
|
17
|
+
- 短信 / 邮件验证码输入
|
|
18
|
+
- 二步验证(TOTP / 短信)
|
|
19
|
+
- 支付密码 / PIN
|
|
20
|
+
|
|
21
|
+
## When NOT to use
|
|
22
|
+
|
|
23
|
+
- 长密码输入 → `Input type="password"`
|
|
24
|
+
- 富文本 / 自由格式 → `Input` / `Textarea`
|
|
25
|
+
- 银行卡号 → `Input` + `format` 函数
|
|
26
|
+
|
|
27
|
+
## Props
|
|
28
|
+
|
|
29
|
+
> 以下表格由 `pnpm --filter @teamix-evo/ui gen:meta` 自动生成。`InputOTP` 透传 `input-otp` 的所有 props。
|
|
30
|
+
|
|
31
|
+
<!-- auto:props:begin -->
|
|
32
|
+
| 名称 | 类型 | 默认值 | 必填 | 说明 |
|
|
33
|
+
| --- | --- | --- | --- | --- |
|
|
34
|
+
| `className` | `string` | – | – | 输入框 className,作用在隐藏的真实 input 上;通常无需关心。 |
|
|
35
|
+
| `containerClassName` | `string` | – | – | 容器 className,作用在 slot 列表外层 wrapper 上, 用于覆盖默认的 `flex items-center gap-2`。 |
|
|
36
|
+
<!-- auto:props:end -->
|
|
37
|
+
|
|
38
|
+
## 依赖
|
|
39
|
+
|
|
40
|
+
> 以下表格由 `pnpm --filter @teamix-evo/ui gen:meta` 自动生成,数据源是 [`manifest.json`](../../../manifest.json)。**手工编辑 marker 之间的内容会在下次生成时被覆盖**。
|
|
41
|
+
|
|
42
|
+
<!-- auto:deps:begin -->
|
|
43
|
+
### 同库依赖
|
|
44
|
+
|
|
45
|
+
> `teamix-evo ui add input-otp` 时,以下 entry 会被自动连带安装(无需手动 add)。
|
|
46
|
+
|
|
47
|
+
| Entry | 类型 | 描述 |
|
|
48
|
+
| --- | --- | --- |
|
|
49
|
+
| `cn` | util | Tailwind className 合并工具(clsx + tailwind-merge) |
|
|
50
|
+
|
|
51
|
+
### npm 依赖
|
|
52
|
+
|
|
53
|
+
> 业务侧需要先 `pnpm add` / `npm install` 这些包。CLI 在 `ui add` 完成后会列出此提示。
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pnpm add input-otp@^1.2.0 lucide-react@^0.460.0
|
|
57
|
+
```
|
|
58
|
+
<!-- auto:deps:end -->
|
|
59
|
+
|
|
60
|
+
> 子组件:`InputOTP`(Root)/ `InputOTPGroup`(slot 容器)/ `InputOTPSlot`(单 slot,需 `index`)/ `InputOTPSeparator`(分隔符,如 4-4 数字之间的点)。
|
|
61
|
+
|
|
62
|
+
## AI 生成纪律
|
|
63
|
+
|
|
64
|
+
- **`maxLength` 必传**:声明位数(常见 4 / 6 / 8)
|
|
65
|
+
- **每个 Slot 必传 `index`**:0-based,与 maxLength 对应
|
|
66
|
+
- **`pattern` 限制字符类**:数字 OTP 用 `pattern={REGEXP_ONLY_DIGITS}`(从 `input-otp` 导入),避免用户粘贴混合字符
|
|
67
|
+
- **多 Group 用 Separator**:6 位时分 3-3 或 4-2 提升可读性
|
|
68
|
+
- **复制粘贴自带**:input-otp 自动处理,不需要再监听 paste
|
|
69
|
+
|
|
70
|
+
## Examples
|
|
71
|
+
|
|
72
|
+
```tsx
|
|
73
|
+
import {
|
|
74
|
+
InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator,
|
|
75
|
+
} from '@/components/ui/input-otp';
|
|
76
|
+
import { REGEXP_ONLY_DIGITS } from 'input-otp';
|
|
77
|
+
|
|
78
|
+
// 6 位
|
|
79
|
+
<InputOTP maxLength={6} pattern={REGEXP_ONLY_DIGITS}>
|
|
80
|
+
<InputOTPGroup>
|
|
81
|
+
{[0, 1, 2, 3, 4, 5].map((i) => <InputOTPSlot key={i} index={i} />)}
|
|
82
|
+
</InputOTPGroup>
|
|
83
|
+
</InputOTP>
|
|
84
|
+
|
|
85
|
+
// 6 位,3-3 分组
|
|
86
|
+
<InputOTP maxLength={6} pattern={REGEXP_ONLY_DIGITS}>
|
|
87
|
+
<InputOTPGroup>
|
|
88
|
+
<InputOTPSlot index={0} />
|
|
89
|
+
<InputOTPSlot index={1} />
|
|
90
|
+
<InputOTPSlot index={2} />
|
|
91
|
+
</InputOTPGroup>
|
|
92
|
+
<InputOTPSeparator />
|
|
93
|
+
<InputOTPGroup>
|
|
94
|
+
<InputOTPSlot index={3} />
|
|
95
|
+
<InputOTPSlot index={4} />
|
|
96
|
+
<InputOTPSlot index={5} />
|
|
97
|
+
</InputOTPGroup>
|
|
98
|
+
</InputOTP>
|
|
99
|
+
|
|
100
|
+
// 受控
|
|
101
|
+
const [v, setV] = React.useState('');
|
|
102
|
+
<InputOTP maxLength={6} value={v} onChange={setV}>
|
|
103
|
+
<InputOTPGroup>...</InputOTPGroup>
|
|
104
|
+
</InputOTP>
|
|
105
|
+
```
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
3
|
+
import { REGEXP_ONLY_DIGITS } from 'input-otp';
|
|
4
|
+
import {
|
|
5
|
+
InputOTP,
|
|
6
|
+
InputOTPGroup,
|
|
7
|
+
InputOTPSlot,
|
|
8
|
+
InputOTPSeparator,
|
|
9
|
+
} from './input-otp';
|
|
10
|
+
|
|
11
|
+
const meta: Meta<typeof InputOTP> = {
|
|
12
|
+
title: '表单与输入 · Form/InputOTP',
|
|
13
|
+
component: InputOTP,
|
|
14
|
+
tags: ['autodocs'],
|
|
15
|
+
parameters: {
|
|
16
|
+
docs: {
|
|
17
|
+
description: {
|
|
18
|
+
component:
|
|
19
|
+
'一次性密码输入 — 短信 / 邮箱验证码场景的格式化输入,每位字符独立可视 slot。基于 [`input-otp`](https://input-otp.rodz.dev/) 实现,自动处理粘贴、键盘前后切换、IME 与 SMS autofill;通过 `pattern` 限制输入字符集(数字 / 字母数字)。shadcn 专有能力,填补 antd 未提供的验证码输入场景。视觉走 OpenTrek semantic tokens,所有样式来自 `@teamix-evo/design`,无 mock。',
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export default meta;
|
|
26
|
+
type Story = StoryObj<typeof InputOTP>;
|
|
27
|
+
|
|
28
|
+
export const SixDigits: Story = {
|
|
29
|
+
render: () => {
|
|
30
|
+
const [v, setV] = React.useState('');
|
|
31
|
+
return (
|
|
32
|
+
<InputOTP
|
|
33
|
+
maxLength={6}
|
|
34
|
+
pattern={REGEXP_ONLY_DIGITS}
|
|
35
|
+
value={v}
|
|
36
|
+
onChange={setV}
|
|
37
|
+
>
|
|
38
|
+
<InputOTPGroup>
|
|
39
|
+
{[0, 1, 2, 3, 4, 5].map((i) => (
|
|
40
|
+
<InputOTPSlot key={i} index={i} />
|
|
41
|
+
))}
|
|
42
|
+
</InputOTPGroup>
|
|
43
|
+
</InputOTP>
|
|
44
|
+
);
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const Grouped: Story = {
|
|
49
|
+
parameters: { controls: { disable: true } },
|
|
50
|
+
render: () => (
|
|
51
|
+
<InputOTP maxLength={6} pattern={REGEXP_ONLY_DIGITS}>
|
|
52
|
+
<InputOTPGroup>
|
|
53
|
+
<InputOTPSlot index={0} />
|
|
54
|
+
<InputOTPSlot index={1} />
|
|
55
|
+
<InputOTPSlot index={2} />
|
|
56
|
+
</InputOTPGroup>
|
|
57
|
+
<InputOTPSeparator />
|
|
58
|
+
<InputOTPGroup>
|
|
59
|
+
<InputOTPSlot index={3} />
|
|
60
|
+
<InputOTPSlot index={4} />
|
|
61
|
+
<InputOTPSlot index={5} />
|
|
62
|
+
</InputOTPGroup>
|
|
63
|
+
</InputOTP>
|
|
64
|
+
),
|
|
65
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { OTPInput, OTPInputContext } from 'input-otp';
|
|
3
|
+
import { Dot } from 'lucide-react';
|
|
4
|
+
|
|
5
|
+
import { cn } from '@/utils/cn';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* InputOTP 自身的可定制属性。底层 `OTPInput` 的所有 props
|
|
9
|
+
* (`maxLength` / `pattern` / `value` / `onChange` / `onComplete` /
|
|
10
|
+
* `children` / `render` / ...)由 input-otp 透传。
|
|
11
|
+
*/
|
|
12
|
+
export interface InputOTPProps {
|
|
13
|
+
/**
|
|
14
|
+
* 输入框 className,作用在隐藏的真实 input 上;通常无需关心。
|
|
15
|
+
*/
|
|
16
|
+
className?: string;
|
|
17
|
+
/**
|
|
18
|
+
* 容器 className,作用在 slot 列表外层 wrapper 上,
|
|
19
|
+
* 用于覆盖默认的 `flex items-center gap-2`。
|
|
20
|
+
*/
|
|
21
|
+
containerClassName?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function InputOTP({
|
|
25
|
+
className,
|
|
26
|
+
containerClassName,
|
|
27
|
+
...props
|
|
28
|
+
}: InputOTPProps & React.ComponentPropsWithoutRef<typeof OTPInput>) {
|
|
29
|
+
return (
|
|
30
|
+
<OTPInput
|
|
31
|
+
containerClassName={cn(
|
|
32
|
+
'flex items-center gap-2 has-[:disabled]:opacity-50',
|
|
33
|
+
containerClassName,
|
|
34
|
+
)}
|
|
35
|
+
className={cn('disabled:cursor-not-allowed', className)}
|
|
36
|
+
{...props}
|
|
37
|
+
/>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
InputOTP.displayName = 'InputOTP';
|
|
41
|
+
|
|
42
|
+
const InputOTPGroup = React.forwardRef<
|
|
43
|
+
React.ElementRef<'div'>,
|
|
44
|
+
React.ComponentPropsWithoutRef<'div'>
|
|
45
|
+
>(({ className, ...props }, ref) => (
|
|
46
|
+
<div ref={ref} className={cn('flex items-center', className)} {...props} />
|
|
47
|
+
));
|
|
48
|
+
InputOTPGroup.displayName = 'InputOTPGroup';
|
|
49
|
+
|
|
50
|
+
export interface InputOTPSlotProps
|
|
51
|
+
extends React.ComponentPropsWithoutRef<'div'> {
|
|
52
|
+
/** Slot 索引(必传)。OTP 的 slots 数组下标。 */
|
|
53
|
+
index: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const InputOTPSlot = React.forwardRef<
|
|
57
|
+
React.ElementRef<'div'>,
|
|
58
|
+
InputOTPSlotProps
|
|
59
|
+
>(({ index, className, ...props }, ref) => {
|
|
60
|
+
const inputOTPContext = React.useContext(OTPInputContext);
|
|
61
|
+
const slot = inputOTPContext.slots[index];
|
|
62
|
+
const char = slot?.char;
|
|
63
|
+
const hasFakeCaret = slot?.hasFakeCaret;
|
|
64
|
+
const isActive = slot?.isActive;
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<div
|
|
68
|
+
ref={ref}
|
|
69
|
+
className={cn(
|
|
70
|
+
'relative flex size-10 items-center justify-center border-y border-r border-input text-sm shadow-sm transition-all first:rounded-l-md first:border-l last:rounded-r-md',
|
|
71
|
+
isActive && 'z-10 ring-1 ring-ring',
|
|
72
|
+
className,
|
|
73
|
+
)}
|
|
74
|
+
{...props}
|
|
75
|
+
>
|
|
76
|
+
{char}
|
|
77
|
+
{hasFakeCaret ? (
|
|
78
|
+
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
|
|
79
|
+
<div className="h-4 w-px animate-caret-blink bg-foreground duration-1000" />
|
|
80
|
+
</div>
|
|
81
|
+
) : null}
|
|
82
|
+
</div>
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
InputOTPSlot.displayName = 'InputOTPSlot';
|
|
86
|
+
|
|
87
|
+
const InputOTPSeparator = React.forwardRef<
|
|
88
|
+
React.ElementRef<'div'>,
|
|
89
|
+
React.ComponentPropsWithoutRef<'div'>
|
|
90
|
+
>(({ ...props }, ref) => (
|
|
91
|
+
<div ref={ref} role="separator" {...props}>
|
|
92
|
+
<Dot className="size-4" />
|
|
93
|
+
</div>
|
|
94
|
+
));
|
|
95
|
+
InputOTPSeparator.displayName = 'InputOTPSeparator';
|
|
96
|
+
|
|
97
|
+
export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator };
|