UNPKG

melt

Version:

The next generation of Melt UI. Built for Svelte 5.

144 lines (143 loc) 5.07 kB
import type { IterableProp, MaybeGetter } from ".."; import { SvelteSet } from "svelte/reactivity"; /** * Internal type for the multiple flag constraint * @internal */ type _multiple_extends = boolean; /** * Internal type for the default multiple flag value * @internal */ type _multiple_default = false; /** * Represents the value type for selection state * @template T - The type of values that can be selected * @template Multiple - Boolean flag indicating if multiple selection is enabled */ export type SelectionStateValue<T, Multiple extends _multiple_extends> = Multiple extends true ? SvelteSet<T> : T | undefined; /** * Represents a value or getter for selection state * @template T - The type of values that can be selected * @template Multiple - Boolean flag indicating if multiple selection is enabled */ export type MaybeMultiple<T, Multiple extends _multiple_extends> = Multiple extends true ? IterableProp<T> : MaybeGetter<T | undefined>; /** * Callback type for selection changes * @template T - The type of values that can be selected * @template Multiple - Boolean flag indicating if multiple selection is enabled */ export type OnMultipleChange<T, Multiple extends _multiple_extends> = (value: SelectionStateValue<T, Multiple>) => void; /** * Configuration props for SelectionState * @template T - The type of values that can be selected * @template Multiple - Boolean flag indicating if multiple selection is enabled */ type SelectionStateProps<T, Multiple extends _multiple_extends> = { value?: MaybeMultiple<T, Multiple>; onChange?: OnMultipleChange<T, Multiple>; multiple?: MaybeGetter<Multiple | undefined>; }; /** * Manages selection state with support for single or multiple selection * @template T - The type of values that can be selected (defaults to string) * @template Multiple - Boolean flag indicating if multiple selection is enabled (defaults to false) * * @example * ```ts * // Single string selection * const singleSelect = new SelectionState<string>(); * * // Multiple string selection * const multiSelect = new SelectionState<string, true>({ multiple: true }); * * // Custom type selection * interface User { id: number; name: string } * const userSelect = new SelectionState<User>(); * ``` */ export declare class SelectionState<T, Multiple extends _multiple_extends = _multiple_default> { #private; isControlled: boolean; isMultiple: Multiple; constructor(props: SelectionStateProps<T, Multiple>); /** * Gets the current selection value(s) * @returns For multiple selection, returns a SvelteSet of values. For single selection, returns a single value or undefined. */ get current(): SelectionStateValue<T, Multiple>; /** * Sets the current selection value(s) * @param value - The new selection value(s) */ set current(value: SelectionStateValue<T, Multiple>); /** * Manipulates the selection set through a callback * @param cb - Callback function that receives the selection set for manipulation * @internal */ manipulate(cb: (set: SvelteSet<T>) => void): void; /** * Triggers the onChange callback with the current selection * @param value - The current selection value(s) * @internal */ onChange(value: SelectionStateValue<T, Multiple>): void; /** * Checks if an item is currently selected * @param item - The item to check * @returns True if the item is selected, false otherwise */ has(item: T): boolean; /** * Adds an item to the selection * For single selection, this replaces the current selection * For multiple selection, this adds to the current selection * @param value - The item to add */ add(value: T): void; /** * Adds multiple items to the selection * For single selection, only the first item is selected * For multiple selection, all items are added to the selection * @param items - The items to add */ addAll(items: Iterable<T>): void; /** * Removes an item from the selection * @param value - The item to remove */ delete(value: T): void; /** * Removes multiple items from the selection * @param items - The items to remove */ deleteAll(items: Iterable<T>): void; /** * Clears all selections */ clear(): void; /** * Gets the number of selected items * @returns The number of selected items */ size(): number; /** * Toggles the selection state of an item * If the item is selected, it will be deselected * If the item is not selected, it will be selected * @param item - The item to toggle */ toggle(item: T): void; /** * Converts the current selection to a SvelteSet * @returns A SvelteSet containing the current selection */ toSet(): SvelteSet<T>; /** * Converts the current selection to an array * @returns An array containing the current selection */ toArray(): T[]; } export {};