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

ParametroTipoDescrizione
items() => T[]Getter reattivo degli elementi disponibili
selectionRef<T[] | T | null>Ref della selezione corrente
multiplebooleanModalita multipla (default: false)
dataKeystring | nullProprieta per confronto oggetti (es. 'id')
getOptionValue(option: T) => unknownFunzione custom per estrarre l'identificativo
onSelectionChange(newSelection) => voidCallback al cambio selezione

Valore di Ritorno

ProprietàTipoDescrizione
isSelected(item: T) => booleanVerifica se un elemento e selezionato
toggleSelection(item: T, event?) => voidToggle selezione
select(item: T) => voidAggiunge alla selezione
deselect(item: T) => voidRimuove dalla selezione
selectAll() => voidSeleziona tutti (solo modalita multipla)
clearSelection() => voidDeseleziona tutti
selectionCountComputedRef<number>Numero elementi selezionati
isAllSelectedComputedRef<boolean>True se tutti gli elementi sono selezionati
isIndeterminateComputedRef<boolean>True se parzialmente selezionato
selectedItemsComputedRef<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: selection contiene T | null; select sostituisce la selezione.
  • In modalita multiple: selection contiene T[]; select aggiunge se non gia presente.
  • Il confronto usa la priorita: getOptionValue > dataKey > identita per riferimento.
  • selectedItems normalizza sempre a array, anche in modalita singola (utile per rendering uniforme).
  • isIndeterminate e utile per la checkbox "seleziona tutto" in stato intermedio.