103 lines
3.4 KiB
TypeScript
103 lines
3.4 KiB
TypeScript
import { computed, shallowRef } from "vue";
|
|
import { assertKey, copyKeys, type CollectionKey } from "./keys";
|
|
|
|
export type CollectionSelectionMode = "none" | "single" | "multiple";
|
|
|
|
interface SelectionOptions {
|
|
mode: () => CollectionSelectionMode;
|
|
/** Presence is explicit: a controlled empty/undefined model is still controlled. */
|
|
controlled: () => boolean;
|
|
value: () => readonly CollectionKey[] | undefined;
|
|
defaultValue?: readonly CollectionKey[];
|
|
disabled?: () => boolean;
|
|
readOnly?: () => boolean;
|
|
disabledKeys?: () => readonly CollectionKey[];
|
|
disallowEmptySelection?: () => boolean;
|
|
onChange: (keys: CollectionKey[]) => void;
|
|
}
|
|
|
|
/** Accepted selection only. Focus, actions, form serialization and range anchors
|
|
* belong to the consuming composite, not a second optimistic selection store. */
|
|
export function useCollectionSelection(options: SelectionOptions) {
|
|
const initial = copyKeys(options.defaultValue ?? []);
|
|
const internal = shallowRef([...initial]);
|
|
const keys = computed(() =>
|
|
Object.freeze(
|
|
copyKeys(options.controlled() ? (options.value() ?? []) : internal.value),
|
|
),
|
|
);
|
|
const selected = (key: CollectionKey) => keys.value.includes(key);
|
|
const disabled = (key: CollectionKey) =>
|
|
!!options.disabled?.() || !!options.disabledKeys?.().includes(key);
|
|
const editable = () =>
|
|
options.mode() !== "none" && !options.disabled?.() && !options.readOnly?.();
|
|
|
|
function request(next: readonly CollectionKey[]): boolean {
|
|
if (!editable()) return false;
|
|
const proposal = copyKeys(next);
|
|
if (options.mode() === "single" && proposal.length > 1) {
|
|
throw new TypeError("Single selection accepts at most one key.");
|
|
}
|
|
// A disabled key cannot be newly selected. Replacement may still leave an
|
|
// initially selected disabled option, as with a native single select.
|
|
if (proposal.some((key) => disabled(key) && !selected(key))) {
|
|
return false;
|
|
}
|
|
if (!proposal.length && options.disallowEmptySelection?.()) return false;
|
|
if (
|
|
proposal.length === keys.value.length &&
|
|
proposal.every((key) => selected(key))
|
|
) {
|
|
return false;
|
|
}
|
|
if (!options.controlled()) internal.value = [...proposal];
|
|
// An emitted array cannot mutate either props or accepted internal state.
|
|
options.onChange([...proposal]);
|
|
return true;
|
|
}
|
|
|
|
function toggle(key: CollectionKey): boolean {
|
|
assertKey(key);
|
|
if (disabled(key)) return false;
|
|
return request(
|
|
selected(key)
|
|
? keys.value.filter((item) => item !== key)
|
|
: options.mode() === "multiple"
|
|
? [...keys.value, key]
|
|
: [key],
|
|
);
|
|
}
|
|
|
|
function replace(key: CollectionKey): boolean {
|
|
assertKey(key);
|
|
return !disabled(key) && request([key]);
|
|
}
|
|
|
|
/** Eligible keys come from the consumer's data set, not necessarily mounted DOM.
|
|
* Existing selected disabled keys survive select-all. */
|
|
function selectAll(availableKeys: readonly CollectionKey[]): boolean {
|
|
if (options.mode() !== "multiple") return false;
|
|
return request([
|
|
...keys.value.filter(disabled),
|
|
...availableKeys.filter((key) => !disabled(key)),
|
|
]);
|
|
}
|
|
|
|
/** Called only after a non-cancelled native reset by the field adapter.
|
|
* Reset is not an edit and never emits a change proposal. */
|
|
function reset(): void {
|
|
if (!options.controlled()) internal.value = [...initial];
|
|
}
|
|
|
|
return {
|
|
keys,
|
|
selected,
|
|
disabled,
|
|
request,
|
|
toggle,
|
|
replace,
|
|
selectAll,
|
|
reset,
|
|
};
|
|
}
|