e34b9debdf
Restore native clearing, unify the form/field protocol with the other choice families (per-key entries, disabled and removed keys omitted, shared form owner), synchronise ComboBox text by key identity, keep filtering out of open composition sessions, and stop stealing focus when a controlled close is rejected. Also merge caller refs/ARIA/disabled, implement asChild, autoFocus and ComboBox variant, use a locale-aware base matcher, and put the indicator class and slot on the icon element the ported CSS selects.
252 lines
8.2 KiB
TypeScript
252 lines
8.2 KiB
TypeScript
import { h } from "vue";
|
|
import {
|
|
Autocomplete,
|
|
AutocompleteClearButton,
|
|
AutocompleteFilter,
|
|
AutocompleteIndicator,
|
|
AutocompletePopover,
|
|
AutocompleteTrigger,
|
|
AutocompleteValue,
|
|
ComboBox,
|
|
ComboBoxInputGroup,
|
|
ComboBoxPopover,
|
|
ComboBoxTrigger,
|
|
ComboBoxValue,
|
|
ListBox,
|
|
ListBoxItem,
|
|
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 comboState = (state: ComboBoxState): string =>
|
|
`${state.isOpen}:${state.inputValue}:${state.selectedKeys.length}`;
|
|
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);
|
|
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,
|
|
};
|