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).
147 lines
3.8 KiB
Vue
147 lines
3.8 KiB
Vue
<script setup lang="ts">
|
|
import { ref } from "vue";
|
|
import {
|
|
Autocomplete,
|
|
AutocompleteClearButton,
|
|
AutocompleteFilter,
|
|
AutocompleteIndicator,
|
|
AutocompletePopover,
|
|
AutocompleteTrigger,
|
|
AutocompleteValue,
|
|
Avatar,
|
|
AvatarFallback,
|
|
AvatarImage,
|
|
Description,
|
|
EmptyState,
|
|
Label,
|
|
ListBox,
|
|
ListBoxItem,
|
|
ListBoxItemIndicator,
|
|
SearchField,
|
|
SearchFieldClearButton,
|
|
SearchFieldGroup,
|
|
SearchFieldInput,
|
|
SearchFieldSearchIcon,
|
|
} from "@heroui-vue/components";
|
|
|
|
const users = [
|
|
{
|
|
id: "1",
|
|
name: "Bob",
|
|
email: "bob@heroui.com",
|
|
fallback: "B",
|
|
avatarUrl:
|
|
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/blue.jpg",
|
|
},
|
|
{
|
|
id: "2",
|
|
name: "Fred",
|
|
email: "fred@heroui.com",
|
|
fallback: "F",
|
|
avatarUrl:
|
|
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg",
|
|
},
|
|
{
|
|
id: "3",
|
|
name: "Martha",
|
|
email: "martha@heroui.com",
|
|
fallback: "M",
|
|
avatarUrl:
|
|
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/purple.jpg",
|
|
},
|
|
{
|
|
id: "4",
|
|
name: "John",
|
|
email: "john@heroui.com",
|
|
fallback: "J",
|
|
avatarUrl:
|
|
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/red.jpg",
|
|
},
|
|
{
|
|
id: "5",
|
|
name: "Jane",
|
|
email: "jane@heroui.com",
|
|
fallback: "J",
|
|
avatarUrl:
|
|
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/orange.jpg",
|
|
},
|
|
];
|
|
|
|
const selectedKey = ref<string | null>(null);
|
|
|
|
const userOf = (key: unknown) => users.find((user) => user.id === key);
|
|
</script>
|
|
|
|
<template>
|
|
<Autocomplete
|
|
v-model="selectedKey"
|
|
class="w-[256px]"
|
|
placeholder="选择用户"
|
|
selection-mode="single"
|
|
>
|
|
<Label>用户</Label>
|
|
<AutocompleteTrigger>
|
|
<AutocompleteValue>
|
|
<template #default="{ defaultChildren, isPlaceholder, selectedItems }">
|
|
<template v-if="isPlaceholder || selectedItems.length === 0">
|
|
{{ defaultChildren }}
|
|
</template>
|
|
<template v-else-if="selectedItems.length > 1">
|
|
已选择 {{ selectedItems.length }} 位用户
|
|
</template>
|
|
<div v-else class="flex items-center gap-2">
|
|
<Avatar class="size-4" size="sm">
|
|
<AvatarImage
|
|
:src="userOf(selectedItems[0]?.key)?.avatarUrl ?? ''"
|
|
/>
|
|
<AvatarFallback>
|
|
{{ userOf(selectedItems[0]?.key)?.fallback }}
|
|
</AvatarFallback>
|
|
</Avatar>
|
|
<span>{{ userOf(selectedItems[0]?.key)?.name }}</span>
|
|
</div>
|
|
</template>
|
|
</AutocompleteValue>
|
|
<AutocompleteClearButton />
|
|
<AutocompleteIndicator />
|
|
</AutocompleteTrigger>
|
|
<AutocompletePopover>
|
|
<AutocompleteFilter>
|
|
<SearchField
|
|
auto-focus
|
|
aria-label="搜索用户"
|
|
name="search"
|
|
variant="secondary"
|
|
>
|
|
<SearchFieldGroup>
|
|
<SearchFieldSearchIcon />
|
|
<SearchFieldInput placeholder="搜索用户…" />
|
|
<SearchFieldClearButton />
|
|
</SearchFieldGroup>
|
|
</SearchField>
|
|
<ListBox>
|
|
<ListBoxItem
|
|
v-for="user in users"
|
|
:id="user.id"
|
|
:key="user.id"
|
|
:text-value="user.name"
|
|
>
|
|
<Avatar size="sm">
|
|
<AvatarImage :src="user.avatarUrl" />
|
|
<AvatarFallback>{{ user.fallback }}</AvatarFallback>
|
|
</Avatar>
|
|
<div class="flex flex-col">
|
|
<Label>{{ user.name }}</Label>
|
|
<Description>{{ user.email }}</Description>
|
|
</div>
|
|
<ListBoxItemIndicator />
|
|
</ListBoxItem>
|
|
<template #empty>
|
|
<EmptyState class="p-1">未找到结果</EmptyState>
|
|
</template>
|
|
</ListBox>
|
|
</AutocompleteFilter>
|
|
</AutocompletePopover>
|
|
</Autocomplete>
|
|
</template>
|