5e688680e9
Port the fixed-baseline Autocomplete onto the B02a collection protocol. The trigger is the single Tab entry; opening moves focus to the search box inside the popover, which owns filtering, aria-activedescendant and keyboard navigation while the list only tracks an active row. - Seven parts: root (field, value/open models, form value, owner wiring), trigger (anchor + activation), value (scoped slot with selectedItems and selectedText instead of copied item subtrees), indicator, clear button, popover (measured trigger width), filter (owns the search text and composes the inner SearchFieldInput into the combobox control). - Filter exposes input-value/update:inputValue/inputChange, matching ComboBox. - Confirming the active row in multiple mode now toggles it instead of replacing the accepted set (ComboBox shared the same bug). - ComboBox shows the accepted label on the first render via a display value that falls back to the selected record, so preselected fields are readable before the selection watcher runs (including SSR). - Popovers measure the anchor with a ResizeObserver because Reka does not publish the anchor size; the caller's root class is merged again so the trigger fills the field width. - 24 baseline previews, Chinese doc page, navigation, 17 behaviour tests, 5 demo/document contracts and an independent tarball consumption script for both pickers. Not included: the full bundler/NodeNext type matrix (B02d).
262 lines
8.2 KiB
Vue
262 lines
8.2 KiB
Vue
<script setup lang="ts">
|
||
import { ref } from "vue";
|
||
import {
|
||
Autocomplete,
|
||
AutocompleteClearButton,
|
||
AutocompleteFilter,
|
||
AutocompleteIndicator,
|
||
AutocompletePopover,
|
||
AutocompleteTrigger,
|
||
AutocompleteValue,
|
||
EmptyState,
|
||
Label,
|
||
ListBox,
|
||
ListBoxItem,
|
||
ListBoxItemIndicator,
|
||
SearchField,
|
||
SearchFieldGroup,
|
||
SearchFieldInput,
|
||
SearchFieldSearchIcon,
|
||
Tag,
|
||
TagGroup,
|
||
TagGroupList,
|
||
type TagKey,
|
||
} from "@heroui-vue/components";
|
||
|
||
const items = [
|
||
{ id: "option1", name: "选项 1" },
|
||
{ id: "option2", name: "选项 2" },
|
||
{ id: "option3", name: "选项 3" },
|
||
{ id: "option4", name: "选项 4" },
|
||
];
|
||
|
||
const selectedKey1 = ref<string | null>(null);
|
||
const selectedKey2 = ref<string | null>(null);
|
||
const selectedKeys1 = ref<TagKey[]>([]);
|
||
const selectedKeys2 = ref<TagKey[]>([]);
|
||
|
||
function onRemoveTags1(keys: TagKey[]) {
|
||
selectedKeys1.value = selectedKeys1.value.filter(
|
||
(key) => !keys.includes(key),
|
||
);
|
||
}
|
||
|
||
function onRemoveTags2(keys: TagKey[]) {
|
||
selectedKeys2.value = selectedKeys2.value.filter(
|
||
(key) => !keys.includes(key),
|
||
);
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div class="flex flex-col gap-8">
|
||
<div class="flex flex-col gap-4">
|
||
<h3 class="text-lg font-semibold">单选变体</h3>
|
||
<div class="flex flex-col gap-4">
|
||
<Autocomplete
|
||
v-model="selectedKey1"
|
||
class="w-[256px]"
|
||
placeholder="请选择一项"
|
||
selection-mode="single"
|
||
variant="primary"
|
||
>
|
||
<Label>主色(primary)变体</Label>
|
||
<AutocompleteTrigger>
|
||
<AutocompleteValue />
|
||
<AutocompleteClearButton />
|
||
<AutocompleteIndicator />
|
||
</AutocompleteTrigger>
|
||
<AutocompletePopover>
|
||
<AutocompleteFilter>
|
||
<SearchField aria-label="搜索选项">
|
||
<SearchFieldGroup>
|
||
<SearchFieldSearchIcon />
|
||
<SearchFieldInput placeholder="搜索…" />
|
||
</SearchFieldGroup>
|
||
</SearchField>
|
||
<ListBox>
|
||
<ListBoxItem
|
||
v-for="item in items"
|
||
:id="item.id"
|
||
:key="item.id"
|
||
:text-value="item.name"
|
||
>
|
||
{{ item.name }}
|
||
<ListBoxItemIndicator />
|
||
</ListBoxItem>
|
||
<template #empty>
|
||
<EmptyState class="p-1">未找到结果</EmptyState>
|
||
</template>
|
||
</ListBox>
|
||
</AutocompleteFilter>
|
||
</AutocompletePopover>
|
||
</Autocomplete>
|
||
<Autocomplete
|
||
v-model="selectedKey2"
|
||
class="w-[256px]"
|
||
placeholder="请选择一项"
|
||
selection-mode="single"
|
||
variant="secondary"
|
||
>
|
||
<Label>次色(secondary)变体</Label>
|
||
<AutocompleteTrigger>
|
||
<AutocompleteValue />
|
||
<AutocompleteClearButton />
|
||
<AutocompleteIndicator />
|
||
</AutocompleteTrigger>
|
||
<AutocompletePopover>
|
||
<AutocompleteFilter>
|
||
<SearchField aria-label="搜索选项">
|
||
<SearchFieldGroup>
|
||
<SearchFieldSearchIcon />
|
||
<SearchFieldInput placeholder="搜索…" />
|
||
</SearchFieldGroup>
|
||
</SearchField>
|
||
<ListBox>
|
||
<ListBoxItem
|
||
v-for="item in items"
|
||
:id="item.id"
|
||
:key="item.id"
|
||
:text-value="item.name"
|
||
>
|
||
{{ item.name }}
|
||
<ListBoxItemIndicator />
|
||
</ListBoxItem>
|
||
<template #empty>
|
||
<EmptyState class="p-1">未找到结果</EmptyState>
|
||
</template>
|
||
</ListBox>
|
||
</AutocompleteFilter>
|
||
</AutocompletePopover>
|
||
</Autocomplete>
|
||
</div>
|
||
</div>
|
||
<div class="flex flex-col gap-4">
|
||
<h3 class="text-lg font-semibold">多选变体</h3>
|
||
<div class="flex flex-col gap-4">
|
||
<Autocomplete
|
||
v-model="selectedKeys1"
|
||
class="w-[256px]"
|
||
placeholder="选择多项"
|
||
selection-mode="multiple"
|
||
variant="primary"
|
||
>
|
||
<Label>主色(primary)变体</Label>
|
||
<AutocompleteTrigger>
|
||
<AutocompleteValue>
|
||
<template
|
||
#default="{ defaultChildren, isPlaceholder, selectedItems }"
|
||
>
|
||
<template v-if="isPlaceholder || selectedItems.length === 0">
|
||
{{ defaultChildren }}
|
||
</template>
|
||
<TagGroup v-else size="sm" @remove="onRemoveTags1">
|
||
<TagGroupList>
|
||
<Tag
|
||
v-for="item in selectedItems"
|
||
:id="item.key"
|
||
:key="item.key"
|
||
:text-value="item.textValue"
|
||
>
|
||
{{ item.textValue }}
|
||
</Tag>
|
||
</TagGroupList>
|
||
</TagGroup>
|
||
</template>
|
||
</AutocompleteValue>
|
||
<AutocompleteClearButton />
|
||
<AutocompleteIndicator />
|
||
</AutocompleteTrigger>
|
||
<AutocompletePopover>
|
||
<AutocompleteFilter>
|
||
<SearchField aria-label="搜索选项">
|
||
<SearchFieldGroup>
|
||
<SearchFieldSearchIcon />
|
||
<SearchFieldInput placeholder="搜索…" />
|
||
</SearchFieldGroup>
|
||
</SearchField>
|
||
<ListBox>
|
||
<ListBoxItem
|
||
v-for="item in items"
|
||
:id="item.id"
|
||
:key="item.id"
|
||
:text-value="item.name"
|
||
>
|
||
{{ item.name }}
|
||
<ListBoxItemIndicator />
|
||
</ListBoxItem>
|
||
<template #empty>
|
||
<EmptyState class="p-1">未找到结果</EmptyState>
|
||
</template>
|
||
</ListBox>
|
||
</AutocompleteFilter>
|
||
</AutocompletePopover>
|
||
</Autocomplete>
|
||
<Autocomplete
|
||
v-model="selectedKeys2"
|
||
class="w-[256px]"
|
||
placeholder="选择多项"
|
||
selection-mode="multiple"
|
||
variant="secondary"
|
||
>
|
||
<Label>次色(secondary)变体</Label>
|
||
<AutocompleteTrigger>
|
||
<AutocompleteValue>
|
||
<template
|
||
#default="{ defaultChildren, isPlaceholder, selectedItems }"
|
||
>
|
||
<template v-if="isPlaceholder || selectedItems.length === 0">
|
||
{{ defaultChildren }}
|
||
</template>
|
||
<TagGroup
|
||
v-else
|
||
size="sm"
|
||
variant="surface"
|
||
@remove="onRemoveTags2"
|
||
>
|
||
<TagGroupList>
|
||
<Tag
|
||
v-for="item in selectedItems"
|
||
:id="item.key"
|
||
:key="item.key"
|
||
:text-value="item.textValue"
|
||
>
|
||
{{ item.textValue }}
|
||
</Tag>
|
||
</TagGroupList>
|
||
</TagGroup>
|
||
</template>
|
||
</AutocompleteValue>
|
||
<AutocompleteClearButton />
|
||
<AutocompleteIndicator />
|
||
</AutocompleteTrigger>
|
||
<AutocompletePopover>
|
||
<AutocompleteFilter>
|
||
<SearchField aria-label="搜索选项">
|
||
<SearchFieldGroup>
|
||
<SearchFieldSearchIcon />
|
||
<SearchFieldInput placeholder="搜索…" />
|
||
</SearchFieldGroup>
|
||
</SearchField>
|
||
<ListBox>
|
||
<ListBoxItem
|
||
v-for="item in items"
|
||
:id="item.id"
|
||
:key="item.id"
|
||
:text-value="item.name"
|
||
>
|
||
{{ item.name }}
|
||
<ListBoxItemIndicator />
|
||
</ListBoxItem>
|
||
<template #empty>
|
||
<EmptyState class="p-1">未找到结果</EmptyState>
|
||
</template>
|
||
</ListBox>
|
||
</AutocompleteFilter>
|
||
</AutocompletePopover>
|
||
</Autocomplete>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|