feat(collections): auto incremental async loading, shared filtering and submenu delay

HD-10 scope, against heroui ce90ea3 collection sources and their
react-aria/react-stately dependencies:

- the ComboBox async demo now auto-loads on scroll-to-bottom
  (IntersectionObserver sentinel + fill chain mirroring the pinned
  ListBoxLoadMoreItem re-observe semantics), never duplicates requests
  (loading guards + monotonic sequence token), drops stale responses on
  query switches, keeps a retry path for failures (injectable mock
  failure) and documents its differences from the pinned SWAPI source;
  the Select demo's observer stays untouched (real-network scroll
  triggering verified in browser)
- the Autocomplete virtualization demo and the default filter share one
  locale-sensitive matcher: createBaseContains (Intl.Collator base
  sensitivity, cafe matches Cafe with accent folding) is now exported
  from the package root
- Autocomplete trigger semantics verified complete (single tab stop,
  Enter/Space/Escape/Delete/IME, single and multiple selection) and the
  scoped slots gained composition data instead of child copying:
  AutocompleteValue exposes selectedItem and per-entry source item,
  the ComboBox root exposes selectedItem
- DropdownSubmenuTrigger exposes the pinned delay contract (default
  200ms): the controlled open gate retimes Reka's fixed 100ms hover,
  keyboard opens stay immediate and cancel the pending hover, pointer
  leave cancels; the dead side/align forwarding is gone (Reka fixes the
  submenu side per direction); Button/Avatar trigger layout
  compensation kept after browser proof it is what wins the cascade
- tests/fixtures/collections-rtl.vue drives the real-browser delay and
  RTL geometry checks; b02-pickers contract pins the new export and
  slot typing

Full suite 111 files / 1666 tests, typecheck, build:docs and the
b02-pickers + api-root-aliases tarball contracts pass.
This commit is contained in:
maoyingjie
2026-10-29 13:46:13 +08:00
parent c34faccd70
commit c8d54abefb
21 changed files with 1020 additions and 173 deletions
+110
View File
@@ -218,3 +218,113 @@ describe("ComboBox docs and demos", () => {
mounted.pop();
});
});
describe("asynchronous loading demo", () => {
class FakeIntersectionObserver {
static instances: FakeIntersectionObserver[] = [];
callback: (entries: unknown[]) => void;
constructor(callback: (entries: unknown[]) => void) {
this.callback = callback;
FakeIntersectionObserver.instances.push(this);
}
observe() {}
unobserve() {}
disconnect() {}
trigger(isIntersecting: boolean) {
this.callback([{ isIntersecting }]);
}
static last() {
return FakeIntersectionObserver.instances.at(-1)!;
}
static reset() {
FakeIntersectionObserver.instances = [];
}
}
async function mountAsyncDemo() {
vi.useFakeTimers();
vi.stubGlobal("IntersectionObserver", FakeIntersectionObserver);
FakeIntersectionObserver.reset();
const demo = (
await import("../apps/docs/demos/combo-box-asynchronous-loading.vue")
).default;
const w = mount(demo, { attachTo: document.body });
mounted.push(w);
// Initial page resolves on the simulated latency.
await vi.advanceTimersByTimeAsync(350);
await nextTick();
(w.get('[data-slot="input"]').element as HTMLInputElement).focus();
await nextTick();
return w;
}
const optionNames = () =>
[...document.querySelectorAll('[role="option"]')].map((node) =>
node.textContent?.trim(),
);
afterEach(() => {
vi.useRealTimers();
vi.unstubAllGlobals();
FakeIntersectionObserver.reset();
});
it("auto-loads the next page when the sentinel intersects, without duplicate requests", async () => {
const w = await mountAsyncDemo();
expect(optionNames()).toHaveLength(4);
// Two intersections in the same tick must start one request only: a
// duplicated fetch would append the same offset twice.
FakeIntersectionObserver.last().trigger(true);
FakeIntersectionObserver.last().trigger(true);
await vi.advanceTimersByTimeAsync(350);
await nextTick();
const afterFirst = optionNames();
expect(afterFirst).toHaveLength(8);
expect(new Set(afterFirst).size).toBe(8);
// The chain fills the remaining page while the sentinel stays visible.
await vi.advanceTimersByTimeAsync(350);
await nextTick();
expect(optionNames()).toHaveLength(12);
expect(document.body.textContent).not.toContain("加载更多…");
void w;
});
it("drops stale responses when the query switches mid-flight", async () => {
await mountAsyncDemo();
const input = document.querySelector<HTMLInputElement>(
'[data-slot="input"]',
)!;
input.value = "vader";
input.dispatchEvent(new Event("input", { bubbles: true }));
await vi.advanceTimersByTimeAsync(100);
input.value = "solo";
input.dispatchEvent(new Event("input", { bubbles: true }));
await vi.advanceTimersByTimeAsync(800);
await nextTick();
const names = optionNames();
expect(names).toEqual(["Han Solo"]);
});
it("retries a failed page from the error state", async () => {
const w = await mountAsyncDemo();
const checkbox = w.get('input[type="checkbox"]');
await checkbox.setValue(true);
FakeIntersectionObserver.last().trigger(true);
await vi.advanceTimersByTimeAsync(350);
await nextTick();
// The error row renders inside the teleported popover surface.
expect(document.body.textContent).toContain("重试");
expect(optionNames()).toHaveLength(4);
await checkbox.setValue(false);
const retry = [...document.querySelectorAll("button")].find(
(button) => button.textContent?.trim() === "重试",
)!;
expect(retry).toBeTruthy();
retry.click();
await vi.advanceTimersByTimeAsync(350);
await nextTick();
expect(document.body.textContent).not.toContain("重试");
expect(optionNames()).toHaveLength(8);
void w;
});
});