以数据形式提供选项,从服务器加载选项,并定义菜单动作。

Select、Combobox、Autocomplete 和 Listbox 通过 property 接收选项。当你的选项已经来自数组时,就用这种方式。你也可以用 HTML 写出它们的选项部件。

从字符串开始

<ui-select>
  <ui-select-trigger
    ><button aria-label="Fruit">
      <ui-select-value placeholder="Choose a fruit"></ui-select-value></button
  ></ui-select-trigger>
  <ui-select-content>
    <ui-select-list></ui-select-list>
  </ui-select-content>
</ui-select>
import "@ui-sdk/web/select"

const select = document.querySelector("ui-select")!
select.items = ["Apple", "Banana", "Pear"]

每个字符串既是值也是它的标签。当值和标签不同时,使用 { value: "apple", label: "Apple" }。选项还可以带有 disabled 和 keywords。

用 { type: "group", label, items } 分组选项,或用 { type: "separator" } 分隔。用 ListData 标注数组,你的编辑器会在你书写的位置检查每个条目:

import type { ListData } from "@ui-sdk/core/select"

const plans: ListData = [
  { value: "free", label: "Free" },
  { value: "pro", label: "Pro" },
  { type: "separator" },
  {
    type: "group",
    label: "Contact sales",
    items: [{ value: "enterprise", label: "Enterprise" }],
  },
]

对于对象值,设置 itemToValue 和 itemToLabel 两个 property,并把类型写成 ListData<Plan>。Select 有完整的示例。

Trigger Menu 用 triggers property 定义它的触发器;见 Trigger Menu。

搜索并加载选项

filter 属性选择一个匹配模式,比如 filter="startsWith"。要做自己的匹配,把函数赋给 filter property。异步选项 涵盖从服务器加载。

对于长列表,把 items property 设为所有选项,只渲染可见的行。Select 和 Combobox 展示了做法。

定义菜单

<ui-menu>
  <ui-menu-trigger><button>File</button></ui-menu-trigger>
</ui-menu>
import "@ui-sdk/web/menu"
import type { MenuData } from "@ui-sdk/web"

const fileMenu: MenuData = [
  { value: "open", label: "Open", shortcut: "mod+o" },
  { type: "separator" },
  {
    type: "submenu",
    label: "Share",
    items: [{ value: "email", label: "Email" }],
  },
]

const menu = document.querySelector("ui-menu")!
menu.items = fileMenu
menu.addEventListener("action", (event) => console.log(event.detail.value))

一个普通字符串就是一个动作,它的值和标签都是这个字符串。Menu 涵盖复选框选项和单选组。Command 使用 CommandData,触发器菜单使用 TriggerMenuData,条目相同但各有自己的字段,树使用 TreeItemData[]。

为数据部件设置样式

从数据渲染的部件一开始没有样式,也没有类名。要设置样式,在元素里放一个 <template>,把每个部件按你想要的外观在里面写一次。元素渲染的每个部件都是模板里同标签部件的副本,类名和内容都包括在内。

<ui-select default-value="pro">
  <ui-select-trigger>
    <button><ui-select-value></ui-select-value></button>
  </ui-select-trigger>
  <template>
    <ui-select-content class="rounded-2xl bg-white p-1 shadow-lg">
      <ui-select-group>
        <ui-select-group-label
          class="px-2 py-1 text-xs text-neutral-500"
        ></ui-select-group-label>
        <ui-select-item
          class="relative flex items-center rounded-xl py-1.5 ps-2 pe-8 data-highlighted:bg-neutral-100"
        >
          <ui-select-item-indicator class="absolute end-2"
            >✓</ui-select-item-indicator
          >
        </ui-select-item>
      </ui-select-group>
      <ui-select-separator
        class="-mx-1 my-1 h-px bg-neutral-200"
      ></ui-select-separator>
    </ui-select-content>
  </template>
</ui-select>
const select = document.querySelector("ui-select")!
select.items = plans

元素按标签查找每个部件,所以模板里每种只需要一个,嵌套方式以最易读为准。没有放进模板的部件渲染时没有样式。

  • 容器只复制自身。 内容、列表、分组或子菜单的内容取得模板的类名,数据则填入其中。
  • 选项复制其中的一切。 你放进模板选项里的指示器、图标或快捷键会显示在每一个选项中。
  • 标签放进文本部件。 当模板的选项里有文本部件时,比如 <ui-select-item-text>,标签就放进去,这样你可以用 [&_mark]:font-semibold 这样的类名给搜索匹配到的内容设置样式。没有文本部件时,标签排在选项最前面。

Select、Combobox、Autocomplete、Command、Listbox 和 Trigger Menu 复制它们的内容、列表、分组、分组标签、选项和分隔符。带标签片(chip)的组合框还会复制它的标签片,连同你在其中写的移除按钮:

<template>
  <ui-combobox-chip
    class="flex items-center gap-1 rounded-2xl bg-neutral-200 ps-1.5 pe-0.5 text-xs"
  >
    <ui-combobox-chip-remove>
      <button type="button" class="opacity-50 hover:opacity-100">×</button>
    </ui-combobox-chip-remove>
  </ui-combobox-chip>
</template>

菜单复制它的内容、选项、复选框选项、单选选项、分组、分组标签、分隔符、子菜单触发器和子菜单内容。把勾选标记放进模板的复选框选项,把箭头放进它的子菜单触发器。

树复制它的选项、指示器、复选框、标签和分组。分支会得到指示器;树是 checkable 时,每个选项都会得到复选框。链接选项的标签会变成一个 <a>,取得标签的类名。

<ui-tree aria-label="Groceries">
  <template>
    <ui-tree-item class="flex flex-wrap items-center">
      <ui-tree-item-indicator
        class="size-4 before:content-['›'] data-expanded:rotate-90"
      ></ui-tree-item-indicator>
      <ui-tree-item-label
        class="flex-1 rounded-xl px-2 data-selected:bg-neutral-100"
      ></ui-tree-item-label>
      <ui-tree-group class="basis-full ps-4"></ui-tree-group>
    </ui-tree-item>
  </template>
</ui-tree>

按键与快捷键

loop 控制方向键是否在列表两端绕回。支持 orientation 的组件用它来选择导航轴。

触发器的 shortcut="mod+k" 让你能从页面其他位置打开它。数据菜单在关闭时会执行选项的快捷键;用 HTML 写的菜单项只显示它的快捷键。快捷键 涵盖注册你自己的快捷键。