feat(autocomplete): migrate Autocomplete with popover-owned filtering
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).
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from "vue";
|
||||
import {
|
||||
Autocomplete,
|
||||
AutocompleteClearButton,
|
||||
AutocompleteFilter,
|
||||
AutocompleteIndicator,
|
||||
AutocompletePopover,
|
||||
AutocompleteTrigger,
|
||||
AutocompleteValue,
|
||||
Description,
|
||||
EmptyState,
|
||||
Label,
|
||||
ListBox,
|
||||
ListBoxItem,
|
||||
ListBoxItemIndicator,
|
||||
SearchField,
|
||||
SearchFieldClearButton,
|
||||
SearchFieldGroup,
|
||||
SearchFieldInput,
|
||||
SearchFieldSearchIcon,
|
||||
Tag,
|
||||
TagGroup,
|
||||
TagGroupList,
|
||||
type TagKey,
|
||||
} from "@heroui-vue/components";
|
||||
|
||||
const teammates = [
|
||||
{ id: "sarah", name: "Sarah Chen", role: "产品设计" },
|
||||
{ id: "marcus", name: "Marcus Lee", role: "工程" },
|
||||
{ id: "priya", name: "Priya Patel", role: "数据科学" },
|
||||
{ id: "jordan", name: "Jordan Kim", role: "客户成功" },
|
||||
{ id: "alex", name: "Alex Rivera", role: "市场营销" },
|
||||
];
|
||||
|
||||
const selectedKeys = ref<TagKey[]>([]);
|
||||
|
||||
const teammateOf = (key: unknown) =>
|
||||
teammates.find((teammate) => teammate.id === key);
|
||||
|
||||
function onRemoveTags(keys: TagKey[]) {
|
||||
selectedKeys.value = selectedKeys.value.filter((key) => !keys.includes(key));
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="w-full max-w-sm pt-6">
|
||||
<Autocomplete
|
||||
v-model="selectedKeys"
|
||||
class="w-full gap-1.5"
|
||||
placeholder="搜索团队成员..."
|
||||
selection-mode="multiple"
|
||||
>
|
||||
<Label class="font-medium text-foreground">负责人</Label>
|
||||
<Description>任务更新时将通知以下人员。</Description>
|
||||
<AutocompleteTrigger
|
||||
class="rounded-xl border border-border/80 bg-surface shadow-sm ring-1 ring-accent/5 transition-[box-shadow,border-color] focus-within:border-accent/25 focus-within:ring-2 focus-within:ring-accent/15 dark:border-border/90 dark:bg-surface dark:ring-accent/10 dark:focus-within:border-accent/35"
|
||||
>
|
||||
<AutocompleteValue>
|
||||
<template
|
||||
#default="{ defaultChildren, isPlaceholder, selectedItems }"
|
||||
>
|
||||
<template v-if="isPlaceholder || selectedItems.length === 0">
|
||||
{{ defaultChildren }}
|
||||
</template>
|
||||
<TagGroup v-else size="sm" @remove="onRemoveTags">
|
||||
<TagGroupList>
|
||||
<Tag
|
||||
v-for="item in selectedItems"
|
||||
:id="item.key"
|
||||
:key="item.key"
|
||||
:text-value="teammateOf(item.key)?.name"
|
||||
class="border border-accent/15 bg-accent/8 text-foreground dark:border-accent/25 dark:bg-accent/12"
|
||||
>
|
||||
{{ teammateOf(item.key)?.name }}
|
||||
</Tag>
|
||||
</TagGroupList>
|
||||
</TagGroup>
|
||||
</template>
|
||||
</AutocompleteValue>
|
||||
<AutocompleteClearButton />
|
||||
<AutocompleteIndicator />
|
||||
</AutocompleteTrigger>
|
||||
<AutocompletePopover
|
||||
class="overflow-hidden rounded-xl border border-border/80 bg-surface shadow-xl ring-1 ring-black/3 dark:border-border/90 dark:bg-surface dark:ring-white/5"
|
||||
>
|
||||
<AutocompleteFilter>
|
||||
<SearchField
|
||||
auto-focus
|
||||
aria-label="按姓名或角色搜索"
|
||||
name="search"
|
||||
variant="secondary"
|
||||
>
|
||||
<SearchFieldGroup
|
||||
class="border-b border-separator bg-surface-secondary/50"
|
||||
>
|
||||
<SearchFieldSearchIcon class="text-muted" />
|
||||
<SearchFieldInput placeholder="按姓名或角色搜索..." />
|
||||
<SearchFieldClearButton />
|
||||
</SearchFieldGroup>
|
||||
</SearchField>
|
||||
<ListBox class="p-1.5">
|
||||
<ListBoxItem
|
||||
v-for="teammate in teammates"
|
||||
:id="teammate.id"
|
||||
:key="teammate.id"
|
||||
:text-value="`${teammate.name} ${teammate.role}`"
|
||||
class="rounded-lg data-[focused=true]:bg-accent/10 data-[selected=true]:bg-accent/5 dark:data-[focused=true]:bg-accent/15 dark:data-[selected=true]:bg-accent/8"
|
||||
>
|
||||
<div class="flex flex-col gap-0.5">
|
||||
<span class="text-sm font-medium text-foreground">
|
||||
{{ teammate.name }}
|
||||
</span>
|
||||
<span class="text-xs text-muted">{{ teammate.role }}</span>
|
||||
</div>
|
||||
<ListBoxItemIndicator />
|
||||
</ListBoxItem>
|
||||
<template #empty>
|
||||
<EmptyState class="p-1">未找到团队成员</EmptyState>
|
||||
</template>
|
||||
</ListBox>
|
||||
</AutocompleteFilter>
|
||||
</AutocompletePopover>
|
||||
</Autocomplete>
|
||||
</div>
|
||||
</template>
|
||||
Reference in New Issue
Block a user