diff --git a/apps/docs/.vitepress/theme/navigation.ts b/apps/docs/.vitepress/theme/navigation.ts index 7cb6346..af81a9e 100644 --- a/apps/docs/.vitepress/theme/navigation.ts +++ b/apps/docs/.vitepress/theme/navigation.ts @@ -95,6 +95,11 @@ export const componentItems: DocsItem[] = [ url: "/components/color-area", category: "colors", }, + { + title: "ColorField 颜色输入框", + url: "/components/color-field", + category: "colors", + }, { title: "ColorSlider 颜色滑块", url: "/components/color-slider", diff --git a/apps/docs/components/color-area.md b/apps/docs/components/color-area.md index ef8b5a1..baf5273 100644 --- a/apps/docs/components/color-area.md +++ b/apps/docs/components/color-area.md @@ -178,4 +178,4 @@ Vue 版写作 `ColorAreaThumb`(对应上游 `ColorArea.Thumb`)。 ## 相关组件 -本批次公开 ColorArea 与 ColorSwatch。ColorField、ColorInputGroup、ColorPicker、ColorSlider 与 ColorSwatchPicker 仍为私有颜色协议,尚未提供公共页面。 +已公开 [ColorField 颜色输入框](/components/color-field)(含 ColorInputGroup)、[ColorArea 颜色区域](/components/color-area)、[ColorSlider 颜色滑块](/components/color-slider) 与 [ColorSwatch 颜色色块](/components/color-swatch)。ColorPicker 与 ColorSwatchPicker 仍未公开。 diff --git a/apps/docs/components/color-field.md b/apps/docs/components/color-field.md new file mode 100644 index 0000000..b69a01c --- /dev/null +++ b/apps/docs/components/color-field.md @@ -0,0 +1,288 @@ +--- +title: ColorField 颜色输入框 +description: 支持 Hex 与颜色通道编辑的颜色输入字段,支持标签、描述与验证 +component: color-field +group: 组件 +--- + +## 用法 + +```vue + + + + 颜色 + + + + + + + 选择你的颜色 + + + +``` + + + +## 组件结构 + +```vue + + + + 颜色 + + + + + + + 选择你的颜色 + + + +``` + +> **ColorField** 将标签、颜色输入、描述与错误合并为单个无障碍组件。 + +## 示例 + +### 变体 + +ColorFieldGroup 组件支持两种视觉变体: + +- **`primary`**(默认)- 标准样式带阴影,适用于大多数场景 +- **`secondary`** - 低强调变体无阴影,适用于 Surface 组件内 + + + +### 表面样式 + +在 [Surface](/components/surface) 组件内使用时,在 ColorFieldGroup 上使用 `variant="secondary"` 以应用适合 Surface 背景的低强调变体。 + + + +### 带描述 + + + +### 必填字段 + + + +### 禁用 + + + +### 宽度充满 + + + +### 表单校验 + +将 `isInvalid` 与 `FieldError` 一起使用以显示验证消息。 + + + +### 分量编辑 + +ColorField 支持通过设置 `colorSpace` 与 `channel` 属性编辑单个颜色通道(hue、saturation、lightness、red、green、blue、alpha)。 + + + +### 受控组件 + +控制值以与其他组件或状态管理同步。 + + + +### 表单示例 + +包含验证与提交处理的完整表单示例。 + + + +### 渲染函数 + +上游 `render` 的元素替换使用 `as` 和透传属性实现;Vue 默认作用域插槽提供实时字段状态,`styleFn` 可依据状态计算样式。 + + + +## 自定义样式 + +### Tailwind CSS + + + +### 全局 CSS + +ColorField 默认样式较少。覆盖 `.hv-color-field` 类以自定义容器样式。 + +```css +@layer components { + .hv-color-field { + @apply flex flex-col gap-1; + + &[data-invalid="true"], + &[aria-invalid="true"] { + [data-slot="description"] { + @apply hidden; + } + } + + [data-slot="label"] { + @apply w-fit; + } + + [data-slot="description"] { + @apply px-1; + } + } +} +``` + +## 样式参考 + +HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。 + +### CSS 类 + +- `.hv-color-field` – 最小样式的根容器(`flex flex-col gap-1`) + +> **Note:** 子组件([Label](/components/label)、[Description](/components/description)、[FieldError](/components/field-error))有各自的 CSS 类与样式。请参阅各自文档了解自定义选项。ColorFieldGroup 样式见下方 API 参考。 + +### 交互状态 + +ColorField 根据状态自动管理以下 data 属性: + +- **Invalid**:`[data-invalid="true"]` 或 `[aria-invalid="true"]` - 无效时自动隐藏 description slot +- **Required**:`[data-required="true"]` - `isRequired` 为 true 时应用 +- **Disabled**:`[data-disabled="true"]` - `isDisabled` 为 true 时应用 +- **Focus Within**:`[data-focus-within="true"]` - 任一子 input 聚焦时应用 + +## API 参考 + +### ColorField + +`ColorFieldRoot` 是 `ColorField` 的别名;Vue 使用具名部件与 `v-model`,颜色复用本库 `Color`。不声明继承全部 React Aria 属性。 + +| Prop / 事件 | 类型 | 默认值 | 描述 | +| -------------------------------- | ------------------------------------------ | ---------------------- | ----------------------------------------------------------------------------------- | +| `modelValue` / `v-model` | `ColorValue \| null` | - | 受控接受值;ColorValue 为本库 Color 或颜色字符串 | +| `value` | `ColorValue \| null` | - | 低优先级受控别名 | +| `defaultValue` | `ColorValue \| null` | `null` | 非受控初始值,捕获一次 | +| `channel` | `ColorChannel` | - | 未指定时编辑 Hex;否则编辑 hue/saturation/lightness/brightness/red/green/blue/alpha | +| `colorSpace` | `"rgb" \| "hsl" \| "hsb"` | 推断 | 通道使用空间,见下方说明 | +| `fullWidth` | `boolean` | `false` | 根容器占满宽度 | +| `isRequired` | `boolean` | `false` | 必填;原生或 ARIA 校验 | +| `isInvalid` | `boolean` | `false` | 外部错误状态 | +| `validate` | `(value: Color \| null) => ValidateResult` | - | 返回 true/null/undefined 表示有效,false/字符串/字符串数组表示无效 | +| `validationBehavior` | `"native" \| "aria"` | `"native"` / Form | 原生约束阻止提交,或仅提供 ARIA 错误 | +| `isDisabled` / `disabled` | `boolean` | `false` | 禁用;disabled 是本库别名 | +| `isReadOnly` | `boolean` | `false` | 可聚焦、可选中,不能编辑 | +| `isWheelDisabled` | `boolean` | `false` | 禁止滚轮调整 | +| `name` / `form` | `string` | - | 原生提交名 / 外部表单 id | +| `id` | `string` | 自动生成 | 分配给真实 Input;Input 自身 id 优先 | +| `autoFocus` / `autoComplete` | `boolean` / `string` | `false` / `"off"` | 自动聚焦 / 原生自动完成 | +| `locale` | `string` | ConfigProvider / en-US | 通道数字与单位格式 | +| `as` | `string` | `"div"` | 更换根标签;传入 data-*、class、原生属性支持自定义根元素 | +| `style` / `styleFn` | `StyleValue` 或状态函数 / 状态函数 | - | styleFn 在 style 之后合并;class 使用 Vue class 绑定 | +| `@update:modelValue` / `@change` | `(color: Color \| null) => void` | - | 提交颜色提案;父级可以拒绝;不支持 changeEnd | + +`modelValue`(包括显式 null)优先于 `value`,受控值优先于 `defaultValue`。全部自身值属性省略时继承最近颜色 owner,这是本库组合扩展;显式 null 清空独立字段。父级拒绝提案后恢复接受值,并允许同一提案再次输入。 + +无通道时只接受 3/6 位 Hex,可省略 #,显示规范化大写 Hex;解析格式范围不等于字段文本格式范围。颜色解析错误按公共 parseColor 边界抛错。空文本提交 null,非法/不完整草稿失焦后恢复;Enter 提交草稿并保留原生表单提交,Esc 撤销草稿。IME 候选确认期间不提交;此处只有合成 composition 回归与桌面输入检查,不代表完整系统输入法验收。 + +通道模式使用现有通道解析:RGB 通道用 rgb、lightness 用 hsl、brightness 用 hsb;hue/saturation 在显式 rgb 时转 hsl,其他情况保留指定或接受值的 HSL/HSB 空间,alpha 保留原空间。与固定依赖可能要求显式空间不同,本库保留既有推断/纠正扩展;Props 分别约束 channel 和 colorSpace 的枚举,不额外声明二者判别联合。 + +通道文本显示本地化数值和单位:hue 带 °,百分比带 %。自然步长为 RGB/hue/百分比 1,alpha 模型步长 .01;输入按自然步长取整并按范围夹限。未编辑的格式化显示不会反写完整颜色精度。上/下方向键单步,Shift 或 PageUp/Down 使用通道页步长,Home/End 到边界;Hex 对应 24 位整数步长 1/10。获得焦点后纵向滚轮向下递增,向上递减;isWheelDisabled、禁用、只读和 preventDefault 阻止调整。 + +### 表单提交与验证 + +Hex 使用命名的可见文本 input:直接构造 `FormData` 会读到编辑中的原生草稿;通常 Enter/失焦先提交或恢复草稿,再由表单提交。此行为保留固定字段协议,不能把编辑中 FormData 称为接受颜色。通道模式只命名一个隐藏 input,提交接受通道数值:hue/RGB 为自然数值,saturation/lightness/brightness 和 alpha 为 0–1。通道显示与提交采用自然步长的舍入值,完整颜色对象仍保留未修改精度。 + +原生必填、validate、Form validationErrors、FieldError、外部 form 和 reset 复用共享字段设施;禁用字段不参与提交,只读保留提交。校验判断颜色接受值;单独读取 FormData 不会主动运行校验。受控 reset 不强制父级改变模型,会把实际 DOM 恢复为当前接受值;非受控 reset 恢复捕获初值。ColorField 不额外提交整个 owner 颜色,Picker 私有组合的完整颜色提交由 owner 负责。 + +### Composition Components + +| Vue 部件 | 对应上游 | 属性与行为 | +| -------------------------------------- | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | +| `ColorFieldGroup` | ColorField.Group | 同 ColorInputGroup/ColorInputGroupRoot;variant primary(默认)/secondary、fullWidth、as、role、style/styleFn;空白点击聚焦 Input,交互前后缀保留焦点 | +| `ColorFieldInput` | ColorField.Input | 同 ColorInputGroupInput;原生 input 属性、事件、class/style;Root 内绑定颜色草稿,不使用另一个文本 v-model | +| `ColorFieldPrefix` | ColorField.Prefix | 同 ColorInputGroupPrefix;as 默认 div,其余原生属性与默认插槽 | +| `ColorFieldSuffix` | ColorField.Suffix | 同 ColorInputGroupSuffix;as 默认 div,其余原生属性与默认插槽 | +| `Label` / `Description` / `FieldError` | 同名组件 | 关联真实输入的 id、描述与错误;无需另建字段状态 | + +一个 ColorField 支持一个 Input;重复挂载 Input 抛错。没有 Input 时根和内容仍可渲染,但没有可编辑/可聚焦的字段。普通 DOM/Fragment 和已知 Group 的插槽可预取输入元数据;未知组件包装不执行 setup/render 探测,其 Input 挂载后注册属性,异步包装应显式传入 Root 的 id/name/form 等公共属性。 + +ColorInputGroup 独立使用时是普通文本组(role=group);Input 可用字符串 v-model/defaultValue。字段内 Group 默认 role=presentation。Root 的 ARIA 和输入事件发给真实 Input,普通 data-*、class/style 发给根;Input 事件与 Root 事件合并,外部取消先于内部行为。原生事件遵循冒泡规则。`aria-label/labelledby/describedby/details`、Label 和动态辅助文本保留关联。可通过 ConfigProvider 的 dir 或 HTML dir 继承 RTL 布局。 + +### Render Props + +| 插槽状态 | 类型 | 含义 | +| -------------------------------------------------------- | --------------- | --------------------------------- | +| `value` | `Color \| null` | 当前接受颜色 | +| `isDisabled` / `isReadOnly` / `isRequired` / `isInvalid` | `boolean` | 实际字段状态 | +| `isFocused` / `isFocusWithin` / `isFocusVisible` | `boolean` | 真实 Input/子元素焦点及焦点可见性 | +| `validationErrors` | `string[]` | 当前可见错误 | + +Group 默认插槽与 styleFn 提供 isDisabled/isInvalid/isFocusWithin/isHovered。Root 实例提供 `focus()`、`element`(真实 Input)与 `color`(接受值);Group 提供 element(根 DOM)与 focus(),Input 提供 element、focus()、select()。不暴露私有 owner、事务 token 或整个字段控制器。不提供 asChild;上游 render 换标签由 as/attrs 实现。 + +### Color Types + +```ts +import { + parseColor, + type Color, + type ColorFieldProps, +} from "@heroui-vue/components"; +const color: Color = parseColor("#3B82F6"); +const props: ColorFieldProps = { defaultValue: color, name: "color" }; +color.toString("hex"); +color.getChannelValue("red"); +``` + +本页部件和 Props,以及 `ColorFieldRenderProps`、`ColorFieldRenderState`、`ColorFieldSlots`、`ColorFieldExpose`、`colorFieldVariants`、`colorInputGroupVariants`、`ColorFieldVariants`、`ColorInputGroupVariants` 均从包根公开。ColorInputGroup 与 ColorField 的对应部件共享实现。ColorPicker/ColorSwatchPicker 尚未公开。 + +## ColorField.Group Styling + +使用 `.hv-color-input-group`、`__input`、`__prefix`、`__suffix` 定制各节点;primary/secondary/full-width 修饰符与上游视觉数值一致。根容器为 `.hv-color-field`。Group 负责 hover/focus-within/invalid/disabled 的样式,Input 自身去掉默认边框和焦点轮廓;焦点环展示在 Group。状态必须写在实际匹配 CSS 的节点。 + +```css +@layer components { + .hv-color-input-group { + border-radius: var(--radius-field); + } + .hv-color-input-group__input { + font-variant-numeric: tabular-nums; + } +} +``` + +## 相关组件 + +[ColorArea 颜色区域](/components/color-area)、[ColorSlider 颜色滑块](/components/color-slider)、[ColorSwatch 颜色色块](/components/color-swatch)。 diff --git a/apps/docs/components/color-slider.md b/apps/docs/components/color-slider.md index a6d3678..2b5a167 100644 --- a/apps/docs/components/color-slider.md +++ b/apps/docs/components/color-slider.md @@ -281,4 +281,4 @@ ColorSlider 实现 ARIA slider 模式,提供: ## 相关组件 -本批次公开 ColorArea、ColorSwatch 与 ColorSlider。ColorField、ColorInputGroup、ColorPicker 与 ColorSwatchPicker 仍为私有颜色协议,尚未提供公共页面。 +已公开 [ColorField 颜色输入框](/components/color-field)(含 ColorInputGroup)、[ColorArea 颜色区域](/components/color-area)、[ColorSlider 颜色滑块](/components/color-slider) 与 [ColorSwatch 颜色色块](/components/color-swatch)。ColorPicker 与 ColorSwatchPicker 仍未公开。 diff --git a/apps/docs/components/color-swatch.md b/apps/docs/components/color-swatch.md index bdcfca2..f1d026a 100644 --- a/apps/docs/components/color-swatch.md +++ b/apps/docs/components/color-swatch.md @@ -152,4 +152,4 @@ ColorSwatch 组件使用以下 CSS 类([查看源码样式](https://github.com ## 相关组件 -本批次公开 ColorArea 与 ColorSwatch。ColorField、ColorInputGroup、ColorPicker、ColorSlider 与 ColorSwatchPicker 仍为私有颜色协议,尚未提供公共页面。 +已公开 [ColorField 颜色输入框](/components/color-field)(含 ColorInputGroup)、[ColorArea 颜色区域](/components/color-area)、[ColorSlider 颜色滑块](/components/color-slider) 与 [ColorSwatch 颜色色块](/components/color-swatch)。ColorPicker 与 ColorSwatchPicker 仍未公开。 diff --git a/apps/docs/demos/color-field-basic.vue b/apps/docs/demos/color-field-basic.vue new file mode 100644 index 0000000..c386882 --- /dev/null +++ b/apps/docs/demos/color-field-basic.vue @@ -0,0 +1,25 @@ + + + + 颜色 + + + + + + + + diff --git a/apps/docs/demos/color-field-channel-editing.vue b/apps/docs/demos/color-field-channel-editing.vue new file mode 100644 index 0000000..3f8282d --- /dev/null +++ b/apps/docs/demos/color-field-channel-editing.vue @@ -0,0 +1,69 @@ + + + + 分别编辑 HSL 通道: + + + 色相 + + + + + + 饱和度 + + + + % + + + + + 明度 + + + + % + + + + + + + + 当前:{{ color ? color.toString("hex") : "(空)" }} + + + + diff --git a/apps/docs/demos/color-field-controlled.vue b/apps/docs/demos/color-field-controlled.vue new file mode 100644 index 0000000..86fd14b --- /dev/null +++ b/apps/docs/demos/color-field-controlled.vue @@ -0,0 +1,41 @@ + + + + + 颜色 + + + + + + + + 当前值:{{ value ? value.toString("hex") : "(空)" }} + + + + + 设为红色 + + + 设为绿色 + + 清空 + + + diff --git a/apps/docs/demos/color-field-custom-styles.vue b/apps/docs/demos/color-field-custom-styles.vue new file mode 100644 index 0000000..687bc3a --- /dev/null +++ b/apps/docs/demos/color-field-custom-styles.vue @@ -0,0 +1,36 @@ + + + + 强调色 + 应用于按钮、链接和焦点环。 + + + + + + + + diff --git a/apps/docs/demos/color-field-disabled.vue b/apps/docs/demos/color-field-disabled.vue new file mode 100644 index 0000000..52ebb8e --- /dev/null +++ b/apps/docs/demos/color-field-disabled.vue @@ -0,0 +1,32 @@ + + + + + 颜色 + + + + 该颜色字段已禁用 + + + 颜色 + + + + 该颜色字段已禁用 + + + diff --git a/apps/docs/demos/color-field-form-example.vue b/apps/docs/demos/color-field-form-example.vue new file mode 100644 index 0000000..bdb5856 --- /dev/null +++ b/apps/docs/demos/color-field-form-example.vue @@ -0,0 +1,57 @@ + + + + + 品牌色 + + + + + + + 选择品牌主色 + + + {{ isSubmitting ? "保存中…" : "保存颜色" }} + + + diff --git a/apps/docs/demos/color-field-full-width.vue b/apps/docs/demos/color-field-full-width.vue new file mode 100644 index 0000000..683716e --- /dev/null +++ b/apps/docs/demos/color-field-full-width.vue @@ -0,0 +1,24 @@ + + + + + 品牌色 + + + + + + 主题色 + + + + + + diff --git a/apps/docs/demos/color-field-invalid.vue b/apps/docs/demos/color-field-invalid.vue new file mode 100644 index 0000000..0720321 --- /dev/null +++ b/apps/docs/demos/color-field-invalid.vue @@ -0,0 +1,27 @@ + + + + + 颜色 + + + + 请输入有效的十六进制颜色 + + + 背景色 + + + + 颜色格式无效,请使用十六进制(例如 #FF5733) + + + diff --git a/apps/docs/demos/color-field-on-surface.vue b/apps/docs/demos/color-field-on-surface.vue new file mode 100644 index 0000000..1112a02 --- /dev/null +++ b/apps/docs/demos/color-field-on-surface.vue @@ -0,0 +1,21 @@ + + + + + 主题色 + + + + 选择你的主题色 + + + diff --git a/apps/docs/demos/color-field-render-function.vue b/apps/docs/demos/color-field-render-function.vue new file mode 100644 index 0000000..26e7280 --- /dev/null +++ b/apps/docs/demos/color-field-render-function.vue @@ -0,0 +1,36 @@ + + + + 颜色 + + + + + + + + diff --git a/apps/docs/demos/color-field-required.vue b/apps/docs/demos/color-field-required.vue new file mode 100644 index 0000000..7db6e7f --- /dev/null +++ b/apps/docs/demos/color-field-required.vue @@ -0,0 +1,26 @@ + + + + + 品牌色 + + + + + + 主题色 + + + + 必填项 + + + diff --git a/apps/docs/demos/color-field-variants.vue b/apps/docs/demos/color-field-variants.vue new file mode 100644 index 0000000..d3ee410 --- /dev/null +++ b/apps/docs/demos/color-field-variants.vue @@ -0,0 +1,28 @@ + + + + + 主要变体 + + + + + + 次要变体 + + + + + + diff --git a/apps/docs/demos/color-field-with-description.vue b/apps/docs/demos/color-field-with-description.vue new file mode 100644 index 0000000..b9f89d0 --- /dev/null +++ b/apps/docs/demos/color-field-with-description.vue @@ -0,0 +1,27 @@ + + + + + 主色 + + + + 输入品牌主色 + + + 强调色 + + + + 用于高亮与行动按钮 + + + diff --git a/apps/playground/src/ColorFieldProtocolLab.vue b/apps/playground/src/ColorFieldProtocolLab.vue index 3db8d3c..4a2629d 100644 --- a/apps/playground/src/ColorFieldProtocolLab.vue +++ b/apps/playground/src/ColorFieldProtocolLab.vue @@ -1,14 +1,19 @@ - + 颜色字段正式协议 · 内部验收 - 组合部件、原生关联、格式化通道、提交和共享颜色;公开导出与正式文档尚未接入。 + 组合部件、原生关联、格式化通道、提交和共享颜色;本页通过公共导出验证正式字段;Picker + 保留内部组合夹具。 + 切换 RTL 受控 Hex / 原生 Enter @@ -49,6 +56,25 @@ function submit(event: Event, channel = false) { 接受值:{{ value?.toString("hex") ?? "empty" }} + + 受控拒绝与重试 + + + 拒绝颜色 + + + 重置拒绝字段 + 提案次数:{{ proposals }} + + + 只读颜色 + + + 百分数显示 / 小数提交 diff --git a/packages/styles/components/color-input-group.css b/packages/styles/components/color-input-group.css index 5c97f5a..ab335cf 100644 --- a/packages/styles/components/color-input-group.css +++ b/packages/styles/components/color-input-group.css @@ -53,7 +53,8 @@ ========================================================================== */ .hv-color-input-group__input { - @apply flex h-full flex-1 cursor-text items-center rounded-none border-0 bg-transparent px-3 py-2 text-base shadow-none outline-none sm:text-sm; + /* Vue native input has a browser intrinsic minimum width; keep narrow channel inputs and affixes inside the group. */ + @apply flex min-w-0 h-full flex-1 cursor-text items-center rounded-none border-0 bg-transparent px-3 py-2 text-base shadow-none outline-none sm:text-sm; /* Placeholder styling */ &::placeholder { diff --git a/packages/styles/src/index.ts b/packages/styles/src/index.ts index ab31fcb..364cf2f 100644 --- a/packages/styles/src/index.ts +++ b/packages/styles/src/index.ts @@ -1902,3 +1902,33 @@ export const rangeCalendarVariants = tv({ }); export type RangeCalendarVariants = VariantProps; + +/** HeroUI 3.2.6 / ce90ea3, Apache-2.0; hv namespace only. */ +export const colorFieldVariants = tv({ + base: "hv-color-field", + defaultVariants: { fullWidth: false }, + variants: { fullWidth: { false: "", true: "hv-color-field--full-width" } }, +}); +export type ColorFieldVariants = VariantProps; +export const colorInputGroupVariants = tv({ + defaultVariants: { fullWidth: false, variant: "primary" }, + slots: { + base: "hv-color-input-group", + input: "hv-color-input-group__input", + prefix: "hv-color-input-group__prefix", + suffix: "hv-color-input-group__suffix", + }, + variants: { + fullWidth: { + false: {}, + true: { base: "hv-color-input-group--full-width" }, + }, + variant: { + primary: { base: "hv-color-input-group--primary" }, + secondary: { base: "hv-color-input-group--secondary" }, + }, + }, +}); +export type ColorInputGroupVariants = VariantProps< + typeof colorInputGroupVariants +>; diff --git a/packages/vue/src/components/color-field/ColorField.vue b/packages/vue/src/components/color-field/ColorField.vue index ab7211c..8061de1 100644 --- a/packages/vue/src/components/color-field/ColorField.vue +++ b/packages/vue/src/components/color-field/ColorField.vue @@ -7,6 +7,7 @@ import { type VNodeChild, type VNode, } from "vue"; +import { colorFieldVariants } from "@heroui-vue/styles"; import { useColorField } from "./useColorField"; import type { ColorFieldProps, ColorFieldRenderState } from "./context"; import type { Color } from "../color/value"; @@ -45,6 +46,10 @@ const rendered = defineComponent({ props: ["nodes"], setup: (p) => () => p.nodes as VNodeChild, }); +const ordinaryStyle = computed(() => + typeof props.style === "function" ? props.style(state.value) : props.style, +); +const computedStyle = computed(() => props.styleFn?.(state.value)); const nativeEvents = new Set([ "aria-label", "aria-labelledby", @@ -66,7 +71,9 @@ const nativeEvents = new Set([ ]); const rootAttrs = () => Object.fromEntries( - Object.entries(attrs).filter(([key]) => !nativeEvents.has(key)), + Object.entries(attrs).filter( + ([key]) => !key.startsWith("aria-") && !nativeEvents.has(key), + ), ); function focusin(event: FocusEvent) { focusWithin.value = true; @@ -83,6 +90,7 @@ function focusout(event: FocusEvent) { defineExpose({ focus: () => control.input.value?.focus(), element: control.input, + color: control.state.value, }); @@ -90,13 +98,16 @@ defineExpose({ :is="as" ref="root" v-bind="rootAttrs()" - class="hv-color-field" - :class="{ 'hv-color-field--full-width': fullWidth }" + :class="colorFieldVariants({ fullWidth })" + :style="[ordinaryStyle, computedStyle]" data-slot="color-field" :data-channel="channel ?? 'hex'" :data-disabled="disabled || undefined" :data-readonly="readonly || undefined" :data-required="required || undefined" + :data-focused="focused || undefined" + :data-focus-within="focusWithin || undefined" + :data-focus-visible="focusVisible || undefined" :data-invalid="invalid || undefined" @focusin="focusin" @focusout="focusout" diff --git a/packages/vue/src/components/color-field/ColorInputGroup.vue b/packages/vue/src/components/color-field/ColorInputGroup.vue index 9eda339..001a9b1 100644 --- a/packages/vue/src/components/color-field/ColorInputGroup.vue +++ b/packages/vue/src/components/color-field/ColorInputGroup.vue @@ -1,18 +1,21 @@ import("vue").StyleValue); + styleFn?: (state: ColorFieldRenderState) => import("vue").StyleValue; fullWidth?: boolean; autoFocus?: boolean; autoComplete?: string; diff --git a/packages/vue/src/components/color-field/types.ts b/packages/vue/src/components/color-field/types.ts new file mode 100644 index 0000000..2418c1b --- /dev/null +++ b/packages/vue/src/components/color-field/types.ts @@ -0,0 +1,50 @@ +import type { StyleValue, VNode } from "vue"; +import type ColorField from "./ColorField.vue"; +import type ColorInputGroup from "./ColorInputGroup.vue"; +import type ColorInputGroupInput from "./ColorInputGroupInput.vue"; +import type ColorInputGroupPrefix from "./ColorInputGroupPrefix.vue"; +import type ColorInputGroupSuffix from "./ColorInputGroupSuffix.vue"; +export type { + ColorFieldProps, + ColorFieldProps as ColorFieldRootProps, + ColorFieldRenderState, + ColorFieldRenderState as ColorFieldRenderProps, +} from "./context"; +export interface ColorInputGroupRenderProps { + isDisabled: boolean; + isInvalid: boolean; + isFocusWithin: boolean; + isHovered: boolean; +} +export interface ColorInputGroupBaseProps { + variant?: "primary" | "secondary"; + fullWidth?: boolean; + as?: string; + role?: string; + style?: StyleValue | ((state: ColorInputGroupRenderProps) => StyleValue); + styleFn?: (state: ColorInputGroupRenderProps) => StyleValue; +} +export type ColorInputGroupProps = InstanceType< + typeof ColorInputGroup +>["$props"]; +export type ColorInputGroupRootProps = ColorInputGroupProps; +export type ColorFieldGroupProps = ColorInputGroupProps; +export type ColorInputGroupInputProps = InstanceType< + typeof ColorInputGroupInput +>["$props"]; +export type ColorFieldInputProps = ColorInputGroupInputProps; +export type ColorInputGroupPrefixProps = InstanceType< + typeof ColorInputGroupPrefix +>["$props"]; +export type ColorFieldPrefixProps = ColorInputGroupPrefixProps; +export type ColorInputGroupSuffixProps = InstanceType< + typeof ColorInputGroupSuffix +>["$props"]; +export type ColorFieldSuffixProps = ColorInputGroupSuffixProps; +export type ColorFieldExpose = Pick< + InstanceType, + "focus" | "element" | "color" +>; +export type ColorFieldSlots = { + default?: (state: import("./context").ColorFieldRenderState) => VNode[]; +}; diff --git a/packages/vue/src/index.ts b/packages/vue/src/index.ts index 90807b7..62bd68f 100644 --- a/packages/vue/src/index.ts +++ b/packages/vue/src/index.ts @@ -1290,3 +1290,35 @@ export type { RangeCalendarCellProps, RangeCalendarCellIndicatorProps, } from "./types"; + +// ColorField and its shared ColorInputGroup parts (HeroUI ce90ea3). +export { + default as ColorField, + default as ColorFieldRoot, +} from "./components/color-field/ColorField.vue"; +export { + default as ColorInputGroup, + default as ColorInputGroupRoot, + default as ColorFieldGroup, +} from "./components/color-field/ColorInputGroup.vue"; +export { + default as ColorInputGroupInput, + default as ColorFieldInput, +} from "./components/color-field/ColorInputGroupInput.vue"; +export { + default as ColorInputGroupPrefix, + default as ColorFieldPrefix, +} from "./components/color-field/ColorInputGroupPrefix.vue"; +export { + default as ColorInputGroupSuffix, + default as ColorFieldSuffix, +} from "./components/color-field/ColorInputGroupSuffix.vue"; +export type * from "./components/color-field/types"; +export { + colorFieldVariants, + colorInputGroupVariants, +} from "@heroui-vue/styles"; +export type { + ColorFieldVariants, + ColorInputGroupVariants, +} from "@heroui-vue/styles"; diff --git a/packages/vue/tests/fixtures/ColorFieldTypeContract.vue b/packages/vue/tests/fixtures/ColorFieldTypeContract.vue new file mode 100644 index 0000000..876422d --- /dev/null +++ b/packages/vue/tests/fixtures/ColorFieldTypeContract.vue @@ -0,0 +1,48 @@ + + + 聚焦 + + 颜色 + + {{ state.value?.toString("hex") }} + + {{ group.isHovered }} + + + + + + + + + + + diff --git a/tests/color-field-demos.test.ts b/tests/color-field-demos.test.ts new file mode 100644 index 0000000..4b4d2e5 --- /dev/null +++ b/tests/color-field-demos.test.ts @@ -0,0 +1,154 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { mount, flushPromises } from "@vue/test-utils"; +import { readFileSync } from "node:fs"; +import { ColorField } from "../packages/vue/src"; +import Basic from "../apps/docs/demos/color-field-basic.vue"; +import Variants from "../apps/docs/demos/color-field-variants.vue"; +import OnSurface from "../apps/docs/demos/color-field-on-surface.vue"; +import WithDescription from "../apps/docs/demos/color-field-with-description.vue"; +import Required from "../apps/docs/demos/color-field-required.vue"; +import Disabled from "../apps/docs/demos/color-field-disabled.vue"; +import FullWidth from "../apps/docs/demos/color-field-full-width.vue"; +import Invalid from "../apps/docs/demos/color-field-invalid.vue"; +import ChannelEditing from "../apps/docs/demos/color-field-channel-editing.vue"; +import Controlled from "../apps/docs/demos/color-field-controlled.vue"; +import FormExample from "../apps/docs/demos/color-field-form-example.vue"; +import RenderFunction from "../apps/docs/demos/color-field-render-function.vue"; +import CustomStyles from "../apps/docs/demos/color-field-custom-styles.vue"; +const demos = { + basic: Basic, + variants: Variants, + "on-surface": OnSurface, + "with-description": WithDescription, + required: Required, + disabled: Disabled, + "full-width": FullWidth, + invalid: Invalid, + "channel-editing": ChannelEditing, + controlled: Controlled, + "form-example": FormExample, + "render-function": RenderFunction, + "custom-styles": CustomStyles, +}; +const mounted: ReturnType[] = []; +afterEach(() => { + mounted.splice(0).forEach((w) => w.unmount()); + document.body.innerHTML = ""; + vi.useRealTimers(); + vi.restoreAllMocks(); +}); +function render(component: Parameters[0]) { + const w = mount(component, { attachTo: document.body }); + mounted.push(w); + return w; +} +const inputs = (w: ReturnType) => + w.findAll("input[type=text]"); +describe("ColorField documentation", () => { + it("keeps all thirteen fixed-source previews in page order", () => { + const doc = readFileSync("apps/docs/components/color-field.md", "utf8"); + expect( + [...doc.matchAll(/ m[1], + ), + ).toEqual(Object.keys(demos)); + }); + for (const [name, demo] of Object.entries(demos)) + it(`renders ${name} through public parts with a label for every input`, () => { + const w = render(demo); + expect(inputs(w).length).toBeGreaterThan(0); + for (const input of inputs(w)) + expect(w.find(`label[for="${input.attributes("id")}"]`).exists()).toBe( + true, + ); + expect( + readFileSync(`apps/docs/demos/color-field-${name}.vue`, "utf8"), + ).not.toContain("packages/vue"); + }); + it("synchronizes basic hex editing and preview on blur", async () => { + const w = render(Basic); + expect(inputs(w)[0]!.element.value).toBe("#0485F7"); + await inputs(w)[0]!.setValue("22c55e"); + await inputs(w)[0]!.trigger("blur"); + expect(inputs(w)[0]!.element.value).toBe("#22C55E"); + expect( + w.get("[data-slot=color-swatch]").element.style + .backgroundColor, + ).toBe("rgb(34, 197, 94)"); + }); + it("renders actual secondary, full width, disabled and invalid states", () => { + expect( + render(Variants) + .find("[data-slot=color-input-group].hv-color-input-group--secondary") + .exists(), + ).toBe(true); + expect( + render(OnSurface) + .find(".hv-surface .hv-color-input-group--secondary") + .exists(), + ).toBe(true); + expect( + render(FullWidth).findAll(".hv-color-field--full-width"), + ).toHaveLength(2); + expect(inputs(render(Disabled)).every((i) => i.element.disabled)).toBe( + true, + ); + expect( + inputs(render(Invalid)).every( + (i) => i.attributes("aria-invalid") === "true", + ), + ).toBe(true); + expect(inputs(render(Required)).every((i) => i.element.required)).toBe( + true, + ); + }); + it("updates all three HSL fields from one accepted color", async () => { + const w = render(ChannelEditing); + await inputs(w)[0]!.setValue("90"); + await inputs(w)[0]!.trigger("blur"); + expect(inputs(w)[0]!.element.value).toBe("90°"); + expect(inputs(w)[1]!.element.value).toBe("100%"); + expect(w.text()).toContain("当前:#407F00"); + }); + it("controlled buttons replace and clear both input and preview", async () => { + const w = render(Controlled); + await w.findAll("button")[0]!.trigger("click"); + expect(inputs(w)[0]!.element.value).toBe("#EF4444"); + await w.findAll("button")[1]!.trigger("click"); + expect(inputs(w)[0]!.element.value).toBe("#10B981"); + await w.findAll("button")[2]!.trigger("click"); + expect(inputs(w)[0]!.element.value).toBe(""); + expect(w.text()).toContain("(空)"); + }); + it("uses a live scoped slot in the custom root example", async () => { + const w = render(RenderFunction); + // Let mounted field metadata and child refs settle before exercising native focus. + await flushPromises(); + inputs(w)[0]!.element.focus(); + await flushPromises(); + expect(w.get("[data-slot=color-field]").attributes("data-focused")).toBe( + "true", + ); + expect( + w.get("[data-slot=color-input-group]").attributes("data-field-focused"), + ).toBe("true"); + }); + it("submits the accepted form color and restores empty state after saving", async () => { + vi.useFakeTimers(); + const log = vi.spyOn(console, "log").mockImplementation(() => {}); + const w = render(FormExample); + expect(w.get("button").element.disabled).toBe(true); + await inputs(w)[0]!.setValue("abc"); + await inputs(w)[0]!.trigger("blur"); + expect(w.get("button").element.disabled).toBe(false); + expect(new FormData(w.get("form").element).get("brand-color")).toBe( + "#AABBCC", + ); + await w.get("form").trigger("submit"); + expect(w.text()).toContain("保存中…"); + await vi.advanceTimersByTimeAsync(1500); + expect(log).toHaveBeenCalledWith("已提交颜色:", { color: "#AABBCC" }); + expect(inputs(w)[0]!.element.value).toBe(""); + expect(w.getComponent(ColorField).emitted("change")).toHaveLength(1); + }); +}); diff --git a/tests/color-field-protocol.test.ts b/tests/color-field-protocol.test.ts index 0d799bd..ad8d2ca 100644 --- a/tests/color-field-protocol.test.ts +++ b/tests/color-field-protocol.test.ts @@ -1,11 +1,13 @@ import { afterEach, describe, it, expect, vi } from "vitest"; import { mount, flushPromises } from "@vue/test-utils"; import { defineComponent, shallowRef } from "vue"; -import ColorField from "../packages/vue/src/components/color-field/ColorField.vue"; -import ColorInputGroup from "../packages/vue/src/components/color-field/ColorInputGroup.vue"; -import ColorInputGroupInput from "../packages/vue/src/components/color-field/ColorInputGroupInput.vue"; -import Prefix from "../packages/vue/src/components/color-field/ColorInputGroupPrefix.vue"; -import Suffix from "../packages/vue/src/components/color-field/ColorInputGroupSuffix.vue"; +import { + ColorField, + ColorInputGroup, + ColorInputGroupInput, + ColorInputGroupPrefix as Prefix, + ColorInputGroupSuffix as Suffix, +} from "../packages/vue/src/index"; import { Label, Description, diff --git a/tests/color-field.test.ts b/tests/color-field.test.ts new file mode 100644 index 0000000..5d9b911 --- /dev/null +++ b/tests/color-field.test.ts @@ -0,0 +1,178 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { mount, flushPromises } from "@vue/test-utils"; +import { defineComponent, shallowRef, h } from "vue"; +import * as api from "../packages/vue/src"; +const mounted: ReturnType[] = []; +afterEach(() => { + mounted.splice(0).forEach((w) => w.unmount()); + document.body.innerHTML = ""; + vi.restoreAllMocks(); +}); +function view(template: string, setup = () => ({})) { + const w = mount( + defineComponent({ + components: { + ColorField: api.ColorField, + ColorFieldGroup: api.ColorFieldGroup, + ColorFieldInput: api.ColorFieldInput, + ColorFieldPrefix: api.ColorFieldPrefix, + ColorFieldSuffix: api.ColorFieldSuffix, + ColorInputGroup: api.ColorInputGroup, + ColorInputGroupInput: api.ColorInputGroupInput, + Label: api.Label, + Description: api.Description, + FieldError: api.FieldError, + }, + setup, + template, + }), + { attachTo: document.body }, + ); + mounted.push(w); + return w; +} +const control = (w: ReturnType) => + w.get("[data-slot=color-input-group-input]"); +const body = + "颜色#色帮助"; +describe("public ColorField", () => { + it("exports aliases without creating separate implementations", () => { + expect(api.ColorFieldRoot).toBe(api.ColorField); + expect(api.ColorInputGroupRoot).toBe(api.ColorFieldGroup); + expect(api.ColorInputGroupInput).toBe(api.ColorFieldInput); + expect(api.ColorInputGroupPrefix).toBe(api.ColorFieldPrefix); + expect(api.ColorInputGroupSuffix).toBe(api.ColorFieldSuffix); + expect(api.colorFieldVariants({ fullWidth: true })).toContain( + "hv-color-field--full-width", + ); + expect( + api.colorInputGroupVariants({ variant: "secondary" }).base(), + ).toContain("hv-color-input-group--secondary"); + }); + it("routes every ARIA attribute to the input and updates root attrs reactively", async () => { + const details = shallowRef("details-a"); + const w = view( + `${body}`, + () => ({ details }), + ); + expect(control(w).attributes("aria-details")).toBe("details-a"); + expect(control(w).attributes("aria-labelledby")).toContain( + "external-label", + ); + expect(control(w).attributes("aria-describedby")).toContain( + "external-description", + ); + expect( + w.get("[data-slot=color-field]").attributes("aria-details"), + ).toBeUndefined(); + details.value = "details-b"; + await flushPromises(); + expect(control(w).attributes("aria-details")).toBe("details-b"); + }); + it("updates scoped slots, styles and focus states from the real input", async () => { + const w = view( + `{{s.isFocused}}/{{s.isFocusWithin}}`, + ); + control(w).element.focus(); + await flushPromises(); + const root = w.get("section"); + expect(root.attributes("data-focused")).toBe("true"); + expect(root.attributes("data-focus-within")).toBe("true"); + expect(root.element.style.opacity).toBe("0.5"); + expect( + w.get("[data-slot=color-input-group]").element.style + .marginTop, + ).toBe("2px"); + expect(w.get("output").text()).toBe("true/true"); + control(w).element.blur(); + await flushPromises(); + expect(w.get("output").text()).toBe("false/false"); + expect(root.attributes("data-focus-within")).toBeUndefined(); + }); + it("exposes the actual input and color, with usable focus()", async () => { + const w = view(`${body}`); + const field = w.getComponent(api.ColorField); + field.vm.focus(); + await flushPromises(); + expect(document.activeElement).toBe(control(w).element); + expect(field.vm.element).toBe(control(w).element); + expect(field.vm.color?.toString("hex")).toBe("#123456"); + }); + it("retries a rejected hex, preserves the native draft until commit and resets accepted DOM", async () => { + const w = view( + `${body}`, + ); + for (let i = 0; i < 2; i++) { + await control(w).setValue("abc"); + expect(new FormData(w.get("form").element).get("color")).toBe("abc"); + await control(w).trigger("keydown", { key: "Enter" }); + expect(control(w).element.value).toBe("#123456"); + } + expect(w.getComponent(api.ColorField).emitted("change")).toHaveLength(2); + await control(w).setValue("#12"); + w.get("form").element.reset(); + await flushPromises(); + expect(control(w).element.value).toBe("#123456"); + }); + it("modelValue including null takes precedence over value/defaultValue", async () => { + const w = view( + `${body}`, + ); + expect(control(w).element.value).toBe(""); + await control(w).setValue("abc"); + await control(w).trigger("blur"); + expect(control(w).element.value).toBe(""); + }); + it("switches between standalone and inherited color values without leaking drafts", async () => { + const owns = shallowRef(false); + const w = view( + ``, + () => ({ owns }), + ); + const inputs = w.findAll("input[type=text]"); + expect(inputs[1]!.element.value).toBe(inputs[0]!.element.value); + await inputs[1]!.setValue("#12"); + owns.value = true; + await flushPromises(); + expect(inputs[1]!.element.value).toBe("#ABCDEF"); + owns.value = false; + await flushPromises(); + expect(inputs[1]!.element.value).toBe(inputs[0]!.element.value); + }); + it("commits composition only after candidate completion and preserves alpha when text is untouched", async () => { + const w = view( + `${body}`, + ); + await control(w).trigger("compositionstart"); + await control(w).setValue("abc"); + await control(w).trigger("keydown", { key: "Enter" }); + expect(w.getComponent(api.ColorField).emitted("change")).toBeUndefined(); + await control(w).trigger("compositionend"); + await flushPromises(); + await control(w).trigger("keydown", { key: "Escape" }); + await control(w).trigger("blur"); + expect( + w.getComponent(api.ColorField).vm.color?.getChannelValue("alpha"), + ).toBe(0.123456789); + }); + it("does not run unknown wrappers to discover an input", () => { + const setup = vi.fn(() => () => h(api.ColorFieldInput)); + const Unknown = defineComponent({ setup }); + const w = mount(api.ColorField, { slots: { default: () => h(Unknown) } }); + mounted.push(w); + expect(setup).toHaveBeenCalledOnce(); + expect(control(w).element.tagName).toBe("INPUT"); + }); + it("supports an independent group as a plain v-model text field", async () => { + const value = shallowRef("free text"); + const w = view( + '', + () => ({ value }), + ); + await control(w).setValue("anything"); + expect(value.value).toBe("anything"); + expect(w.get("[data-slot=color-input-group]").attributes("role")).toBe( + "group", + ); + }); +}); diff --git a/tests/package/check-client.mjs b/tests/package/check-client.mjs index 15a5048..59e5adc 100644 --- a/tests/package/check-client.mjs +++ b/tests/package/check-client.mjs @@ -44,6 +44,26 @@ for (const id of process.argv.slice(2)) { assert(lib.RangeCalendar === lib.RangeCalendarRoot); assert(lib.rangeCalendarVariants().cell() === "hv-range-calendar__cell"); } + if (id === "color-field") { + assert(lib.ColorField === lib.ColorFieldRoot); + assert(lib.ColorFieldGroup === lib.ColorInputGroup); + assert(lib.ColorFieldGroup === lib.ColorInputGroupRoot); + assert(lib.ColorFieldInput === lib.ColorInputGroupInput); + assert(lib.ColorFieldPrefix === lib.ColorInputGroupPrefix); + assert(lib.ColorFieldSuffix === lib.ColorInputGroupSuffix); + assert( + lib + .colorFieldVariants({ fullWidth: true }) + .includes("hv-color-field--full-width"), + ); + const slots = lib.colorInputGroupVariants({ + variant: "secondary", + fullWidth: true, + }); + assert(slots.base().includes("hv-color-input-group--secondary")); + assert(slots.base().includes("hv-color-input-group--full-width")); + assert(slots.input() === "hv-color-input-group__input"); + } if (id === "time-field") { assert(lib.TimeField === lib.TimeFieldRoot); assert(lib.TimeFieldGroup === lib.DateInputGroup); diff --git a/tests/package/client-checks.json b/tests/package/client-checks.json index 897c4ca..5fb74b1 100644 --- a/tests/package/client-checks.json +++ b/tests/package/client-checks.json @@ -137,12 +137,12 @@ ".hv-color-input-group__prefix", ".hv-color-input-group__suffix" ], - "absent": ["ColorField", "ColorInputGroup", "ColorPicker"] + "absent": ["ColorPicker"] }, "color-foundation": { "exports": [], "css": [".hv-color-area", ".hv-color-area__thumb"], - "absent": ["ColorField", "ColorInputGroup", "ColorPicker"] + "absent": ["ColorPicker"] }, "color-picker-protocol": { "exports": [], @@ -151,7 +151,7 @@ ".hv-color-picker__trigger", ".hv-color-picker__popover" ], - "absent": ["ColorField", "ColorInputGroup", "ColorPicker"] + "absent": ["ColorPicker"] }, "color-slider-foundation": { "exports": [], @@ -165,7 +165,7 @@ "max-height:inherit", "overscroll-behavior:contain" ], - "absent": ["ColorField", "ColorInputGroup", "ColorPicker"] + "absent": ["ColorPicker"] }, "date-field": { "exports": [ @@ -462,5 +462,32 @@ "--track-start-color", "--track-end-color" ] + }, + "color-field": { + "exports": [ + "ColorField", + "ColorFieldRoot", + "ColorFieldGroup", + "ColorFieldInput", + "ColorFieldPrefix", + "ColorFieldSuffix", + "ColorInputGroup", + "ColorInputGroupRoot", + "ColorInputGroupInput", + "ColorInputGroupPrefix", + "ColorInputGroupSuffix", + "colorFieldVariants", + "colorInputGroupVariants" + ], + "css": [ + ".hv-color-field", + ".hv-color-field--full-width", + ".hv-color-input-group", + ".hv-color-input-group--secondary", + ".hv-color-input-group__input", + ".hv-color-input-group__prefix", + ".hv-color-input-group__suffix" + ], + "absent": ["ColorPicker", "ColorSwatchPicker"] } } diff --git a/tests/package/color-field-contract.ts b/tests/package/color-field-contract.ts new file mode 100644 index 0000000..af179e2 --- /dev/null +++ b/tests/package/color-field-contract.ts @@ -0,0 +1,126 @@ +import { h } from "vue"; +import { + ColorField, + ColorFieldRoot, + ColorFieldGroup, + ColorFieldInput, + ColorFieldPrefix, + ColorFieldSuffix, + ColorInputGroup, + ColorInputGroupRoot, + ColorInputGroupInput, + ColorInputGroupPrefix, + ColorInputGroupSuffix, + colorFieldVariants, + colorInputGroupVariants, + parseColor, + type ColorFieldProps, + type ColorFieldRootProps, + type ColorFieldGroupProps, + type ColorFieldInputProps, + type ColorFieldPrefixProps, + type ColorFieldSuffixProps, + type ColorFieldRenderProps, + type ColorFieldRenderState, + type ColorFieldSlots, + type ColorFieldExpose, + type ColorFieldVariants, + type ColorInputGroupVariants, + type ColorInputGroupProps, + type ColorInputGroupInputProps, + type ColorInputGroupRootProps, + type ColorInputGroupPrefixProps, + type ColorInputGroupSuffixProps, + type ColorInputGroupRenderProps, +} from "@heroui-vue/components"; +const props: ColorFieldProps = { + modelValue: parseColor("#123456"), + value: null, + defaultValue: "#abcdef", + channel: "hue", + colorSpace: "hsb", + name: "hue", + form: "external", + isRequired: true, + isReadOnly: false, + isWheelDisabled: true, + locale: "de-DE", + validate: (color) => (color ? true : "必填"), + styleFn: (s) => ({ opacity: s.isFocused ? 0.5 : 1 }), +}; +const root: ColorFieldRootProps = props; +const group: ColorFieldGroupProps = { + variant: "secondary", + styleFn: (s) => ({ opacity: s.isFocusWithin ? 1 : 0.5 }), +}; +const ig: ColorInputGroupProps = group; +const igr: ColorInputGroupRootProps = ig; +const input: ColorFieldInputProps = { defaultValue: "abc" }; +const ii: ColorInputGroupInputProps = input; +const prefix: ColorFieldPrefixProps = { as: "span" }; +const ip: ColorInputGroupPrefixProps = prefix; +const suffix: ColorFieldSuffixProps = { as: "span" }; +const is: ColorInputGroupSuffixProps = suffix; +const slots: ColorFieldSlots = { + default: (s) => [h("span", s.value?.toString("hex"))], +}; +const state: ColorFieldRenderProps = { + value: null, + isDisabled: false, + isReadOnly: false, + isRequired: false, + isInvalid: false, + isFocused: false, + isFocusWithin: false, + isFocusVisible: false, + validationErrors: [], +}; +const renderState: ColorFieldRenderState = state; +const gs: ColorInputGroupRenderProps = { + isDisabled: false, + isInvalid: false, + isFocusWithin: false, + isHovered: false, +}; +const variant: ColorFieldVariants = { fullWidth: true }; +const iv: ColorInputGroupVariants = { variant: "secondary" }; +colorFieldVariants(variant); +colorInputGroupVariants(iv).input(); +h(ColorField, { + ...props, + "onUpdate:modelValue": (value) => value?.toString("hex"), +}); +h(ColorFieldRoot, root); +h(ColorFieldGroup, group); +h(ColorFieldInput, input); +h(ColorFieldPrefix, prefix); +h(ColorFieldSuffix, suffix); +h(ColorInputGroup, ig); +h(ColorInputGroupRoot, igr); +h(ColorInputGroupInput, ii); +h(ColorInputGroupPrefix, ip); +h(ColorInputGroupSuffix, is); +declare const field: InstanceType; +field.focus(); +field.element?.select(); +field.color?.toString("hex"); +const exposed: ColorFieldExpose = field; +declare const control: InstanceType; +control.focus(); +control.select(); +control.element?.select(); +// @ts-expect-error invalid model +const bad: ColorFieldProps = { modelValue: 123 }; +// @ts-expect-error invalid channel +const badChannel: ColorFieldProps = { channel: "cyan" }; +// @ts-expect-error invalid color space +h(ColorField, { colorSpace: "oklch" }); +// @ts-expect-error actual public component rejects invalid model +h(ColorField, { modelValue: 123 }); +// @ts-expect-error variant is primary or secondary +const badGroup: ColorFieldGroupProps = { variant: "tertiary" }; +// @ts-expect-error validators receive Color/null +const badValidator: ColorFieldProps = { validate: (v: string) => true }; +// @ts-expect-error no public transaction state +field.state.begin(); +void [slots, renderState, gs, exposed, bad, badChannel, badGroup, badValidator];
分别编辑 HSL 通道:
- 组合部件、原生关联、格式化通道、提交和共享颜色;公开导出与正式文档尚未接入。 + 组合部件、原生关联、格式化通道、提交和共享颜色;本页通过公共导出验证正式字段;Picker + 保留内部组合夹具。