diff --git a/apps/docs/.vitepress/theme/navigation.ts b/apps/docs/.vitepress/theme/navigation.ts index 8d68d3c..0aae4ef 100644 --- a/apps/docs/.vitepress/theme/navigation.ts +++ b/apps/docs/.vitepress/theme/navigation.ts @@ -80,6 +80,11 @@ export const componentItems: DocsItem[] = [ url: "/components/close-button", category: "buttons", }, + { + title: "ComboBox 组合框", + url: "/components/combo-box", + category: "pickers", + }, { title: "DateField 日期字段", url: "/components/date-field", diff --git a/apps/docs/components/combo-box.md b/apps/docs/components/combo-box.md new file mode 100644 index 0000000..ca3fd47 --- /dev/null +++ b/apps/docs/components/combo-box.md @@ -0,0 +1,378 @@ +--- +title: ComboBox 组合框 +description: 将文本输入与 ListBox 结合,用户可通过输入查询把选项列表过滤为匹配项 +component: combo-box +group: 组件 +--- + +## 用法 + +```vue + + + +``` + + + +输入框既是过滤条件也是控件的真实焦点:`ComboBoxInputGroup` 把 `Input` 组合成 `role="combobox"` 控件,键盘与 `aria-activedescendant` 都落在它身上,弹层里的列表只维护活动行,不抢焦点。 + +## 组件结构 + +```vue + + + +``` + +上游的对象式子部件 `ComboBox.InputGroup` / `.Trigger` / `.Value` / `.Popover` 在本库中是具名导出 `ComboBoxInputGroup` / `ComboBoxTrigger` / `ComboBoxValue` / `ComboBoxPopover`。 + +## 示例 + +### 宽度充满 + + + +### 带描述 + + + +### 必填 + + + +### 禁用 + + + +### 含禁用选项 + + + +### 分组选项 + + + +### 受控组件 + + + +### 受控输入值 + + + +### 异步加载 + + + +### 默认选中项 + + + +### 允许自定义值 + + + +### 自定义指示器 + + + +### 自定义展示值 + + + +### 自定义过滤 + + + +### 渲染函数 + + + +上游用 React `render` 函数替换根 DOM 元素;Vue 中对应 `as`(自定义根元素标签)与 `as-child`(自定义组件作为唯一根),示例通过根元素属性演示同一能力。 + +### 菜单触发方式 + +使用 `menu-trigger` 控制弹层何时打开: + +- `focus`(默认):输入框获得焦点时打开 +- `input`:用户编辑输入文本时打开 +- `manual`:仅当按下触发按钮或使用方向键时打开 + + + +### 表单值 + +使用 `form-value` 控制提交表单时提交选中项的 key 还是文本。默认值为 `key`;`allows-custom-value` 为真时始终提交文本。多选时多个值以英文逗号连接。 + + + +### 校验行为 + +使用 `validation-behavior` 控制校验信息的展示方式: + +- `native`(默认):值为空或无效时阻止表单提交 +- `aria`:实时显示错误,不阻止提交 + + + +### 自定义校验 + +使用 `validate` 在值无效时返回错误信息,有效时返回 `true`。 + + + +### 只读 + +使用 `is-read-only` 将 ComboBox 设为只读。选中值可以获得焦点,但不能修改。 + + + +### 多选 + +设置 `selection-mode="multiple"` 允许选择多个选项。多选时用 `ComboBoxValue` 展示已选项,并同时给内部 `ListBox` 传 `selection-mode="multiple"`。值通过 `v-model` / `default-value`(数组)以及 `@selection-change` 控制。 + + + +### 表面样式 + +在 Surface 内使用时,请使用 `variant="secondary"` 以应用适合 Surface 背景的低强调变体。 + + + +## 自定义样式 + +### Tailwind CSS + + + +### 全局 CSS + +可使用 `@layer components` 指令自定义 ComboBox 组件类。 + +```css +@layer components { + .hv-combo-box { + @apply flex flex-col gap-1; + } + + .hv-combo-box__input-group { + @apply relative inline-flex items-center; + } + + .hv-combo-box__trigger { + @apply absolute end-0 text-muted; + } + + .hv-combo-box__popover { + @apply rounded-lg border border-border bg-surface p-2; + } +} +``` + +## 样式参考 + +HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。 + +### CSS 类 + +ComboBox 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/combo-box.css)): + +#### 基础类 + +- `.hv-combo-box` - ComboBox 根容器 +- `.hv-combo-box__input-group` - 输入框与触发按钮的容器 +- `.hv-combo-box__value` - 已选值的展示区域(用于多选) +- `.hv-combo-box__trigger` - 打开 Popover 的按钮 +- `.hv-combo-box__popover` - Popover 容器 + +#### 状态类 + +- `.hv-combo-box[data-invalid="true"]` - 无效状态(隐藏 Description) +- `.hv-combo-box[data-disabled="true"]` - 禁用状态 +- `.hv-combo-box__trigger[data-focus-visible="true"]` - 触发器聚焦 +- `.hv-combo-box__trigger[data-disabled="true"]` - 触发器禁用 +- `.hv-combo-box__trigger[data-open="true"]` - 展开状态(图标旋转 180°) +- `.hv-combo-box__value[data-placeholder="true"]` - 占位符状态 +- `.hv-combo-box [data-slot="input"]:focus` - 输入框聚焦(组合框里额外提供非 `focus-visible` 的聚焦态) + +### 交互状态 + +- **Hover**:触发器上 `:hover` 或 `[data-hovered="true"]` +- **Focus**:触发器上 `:focus-visible` 或 `[data-focus-visible="true"]` +- **Disabled**:ComboBox 上 `[data-disabled="true"]`,触发器上 `[data-disabled="true"]` +- **Open**:触发器上 `[data-open="true"]` + +## API 参考 + +### ComboBox + +| Prop | 类型 | 默认值 | 描述 | +| ------------------------- | ------------------------------------------------------------ | ----------- | ------------------------------------------------------- | +| `input-value` | `string` | - | 当前输入值(受控,可用 `v-model:input-value`) | +| `default-input-value` | `string` | `''` | 默认输入值(非受控) | +| `selection-mode` | `'single' \| 'multiple'` | `'single'` | 单选或多选 | +| `model-value` | `string \| number \| Array \| null` | - | 当前值(受控,`v-model`) | +| `value` | `string \| number \| Array \| null` | - | 同 `model-value`(受控别名) | +| `default-value` | `string \| number \| Array \| null` | - | 初始值(非受控) | +| `selected-key` | `string \| number \| null` | - | 单选当前 key(受控别名) | +| `default-selected-key` | `string \| number \| null` | - | 单选初始 key(非受控别名) | +| `items` | `Array<{ id, textValue, isDisabled? }>` | - | 数据驱动渲染;不传时以 `ListBox` 的静态子项作为集合 | +| `disabled-keys` | `Array` | - | 禁用项的 key | +| `default-filter` | `(text: string, inputValue: string) => boolean` | 包含匹配 | 自定义过滤函数 | +| `is-disabled` | `boolean` | `false` | 是否禁用 | +| `is-read-only` | `boolean` | `false` | 只读:值可聚焦但不可修改 | +| `is-required` | `boolean` | `false` | 是否必填 | +| `is-invalid` | `boolean` | `false` | 值是否无效 | +| `validate` | `(value) => string \| string[] \| true \| null \| undefined` | - | 校验函数,返回字符串表示无效 | +| `validation-behavior` | `'native' \| 'aria'` | `'native'` | 原生校验阻止提交,或只用 ARIA 标记 | +| `name` | `string` | - | 表单提交时的字段名(输出隐藏输入) | +| `form` | `string` | - | 关联的表单 id | +| `form-value` | `'key' \| 'text'` | `'key'` | 提交 key 还是文本;`allows-custom-value` 时始终提交文本 | +| `auto-complete` | `string` | `'off'` | 输入框 `autocomplete` | +| `auto-focus` | `boolean` | `false` | 挂载时聚焦 | +| `allows-custom-value` | `boolean` | `false` | 允许输入不在列表中的值 | +| `allows-empty-collection` | `boolean` | `false` | 无匹配项时仍可打开弹层 | +| `menu-trigger` | `'focus' \| 'input' \| 'manual'` | `'focus'` | 打开弹层的交互方式 | +| `should-focus-wrap` | `boolean` | `false` | 方向键是否在首尾循环 | +| `full-width` | `boolean` | `false` | 是否占满容器宽度 | +| `variant` | `'primary' \| 'secondary'` | `'primary'` | 视觉变体 | +| `placeholder` | `string` | - | `ComboBoxValue` 的缺省占位文本 | +| `as` | `string` | `'div'` | 自定义根元素标签 | +| `as-child` | `boolean` | `false` | 使用唯一子元素作为根节点并合并属性 | +| 默认插槽 | `{ isOpen, inputValue, selectedKey, selectedKeys }` | - | 组件内容;作用域提供当前状态 | +| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) | + +事件: + +| 事件 | 载荷 | 描述 | +| -------------------- | ----------------------------------- | ------------- | +| `update:modelValue` | `string \| number \| Array \| null` | 值变化 | +| `change` | 同上 | 值变化 | +| `update:selectedKey` | `string \| number \| null` | 单选 key 变化 | +| `selectionChange` | `string \| number \| null` | 单选 key 变化 | +| `update:inputValue` | `string` | 输入值变化 | +| `inputChange` | `string` | 输入值变化 | +| `update:open` | `boolean` | 弹层开关变化 | +| `openChange` | `boolean` | 弹层开关变化 | + +`aria-label`、`aria-labelledby`、`aria-describedby` 落在真正的 `role="combobox"` 输入框上,而不是根容器。 + +### ComboBoxInputGroup + +| Prop | 类型 | 默认值 | 描述 | +| -------- | --------------------------- | ------ | ---------------------------------------------- | +| 默认插槽 | `Component` | - | 必须包含本库 `Input`;可再放 `ComboBoxTrigger` | +| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) | + +### ComboBoxValue + +渲染已选中的值;没有选中项时渲染占位符。多选时以逗号连接各选中项文本。 + +| Prop | 类型 | 默认值 | 描述 | +| ------------- | ---------------------------------- | -------------------- | ----------------------------- | +| `placeholder` | `string` | 根上的 `placeholder` | 未选中任何项时的占位文本 | +| 默认插槽 | `{ isPlaceholder, selectedItems }` | - | 自定义已选值展示 | +| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) | + +### ComboBoxTrigger + +| Prop | 类型 | 默认值 | 描述 | +| ------------- | --------------------------- | ------- | ------------------------------ | +| `is-disabled` | `boolean` | `false` | 是否禁用触发器 | +| 默认插槽 | `Component` | - | 自定义触发器内容(默认下箭头) | +| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) | + +触发器默认 `tabindex="-1"`:整个组合框保留输入框一个 Tab 入口。 + +### ComboBoxPopover + +| Prop | 类型 | 默认值 | 描述 | +| ------------------- | --------------------------- | ---------- | ----------------------------- | +| `placement` | 位置联合类型 | `'bottom'` | 相对触发容器的位置 | +| `offset` | `number` | `8` | 偏移量 | +| `should-flip` | `boolean` | `true` | 空间不足时是否翻转 | +| `container-padding` | `number` | `12` | 距离视口边缘的最小间距 | +| `to` | `string \| HTMLElement` | - | 自定义 Portal 容器 | +| 默认插槽 | `Component` | - | 弹层内容(通常为 `ListBox`) | +| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) | + +## 无障碍 + +ComboBox 实现 ARIA combobox 模式: + +- 真实焦点始终留在输入框,`aria-activedescendant` 指向已挂载且可见的活动选项 +- `ArrowDown` / `ArrowUp` / `Home` / `End` 在过滤后的可见集合中移动活动项,`Enter` 确认,`Escape` 关闭,`Tab` 离开 +- 中文输入法组合期间不确认选择(`isComposing` / `keyCode 229` 时忽略键盘提交) +- 过滤不会清除已接受的选择;被过滤掉的已选项仍保留标签 +- 弹层内不出现第二个 Tab 入口,关闭后移除 `aria-activedescendant` + +## 相关组件 + +[ListBox 列表框](./list-box) · [Select 选择器](./select) · [FieldError 字段错误](./field-error) · [Label 标签](./label) diff --git a/apps/docs/demos/combo-box-allows-custom-value.vue b/apps/docs/demos/combo-box-allows-custom-value.vue new file mode 100644 index 0000000..bbc09cb --- /dev/null +++ b/apps/docs/demos/combo-box-allows-custom-value.vue @@ -0,0 +1,53 @@ + + + diff --git a/apps/docs/demos/combo-box-asynchronous-loading.vue b/apps/docs/demos/combo-box-asynchronous-loading.vue new file mode 100644 index 0000000..6d27ec3 --- /dev/null +++ b/apps/docs/demos/combo-box-asynchronous-loading.vue @@ -0,0 +1,158 @@ + + + + diff --git a/apps/docs/demos/combo-box-controlled-input-value.vue b/apps/docs/demos/combo-box-controlled-input-value.vue new file mode 100644 index 0000000..e15e0b1 --- /dev/null +++ b/apps/docs/demos/combo-box-controlled-input-value.vue @@ -0,0 +1,61 @@ + + + diff --git a/apps/docs/demos/combo-box-controlled.vue b/apps/docs/demos/combo-box-controlled.vue new file mode 100644 index 0000000..ed0851b --- /dev/null +++ b/apps/docs/demos/combo-box-controlled.vue @@ -0,0 +1,59 @@ + + + diff --git a/apps/docs/demos/combo-box-custom-filtering.vue b/apps/docs/demos/combo-box-custom-filtering.vue new file mode 100644 index 0000000..e0d2fcc --- /dev/null +++ b/apps/docs/demos/combo-box-custom-filtering.vue @@ -0,0 +1,50 @@ + + + diff --git a/apps/docs/demos/combo-box-custom-indicator.vue b/apps/docs/demos/combo-box-custom-indicator.vue new file mode 100644 index 0000000..c7477fc --- /dev/null +++ b/apps/docs/demos/combo-box-custom-indicator.vue @@ -0,0 +1,54 @@ + + + diff --git a/apps/docs/demos/combo-box-custom-styles.vue b/apps/docs/demos/combo-box-custom-styles.vue new file mode 100644 index 0000000..0ba4120 --- /dev/null +++ b/apps/docs/demos/combo-box-custom-styles.vue @@ -0,0 +1,48 @@ + + + diff --git a/apps/docs/demos/combo-box-custom-validation.vue b/apps/docs/demos/combo-box-custom-validation.vue new file mode 100644 index 0000000..e1c0a69 --- /dev/null +++ b/apps/docs/demos/combo-box-custom-validation.vue @@ -0,0 +1,68 @@ + + + diff --git a/apps/docs/demos/combo-box-custom-value.vue b/apps/docs/demos/combo-box-custom-value.vue new file mode 100644 index 0000000..2165824 --- /dev/null +++ b/apps/docs/demos/combo-box-custom-value.vue @@ -0,0 +1,90 @@ + + + diff --git a/apps/docs/demos/combo-box-default-selected-key.vue b/apps/docs/demos/combo-box-default-selected-key.vue new file mode 100644 index 0000000..8a92ae3 --- /dev/null +++ b/apps/docs/demos/combo-box-default-selected-key.vue @@ -0,0 +1,51 @@ + + + diff --git a/apps/docs/demos/combo-box-default.vue b/apps/docs/demos/combo-box-default.vue new file mode 100644 index 0000000..06d39a6 --- /dev/null +++ b/apps/docs/demos/combo-box-default.vue @@ -0,0 +1,45 @@ + + + diff --git a/apps/docs/demos/combo-box-disabled.vue b/apps/docs/demos/combo-box-disabled.vue new file mode 100644 index 0000000..ce56367 --- /dev/null +++ b/apps/docs/demos/combo-box-disabled.vue @@ -0,0 +1,51 @@ + + + diff --git a/apps/docs/demos/combo-box-form-value.vue b/apps/docs/demos/combo-box-form-value.vue new file mode 100644 index 0000000..12a24b8 --- /dev/null +++ b/apps/docs/demos/combo-box-form-value.vue @@ -0,0 +1,86 @@ + + + diff --git a/apps/docs/demos/combo-box-full-width.vue b/apps/docs/demos/combo-box-full-width.vue new file mode 100644 index 0000000..5cb78a2 --- /dev/null +++ b/apps/docs/demos/combo-box-full-width.vue @@ -0,0 +1,41 @@ + + + diff --git a/apps/docs/demos/combo-box-menu-trigger.vue b/apps/docs/demos/combo-box-menu-trigger.vue new file mode 100644 index 0000000..e839636 --- /dev/null +++ b/apps/docs/demos/combo-box-menu-trigger.vue @@ -0,0 +1,138 @@ + + + diff --git a/apps/docs/demos/combo-box-multiple-selection.vue b/apps/docs/demos/combo-box-multiple-selection.vue new file mode 100644 index 0000000..05a8cb9 --- /dev/null +++ b/apps/docs/demos/combo-box-multiple-selection.vue @@ -0,0 +1,47 @@ + + + diff --git a/apps/docs/demos/combo-box-on-surface.vue b/apps/docs/demos/combo-box-on-surface.vue new file mode 100644 index 0000000..dabb6a1 --- /dev/null +++ b/apps/docs/demos/combo-box-on-surface.vue @@ -0,0 +1,66 @@ + + + diff --git a/apps/docs/demos/combo-box-read-only.vue b/apps/docs/demos/combo-box-read-only.vue new file mode 100644 index 0000000..8b4a82c --- /dev/null +++ b/apps/docs/demos/combo-box-read-only.vue @@ -0,0 +1,45 @@ + + + diff --git a/apps/docs/demos/combo-box-render-function.vue b/apps/docs/demos/combo-box-render-function.vue new file mode 100644 index 0000000..af70d53 --- /dev/null +++ b/apps/docs/demos/combo-box-render-function.vue @@ -0,0 +1,51 @@ + + + diff --git a/apps/docs/demos/combo-box-required.vue b/apps/docs/demos/combo-box-required.vue new file mode 100644 index 0000000..f640301 --- /dev/null +++ b/apps/docs/demos/combo-box-required.vue @@ -0,0 +1,75 @@ + + + diff --git a/apps/docs/demos/combo-box-validation-behavior.vue b/apps/docs/demos/combo-box-validation-behavior.vue new file mode 100644 index 0000000..654b7a1 --- /dev/null +++ b/apps/docs/demos/combo-box-validation-behavior.vue @@ -0,0 +1,107 @@ + + + diff --git a/apps/docs/demos/combo-box-with-description.vue b/apps/docs/demos/combo-box-with-description.vue new file mode 100644 index 0000000..91b6926 --- /dev/null +++ b/apps/docs/demos/combo-box-with-description.vue @@ -0,0 +1,53 @@ + + + diff --git a/apps/docs/demos/combo-box-with-disabled-options.vue b/apps/docs/demos/combo-box-with-disabled-options.vue new file mode 100644 index 0000000..d32cace --- /dev/null +++ b/apps/docs/demos/combo-box-with-disabled-options.vue @@ -0,0 +1,53 @@ + + + diff --git a/apps/docs/demos/combo-box-with-sections.vue b/apps/docs/demos/combo-box-with-sections.vue new file mode 100644 index 0000000..0cd4856 --- /dev/null +++ b/apps/docs/demos/combo-box-with-sections.vue @@ -0,0 +1,89 @@ + + + diff --git a/docs/compatibility.md b/docs/compatibility.md index 9b1e83f..61ff417 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -24,7 +24,7 @@ | color-swatch | 计划 | 待实施时核验子部件、属性、事件、键盘行为与替代底座 | | color-swatch-picker | 计划 | 待实施时核验子部件、属性、事件、键盘行为与替代底座 | | close-button | 验证中(最小实现) | 专用 CSS/变体、默认基线 CloseIcon、插槽替换图标、isDisabled、aria-label 默认 Close;onPress 由 click 适配 | -| combo-box | 计划 | 待实施时核验子部件、属性、事件、键盘行为与替代底座 | +| combo-box | 已迁移(有已知差异) | ComboBox 五部件;24/24 基线例;可编辑输入 + 过滤 + 虚拟焦点、form-value key/text、menu-trigger 三态、多选 Value;见 B02 报告 | | date-field | 已迁移(有已知差异) | DateField 七部件与 DateInputGroup 六部件、17 预览;完整 ISO、字段校验、reset、SSR/水合与浏览器重点检查,见 DATE-02 报告;本地化整日期粘贴、13 月/era 编辑等仍待专项。 | | date-picker | 计划 | 待实施时核验子部件、属性、事件、键盘行为与替代底座 | | date-range-picker | 计划 | 待实施时核验子部件、属性、事件、键盘行为与替代底座 | diff --git a/docs/migrations/B02-combobox-autocomplete.md b/docs/migrations/B02-combobox-autocomplete.md index 3ac4fd1..57058f9 100644 --- a/docs/migrations/B02-combobox-autocomplete.md +++ b/docs/migrations/B02-combobox-autocomplete.md @@ -249,3 +249,52 @@ export interface CollectionOwner extends CollectionView { ## 11. B02a 阶段复核通过 以 [B02a 阶段验收](B02a-acceptance.md) 为当前结论。集成人补齐静态空集合、嵌套控件按下状态及探针独立运行的 scrollTo 环境初始化;最新全套为 77 文件 / 1026 项,无未处理错误。仅放行 B02a,在当前任务分支连续完成 B02b、B02c、B02d,公共组件和完整示例尚未验收。 + +## 11. B02b:ComboBox 全部部件、文档与示例(已实现) + +起点 `ff486fb`(含 B02a 验收 `B02a-acceptance.md`)。5 个部件、24 个基线示例、1 页中文文档、导航、19 项行为测试与 6 项演示/文档测试全部落地。**未做**独立产物消费(按四段安排在 B02d 统一完成)。 + +### 11.1 部件与映射 + +| 部件 | 实现要点 | +| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `ComboBox` | 字段上下文(`kind: "text"`,`Label` 渲染 `