Files
heroui-vue/tests/package/b02-pickers-contract.ts
T
maoyingjie c8d54abefb 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.
2026-10-29 13:46:34 +08:00

259 lines
8.5 KiB
TypeScript

import { h } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
ComboBoxValue,
ListBox,
ListBoxItem,
createBaseContains,
type AutocompleteItem,
type AutocompleteProps,
type AutocompleteState,
type AutocompleteValueState,
type AutocompleteValueProps,
type ComboBoxItem,
type ComboBoxProps,
type ComboBoxState,
type ComboBoxValueProps,
type ComboBoxValueState,
type ComboBoxModelValue,
type CollectionKey,
type ValidateResult,
} from "@heroui-vue/components";
// ---------------------------------------------------------------------------
// Keys: numbers and strings stay distinct, null means "no selection".
// ---------------------------------------------------------------------------
const stringKey: ComboBoxModelValue = "cat";
const numericKey: ComboBoxModelValue = 0;
const emptyKey: ComboBoxModelValue = "";
const cleared: ComboBoxModelValue = null;
const multiple: ComboBoxModelValue = ["cat", 0];
// @ts-expect-error a key is a string or a finite number
const objectKey: ComboBoxModelValue = { id: "cat" };
// @ts-expect-error undefined is not a selection state; use null
const missingKey: ComboBoxModelValue = undefined;
// ---------------------------------------------------------------------------
// ComboBox props
// ---------------------------------------------------------------------------
const comboProps: ComboBoxProps = {
modelValue: stringKey,
defaultValue: numericKey,
selectionMode: "multiple",
inputValue: "",
defaultInputValue: "",
items: [{ id: 0, textValue: "Zero", isDisabled: false }],
disabledKeys: ["a", 0, ""],
open: false,
defaultOpen: false,
isDisabled: false,
isReadOnly: false,
isRequired: false,
isInvalid: false,
allowsCustomValue: true,
allowsEmptyCollection: true,
menuTrigger: "manual",
shouldFocusWrap: true,
fullWidth: true,
variant: "secondary",
name: "animal",
form: "external-form",
formValue: "text",
autoComplete: "off",
autoFocus: true,
validationBehavior: "aria",
placeholder: "Select an item",
as: "section",
asChild: false,
class: ["hv-combo-box", { "is-dense": true }],
};
// @ts-expect-error selectionMode is a closed union
const badMode: ComboBoxProps = { selectionMode: "triple" };
// @ts-expect-error variant is a closed union
const badVariant: ComboBoxProps = { variant: "tertiary" };
// @ts-expect-error formValue only allows key or text
const badFormValue: ComboBoxProps = { formValue: "both" };
// `validate` keeps its parameter and result types.
const comboWithValidate: ComboBoxProps = {
validate: (value) => {
const accepted: ComboBoxModelValue = value;
void accepted;
return true;
},
};
// @ts-expect-error validate must return a validation result, not a boolean flag object
const badValidate: ComboBoxProps = { validate: () => ({ ok: true }) };
// ---------------------------------------------------------------------------
// Autocomplete props, including the source `items` mapping
// ---------------------------------------------------------------------------
const autocompleteItems: AutocompleteItem[] = [
{ id: "california", textValue: "California" },
{ id: 0, textValue: "Zero", isDisabled: true },
];
// @ts-expect-error items need a textValue for filtering and display
const itemsWithoutText: AutocompleteItem[] = [{ id: "a" }];
const autocompleteProps: AutocompleteProps = {
modelValue: cleared,
value: multiple,
defaultValue: ["a", "b"],
items: autocompleteItems,
selectionMode: "multiple",
disabledKeys: ["a"],
open: true,
isOpen: true,
defaultOpen: false,
isDisabled: false,
isRequired: true,
isInvalid: false,
allowsEmptyCollection: false,
placeholder: "Select an item",
name: "state",
form: "external-form",
fullWidth: true,
variant: "primary",
validationBehavior: "native",
as: "div",
asChild: true,
validate: (value): ValidateResult => (Array.isArray(value) ? true : null),
};
const badAutocompleteValidate: AutocompleteProps = {
// @ts-expect-error validate must not widen to an arbitrary return type
validate: () => Symbol("nope"),
};
// ---------------------------------------------------------------------------
// Events keep their payload types
// ---------------------------------------------------------------------------
const comboEvents = h(ComboBox, {
"onUpdate:modelValue": (value: ComboBoxModelValue) => void value,
onChange: (value: ComboBoxModelValue) => void value,
"onUpdate:selectedKey": (key: CollectionKey | null) => void key,
selectionChange: (key: CollectionKey | null) => void key,
"onUpdate:inputValue": (text: string) => void text,
inputChange: (text: string) => void text,
"onUpdate:open": (open: boolean) => void open,
openChange: (open: boolean) => void open,
});
const badComboEvent = h(ComboBox, {
// @ts-expect-error the model event carries the value, never the input text
"onUpdate:modelValue": (text: string) => void text,
});
// Root scoped slots expose the open/selection state.
const sharedMatcher: (text: string, query: string) => boolean =
createBaseContains("zh-CN");
void sharedMatcher("Café", "cafe");
const comboState = (state: ComboBoxState): string =>
`${state.isOpen}:${state.inputValue}:${state.selectedKeys.length}:${state.selectedItem?.textValue ?? ""}`;
const autocompleteState = (state: AutocompleteState): string =>
`${state.isOpen}:${state.selectedKeys.length}`;
const comboValueState = (state: ComboBoxValueState): string =>
`${state.isPlaceholder}:${state.selectedItems.length}`;
const autocompleteEvents = h(Autocomplete, {
"onUpdate:modelValue": (value: ComboBoxModelValue) => void value,
onChange: (value: ComboBoxModelValue) => void value,
"onUpdate:open": (open: boolean) => void open,
openChange: (open: boolean) => void open,
clear: () => undefined,
});
// ---------------------------------------------------------------------------
// Parts: every public part accepts the documented props and slots
// ---------------------------------------------------------------------------
const comboTree = h(ComboBox, { "aria-label": "动物" }, () => [
h(ComboBoxInputGroup, { class: "w-full" }, () => "input"),
h(ComboBoxValue, { placeholder: "未选择" }),
h(ComboBoxPopover, { placement: "top", offset: 4, shouldFlip: false }, () =>
h(ListBox, { "aria-label": "动物" }, () =>
h(ListBoxItem, { id: 0, textValue: "Zero" }, () => "Zero"),
),
),
h(ComboBoxTrigger, { isDisabled: true }),
]);
// @ts-expect-error placement is a closed union
const badPlacement = h(ComboBoxPopover, { placement: "middle" });
const autocompleteTree = h(Autocomplete, { "aria-label": "州" }, () => [
h(AutocompleteTrigger, { isDisabled: false }, () => [
h(AutocompleteValue, null, {
// Scoped slot state keeps its documented, exported shape.
default: (state: AutocompleteValueState) => {
const placeholder: boolean = state.isPlaceholder;
const joined: string = state.selectedText || state.defaultChildren;
const keys = state.selectedItems.map((item) => item.key);
// Source `items` data rides along for rich trigger composition.
const source = state.selectedItem?.item?.textValue ?? "";
void source;
return placeholder ? joined : keys.join(",");
},
}),
h(AutocompleteClearButton),
h(AutocompleteIndicator),
]),
h(AutocompletePopover, { placement: "bottom end" }, () =>
h(
AutocompleteFilter,
{ filter: (text, input) => text.includes(input) },
() =>
h(ListBox, null, () =>
h(ListBoxItem, { id: "a", textValue: "A" }, () => "A"),
),
),
),
]);
const valueProps: ComboBoxValueProps = { placeholder: "未选择" };
const autocompleteValueProps: AutocompleteValueProps = {};
// @ts-expect-error ComboBoxValue has no `items` prop
const badValueProps: ComboBoxValueProps = { items: [] };
const item: ComboBoxItem = { id: 0, textValue: "Zero" };
const autocompleteFirstItem: AutocompleteItem = autocompleteItems[0]!;
export {
comboState,
comboValueState,
autocompleteState,
autocompleteEvents,
autocompleteFirstItem,
autocompleteProps,
autocompleteTree,
autocompleteValueProps,
badAutocompleteValidate,
badComboEvent,
badFormValue,
badMode,
badPlacement,
badValidate,
badValueProps,
badVariant,
cleared,
comboEvents,
comboProps,
comboTree,
comboWithValidate,
emptyKey,
item,
itemsWithoutText,
missingKey,
multiple,
numericKey,
objectKey,
stringKey,
valueProps,
};