fix(collection): make virtual window, reveal and confirmation honest
- Give the verification instance a real constrained scroll owner and a row height that matches the rendered row, and feed ListBox the full data schema so a row that was never mounted can still be filtered. - Move reveal validity in front of every side effect and re-check after each await: latest token, mounted component, enabled row, still visible and open. Waiting for a virtualized row now requires connected DOM, not just a registration, and closing the collection cancels pending work. - Reconcile the shared active key reactively: a removed, disabled, filtered-out or closed active row is cleared, while the accepted value stays as history. The composite re-validates before confirming, so a stale candidate cannot be submitted. - Read the caller's cancellation before the focus guard so a plain virtual-focus press keeps its pressed state, and use one nested-interactive boundary for both pointer and click, covering contenteditable variants and custom roles. - Render the empty channel for static slot collections too, keeping hidden members mounted. Adds the seven-case round-two probe and twelve regressions; browser checks cover the real virtual window, remote filtering, viewport-contained navigation and filter restoration.
This commit is contained in:
+42
-12
@@ -64,12 +64,16 @@ 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[]>(() =>
|
||||
@@ -91,23 +95,48 @@ const eligible = computed(() =>
|
||||
records.value.filter((record) => !record.disabled && filterRecord(record)),
|
||||
);
|
||||
|
||||
/** Read from the full data set: a filtered-out selection keeps its label. */
|
||||
const acceptedText = computed(
|
||||
() => records.value.find((item) => item.key === accepted.value)?.text ?? null,
|
||||
/**
|
||||
* 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[]) {
|
||||
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;
|
||||
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 = {
|
||||
@@ -140,6 +169,7 @@ const owner: CollectionOwner = {
|
||||
virtualFocus: () => true,
|
||||
activeKey,
|
||||
keyboardIntent,
|
||||
isOpen: () => open.value,
|
||||
};
|
||||
provide(collectionOwnerKey, owner);
|
||||
|
||||
@@ -210,8 +240,8 @@ function keydown(event: KeyboardEvent) {
|
||||
}
|
||||
if (event.key === "Enter") {
|
||||
event.preventDefault();
|
||||
if (activeKey.value !== undefined) request([activeKey.value]);
|
||||
open.value = false;
|
||||
if (activeKey.value !== undefined && request([activeKey.value]))
|
||||
open.value = false;
|
||||
return;
|
||||
}
|
||||
if (event.key === "Escape" || event.key === "Tab") open.value = false;
|
||||
@@ -272,18 +302,18 @@ defineExpose({
|
||||
<ListBox
|
||||
v-if="virtualized"
|
||||
:id="listId"
|
||||
:items="options"
|
||||
:row-height="32"
|
||||
:items="listItems"
|
||||
:row-height="rowHeight"
|
||||
:virtualized="true"
|
||||
class="max-h-[128px] overflow-y-auto"
|
||||
:style="viewportStyle"
|
||||
>
|
||||
<template #default="{ item }">
|
||||
<ListBoxItem
|
||||
:id="item.id"
|
||||
:key="item.id"
|
||||
:is-disabled="item.disabled"
|
||||
:is-disabled="item.isDisabled"
|
||||
>
|
||||
{{ item.text }}
|
||||
{{ item.textValue }}
|
||||
</ListBoxItem>
|
||||
</template>
|
||||
<template #empty>
|
||||
|
||||
Reference in New Issue
Block a user