merge: integrate public collection picker migration

# Conflicts:
#	AGENTS.md
#	docs/compatibility.md
#	docs/upstream.md
#	docs/verification.md
#	package.json
#	packages/styles/src/index.ts
#	packages/vue/src/index.ts
#	packages/vue/src/types.ts
#	tests/package/README.md
This commit is contained in:
maoyingjie
2026-10-25 20:55:18 +08:00
201 changed files with 25848 additions and 48 deletions
+338
View File
@@ -0,0 +1,338 @@
<script setup lang="ts">
/**
* B02a verification harness: a minimal combobox that consumes the optional
* owner protocol (filter + virtual focus). It proves the protocol; it is NOT
* the public ComboBox.
*/
import {
computed,
nextTick,
provide,
ref,
shallowRef,
useId,
watchEffect,
} from "vue";
import { ListBox, ListBoxItem } from "@heroui-vue/components";
import {
collectionOwnerKey,
type CollectionOwner,
type CollectionRecord,
type OwnedCollection,
} from "../../src/components/collection/owner";
import type { CollectionKey } from "../../src/components/collection/keys";
interface Option {
id: CollectionKey;
text: string;
disabled?: boolean;
}
const props = withDefaults(
defineProps<{
options?: readonly Option[];
/** When false the parent rejects every selection proposal. */
acceptSelection?: boolean;
filterEnabled?: boolean;
/** Render the collection through ListBox's virtualized items mode. */
virtualized?: boolean;
}>(),
{
options: () => [
{ id: "cat", text: "猫" },
{ id: "dog", text: "狗" },
{ id: "bird", text: "鸟" },
{ id: "fish", text: "鱼" },
{ id: "hamster", text: "仓鼠" },
],
acceptSelection: true,
filterEnabled: true,
virtualized: false,
},
);
const emit = defineEmits<{
"update:modelValue": [key: CollectionKey | null];
proposal: [key: CollectionKey | null];
}>();
// Instance-unique ids: several harnesses coexist on the verification page.
const uid = useId();
const labelId = `${uid}-label`;
const inputId = `${uid}-input`;
const listId = `${uid}-list`;
const input = ref<HTMLInputElement>();
const inputValue = ref("");
const accepted = ref<CollectionKey | null>(null);
const acceptedLabel = ref<string | null>(null);
const activeKey = ref<CollectionKey>();
const activeId = ref<string>();
const open = ref(false);
const composing = ref(false);
/** Shared modality: ListBox clears it on pointer presses, keys restore it. */
const keyboardIntent = ref(true);
/** Real constrained scroll owner for the virtualized instance. */
const rowHeight = 36;
const viewportStyle = `max-height: ${rowHeight * 4}px; overflow-y: auto;`;
const collection = shallowRef<OwnedCollection>();
const records = computed<CollectionRecord[]>(() =>
props.options.map((option) => ({
key: option.id,
text: option.text,
disabled: !!option.disabled,
})),
);
/** Filtering affects display and navigation only; accepted values stay. */
function filterRecord(record: CollectionRecord) {
if (!props.filterEnabled) return true;
const needle = inputValue.value.trim();
return !needle || record.text.includes(needle);
}
const eligible = computed(() =>
records.value.filter((record) => !record.disabled && filterRecord(record)),
);
/**
* ListBox `items` schema. Every record carries its own textValue so the full
* data set can filter rows that were never mounted in a virtual window.
*/
const listItems = computed(() =>
props.options.map((option) => ({
id: option.id,
textValue: option.text,
isDisabled: !!option.disabled,
})),
);
/** Read from the full data set: a filtered-out selection keeps its label. */
const acceptedText = computed(() => {
if (accepted.value === null) return null;
// An accepted value survives its row being removed or filtered out; it is
// history, not the active candidate.
const record = records.value.find((item) => item.key === accepted.value);
return record?.text ?? acceptedLabel.value ?? String(accepted.value);
});
const hiddenCount = computed(
() => records.value.filter((record) => !filterRecord(record)).length,
);
function request(keys: CollectionKey[]): boolean {
const next = keys[0] ?? null;
// Confirm only what is still a real candidate: a row removed, disabled or
// filtered out since it became active cannot be submitted.
if (next !== null) {
const candidate = eligible.value.find((record) => record.key === next);
if (!open.value || !candidate) return false;
}
emit("proposal", next);
if (!props.acceptSelection) return false;
accepted.value = next;
const record = records.value.find((item) => item.key === next);
// Remember the label: history outlives the row leaving the collection.
acceptedLabel.value = record?.text ?? null;
if (record) inputValue.value = record.text;
emit("update:modelValue", next);
return true;
}
const owner: CollectionOwner = {
keys: computed(() => (accepted.value === null ? [] : [accepted.value])),
mode: computed(() => "single" as const),
disabled: computed(() => false),
readOnly: computed(() => false),
disabledKeys: computed(() =>
records.value
.filter((record) => record.disabled)
.map((record) => record.key),
),
id: listId,
labelledBy: () => labelId,
ariaLabel: () => undefined,
request,
// The harness stays open after a pick so the active-key/id sync is observable;
// closing behaviour belongs to the real ComboBox.
action: () => {},
leave: () => {
open.value = false;
},
connect: (next: OwnedCollection) => {
collection.value = next;
return () => {
if (collection.value === next) collection.value = undefined;
};
},
filter: filterRecord,
virtualFocus: () => true,
activeKey,
keyboardIntent,
isOpen: () => open.value,
};
provide(collectionOwnerKey, owner);
/**
* The id is only usable when the list is open and the row is mounted, visible
* and not filtered out; otherwise the attribute must disappear.
*/
function resolveActiveId(): string | undefined {
if (!open.value) return undefined;
const key = activeKey.value;
if (key === undefined) return undefined;
const id = collection.value?.optionId?.(key);
if (!id) return undefined;
const element =
typeof document === "undefined" ? null : document.getElementById(id);
if (!element || element.hasAttribute("data-filtered")) return undefined;
return id;
}
// One derivation for every path that can move the active key: keyboard,
// pointer clicks inside the list, filtering and open/close.
watchEffect(() => {
void eligible.value.length;
void collection.value;
activeId.value = resolveActiveId();
});
async function setActive(key: CollectionKey | undefined) {
activeKey.value = key;
if (key !== undefined) {
// Reveal first (mounts a virtualized row); the effect above re-reads the id
// once the row is actually mounted.
await collection.value?.reveal?.(key);
}
activeId.value = resolveActiveId();
}
function moveActive(delta: 1 | -1) {
const items = eligible.value;
if (!items.length) {
void setActive(undefined);
return;
}
const index = items.findIndex((item) => item.key === activeKey.value);
const next =
index < 0
? items[delta > 0 ? 0 : items.length - 1]
: items[(index + delta + items.length) % items.length];
void setActive(next.key);
}
function onInput() {
if (composing.value) return;
inputValue.value = input.value?.value ?? "";
open.value = true;
void setActive(eligible.value[0]?.key);
}
function keydown(event: KeyboardEvent) {
// IME: never confirm while composing (Safari reports keyCode 229).
if (event.isComposing || composing.value || event.keyCode === 229) return;
keyboardIntent.value = true;
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
event.preventDefault();
if (!open.value) open.value = true;
moveActive(event.key === "ArrowDown" ? 1 : -1);
return;
}
if (event.key === "Enter") {
event.preventDefault();
if (activeKey.value !== undefined && request([activeKey.value]))
open.value = false;
return;
}
if (event.key === "Escape" || event.key === "Tab") open.value = false;
}
function compositionStart() {
composing.value = true;
}
function compositionEnd() {
composing.value = false;
onInput();
void nextTick();
}
defineExpose({
inputValue,
accepted,
activeKey,
activeId,
open,
eligible: () => eligible.value,
hiddenCount: () => hiddenCount.value,
});
</script>
<template>
<div class="flex w-[256px] flex-col gap-1">
<label :id="labelId" class="text-sm font-medium" :for="inputId">
动物(B02a 协议验证)
</label>
<input
:id="inputId"
ref="input"
:value="inputValue"
aria-autocomplete="list"
:aria-activedescendant="activeId"
:aria-controls="listId"
:aria-expanded="open"
:aria-labelledby="labelId"
autocomplete="off"
class="rounded-field border border-field-border bg-field px-3 py-2 text-sm"
data-test="harness-input"
role="combobox"
type="text"
@compositionend="compositionEnd"
@compositionstart="compositionStart"
@input="onInput"
@keydown="keydown"
/>
<p class="text-xs text-muted" data-test="accepted">
已选:{{ acceptedText ?? "无" }}
</p>
<p class="text-xs text-muted" data-test="hidden-count">
已隐藏:{{ hiddenCount }}
</p>
<div v-show="open" data-test="popover">
<ListBox
v-if="virtualized"
:id="listId"
:items="listItems"
:row-height="rowHeight"
:virtualized="true"
:style="viewportStyle"
>
<template #default="{ item }">
<ListBoxItem
:id="item.id"
:key="item.id"
:is-disabled="item.isDisabled"
>
{{ item.textValue }}
</ListBoxItem>
</template>
<template #empty>
<p data-test="empty">没有匹配项</p>
</template>
</ListBox>
<ListBox v-else :id="listId">
<ListBoxItem
v-for="option in options"
:id="option.id"
:key="option.id"
:is-disabled="option.disabled"
>
{{ option.text }}
</ListBoxItem>
<template #empty>
<p data-test="empty">没有匹配项</p>
</template>
</ListBox>
</div>
</div>
</template>