UNPKG

@humanspeak/svelte-headless-table

Version:

A powerful, headless table library for Svelte that provides complete control over table UI while handling complex data operations like sorting, filtering, pagination, grouping, and row expansion. Build custom, accessible data tables with zero styling opin

145 lines (144 loc) 5.34 kB
import { type Readable, type Writable } from 'svelte/store'; /** Union type representing either a Readable or Writable Svelte store. */ export type ReadOrWritable<T> = Readable<T> | Writable<T>; /** * Type guard that checks if a value is a Svelte Readable store. * * @template T - The type of the store's value. * @param value - The value to check. * @returns True if the value has a subscribe method. * @example * ```typescript * if (isReadable(maybeStore)) { * maybeStore.subscribe(value => console.log(value)) * } * ``` */ export declare const isReadable: <T>(value: any) => value is Readable<T>; /** * Type guard that checks if a value is a Svelte Writable store. * * @template T - The type of the store's value. * @param store - The value to check. * @returns True if the value has both update and set methods. * @example * ```typescript * if (isWritable(maybeStore)) { * maybeStore.set(newValue) * } * ``` */ export declare const isWritable: <T>(store: any) => store is Writable<T>; /** * Maps each key of T to a Writable store containing that key's value type. * @template T - The object type to map. */ export type WritableKeys<T> = { [K in keyof T]: T[K] extends undefined ? Writable<T[K] | undefined> : Writable<T[K]>; }; /** * Maps each key of T to a Readable store containing that key's value type. * @template T - The object type to map. */ export type ReadableKeys<T> = { [K in keyof T]: T[K] extends undefined ? Readable<T[K] | undefined> : Readable<T[K]>; }; /** * Maps each key of T to either a Readable or Writable store. * @template T - The object type to map. */ export type ReadOrWritableKeys<T> = { [K in keyof T]: T[K] extends undefined ? ReadOrWritable<T[K] | undefined> : ReadOrWritable<T[K]>; }; /** A readable store that always contains undefined. */ export declare const Undefined: Readable<undefined>; /** * Returns the Undefined store typed as a Readable of type T. * Useful for providing a default store value. * * @template T - The type to cast the undefined store to. * @returns A Readable store typed as Readable<T>. */ export declare const UndefinedAs: <T>() => Readable<T>; /** * Options for toggle operations in set stores. */ export interface ToggleOptions { /** If true, removes all other items when toggling an item on. */ clearOthers?: boolean; } /** * Configuration options for creating an ArraySetStore. * @template T - The type of elements in the array. */ export interface ArraySetStoreOptions<T> { /** Custom equality function for comparing items. Defaults to strict equality. */ isEqual?: (_a: T, _b: T) => boolean; } /** * A Svelte store that maintains a unique set of items as an array. * Extends Writable with set-like operations. * * @template T - The type of elements in the set. */ export interface ArraySetStore<T> extends Writable<T[]> { /** Toggles an item in the set (adds if absent, removes if present). */ toggle: (_item: T, _options?: ToggleOptions) => void; /** Adds an item to the set if not already present. */ add: (_item: T) => void; /** Removes an item from the set if present. */ remove: (_item: T) => void; /** Removes all items from the set. */ clear: () => void; } /** * Creates a Svelte store that maintains a unique set of items as an array. * Supports toggle, add, remove, and clear operations. * * @template T - The type of elements in the set. * @param initial - The initial array of items. * @param options - Configuration options including custom equality function. * @returns An ArraySetStore with set-like operations. * @example * ```typescript * const selectedIds = arraySetStore<string>(['id1']) * selectedIds.toggle('id2') // Adds 'id2' * selectedIds.toggle('id1') // Removes 'id1' * ``` */ export declare const arraySetStore: <T>(initial?: T[], { isEqual }?: ArraySetStoreOptions<T>) => ArraySetStore<T>; /** * A Svelte store that maintains a set using a Record with boolean values. * More efficient for string/number keys than ArraySetStore. * * @template T - The type of keys (must be string or number). */ export interface RecordSetStore<T extends string | number> extends Writable<Record<T, boolean>> { /** Toggles a key in the set (adds if absent, removes if present). */ toggle: (_item: T) => void; /** Adds a key to the set. */ add: (_item: T) => void; /** Adds multiple keys to the set. */ addAll: (_items: T[]) => void; /** Removes a key from the set. */ remove: (_item: T) => void; /** Removes multiple keys from the set. */ removeAll: (_items: T[]) => void; /** Removes all keys from the set. */ clear: () => void; } /** * Creates a Svelte store that maintains a set using a Record with boolean values. * False values are automatically removed to keep the record clean. * * @template T - The type of keys (must be string or number). * @param initial - The initial record of key-boolean pairs. * @returns A RecordSetStore with set-like operations. * @example * ```typescript * const expandedIds = recordSetStore<string>({ row1: true }) * expandedIds.toggle('row2') // Adds 'row2' * expandedIds.toggle('row1') // Removes 'row1' * ``` */ export declare const recordSetStore: <T extends string | number>(initial?: Record<T, boolean>) => RecordSetStore<T>;