Common
useSelectionManager
Composable interno per selezione singola/multipla in liste con confronto per dataKey e range selection.
Composable interno — utilizzato dai componenti di selezione della libreria (OrderList, PickList, ecc.).
Firma TypeScript
export function useSelectionManager<T>(
options: UseSelectionManagerOptions<T>
): UseSelectionManagerResult<T>
export interface UseSelectionManagerOptions<T> {
items: () => T[];
selection: Ref<T[] | T | null>;
multiple?: boolean; // default: false
dataKey?: string | null;
getOptionValue?: (option: T) => unknown;
onSelectionChange?: (newSelection: T[] | T | null) => void;
}
export interface UseSelectionManagerResult<T> {
isSelected: (item: T) => boolean;
toggleSelection: (item: T, event?: MouseEvent | KeyboardEvent) => void;
select: (item: T) => void;
deselect: (item: T) => void;
selectAll: () => void;
clearSelection: () => void;
selectionCount: ComputedRef<number>;
isAllSelected: ComputedRef<boolean>;
isIndeterminate: ComputedRef<boolean>;
selectedItems: ComputedRef<T[]>;
}
Parametri
| Parametro | Tipo | Descrizione |
|---|---|---|
items | () => T[] | Getter reattivo degli elementi disponibili |
selection | Ref<T[] | T | null> | Ref della selezione corrente |
multiple | boolean | Modalita multipla (default: false) |
dataKey | string | null | Proprieta per confronto oggetti (es. 'id') |
getOptionValue | (option: T) => unknown | Funzione custom per estrarre l'identificativo |
onSelectionChange | (newSelection) => void | Callback al cambio selezione |
Valore di Ritorno
| Proprietà | Tipo | Descrizione |
|---|---|---|
isSelected | (item: T) => boolean | Verifica se un elemento e selezionato |
toggleSelection | (item: T, event?) => void | Toggle selezione |
select | (item: T) => void | Aggiunge alla selezione |
deselect | (item: T) => void | Rimuove dalla selezione |
selectAll | () => void | Seleziona tutti (solo modalita multipla) |
clearSelection | () => void | Deseleziona tutti |
selectionCount | ComputedRef<number> | Numero elementi selezionati |
isAllSelected | ComputedRef<boolean> | True se tutti gli elementi sono selezionati |
isIndeterminate | ComputedRef<boolean> | True se parzialmente selezionato |
selectedItems | ComputedRef<T[]> | Array normalizzato degli elementi selezionati |
Esempio
// Selezione multipla
const selection = ref<Product[]>([])
const { isSelected, toggleSelection, selectAll, clearSelection } = useSelectionManager({
items: () => products.value,
selection,
multiple: true,
dataKey: 'id',
onSelectionChange: (newSel) => emit('update:selection', newSel)
})
// Selezione singola
const selected = ref<Product | null>(null)
const { isSelected: isSingleSelected } = useSelectionManager({
items: () => products.value,
selection: selected,
multiple: false,
dataKey: 'id',
})
Note
- In modalita single:
selectioncontieneT | null;selectsostituisce la selezione. - In modalita multiple:
selectioncontieneT[];selectaggiunge se non gia presente. - Il confronto usa la priorita:
getOptionValue>dataKey> identita per riferimento. selectedItemsnormalizza sempre a array, anche in modalita singola (utile per rendering uniforme).isIndeterminatee utile per la checkbox "seleziona tutto" in stato intermedio.