type SortDirection = "asc" | "desc"; /** * Model to show a list of items. */ interface Model { /** * The collection of items. */ items: TData[]; /** * The key of the currently active sorter. */ currentSorterKey: TSortKey | null; /** * The current sort direction. */ sortDirection: SortDirection; } type SortFunc = (item1: TData, item2: TData) => number; interface Sorter { key: TSortKey; name: string; sorter: SortFunc; } interface Options { /** * A function to sort the items, or an array of `Sorter` objects. */ sorter?: SortFunc | Sorter[]; /** * Gets called when the sorting has changed. */ onUpdateSorting?: (model: TModel, props: TProps, sorting: { key: TSortKey | null; direction: SortDirection; }) => void; /** * Gets called when the used `Sorter` or the sort direction has changed. */ onSorterChanged?: (sorter: Sorter, sortDirection: SortDirection) => void; } type Message = { name: "dataLoaded"; data: TData[]; } | { name: "refresh"; } | { name: "setSorter"; key: TSortKey; toggleDirection?: boolean; } | { name: "setSortDirection"; direction: SortDirection; } | { name: "toggleSortDirection"; } | { name: "setSorting"; key: TSortKey; direction: SortDirection; }; interface Msg { /** * Sets the loaded items. * This message must be called after the data has been loaded. * @param data The loaded items. */ dataLoaded: (data: TData[]) => Message; /** * Refreshes the view. */ refresh: () => Message; /** * Changes the sorting. * @param key The key of the `Sorter` to use. * @param toggleDirection If `true` it toggles the sort direction if the used `Sorter` stays the same. */ setSorter: (key: TSortKey, toggleDirection?: boolean) => Message; /** * Sets the sort direction. * @param direction The new sort direction. */ setSortDirection: (direction: SortDirection) => Message; /** * Toggles the sort direction. */ toggleSortDirection: () => Message; /** * Sets the `Sorter` with the given key. * @param key The key of the `Sorter` to set. * @param direction The sort direction. */ setSorting: (key: TSortKey, direction: SortDirection) => Message; } /** * Creates the `Msg` object with the list message creators. * @returns The `Msg` object. */ declare function createMsg(): Msg; /** * Creates the initial list model. * @param options The list options. * @returns The `init` function. */ declare function createInit(options: Options): () => Model; declare function getSorterByKey(key: string | null, sorters?: SortFunc | Sorter[]): Sorter | null; declare function sortDescending(array: TData[], compareFn: (a: TData, b: TData) => number): TData[]; /** * Resolves the currently active sort function for the given model. * @param currentSorterKey The key of the current sorter. * @param sorter The configured sorter option. * @returns The active sort function, or null if there is none. */ declare function getCurrentSorter(currentSorterKey: TSortKey | null, sorter?: SortFunc | Sorter[]): SortFunc | null; export type { Message, Model, Msg, Options, SortDirection, Sorter, SortFunc }; export { createInit, createMsg, getCurrentSorter, getSorterByKey, sortDescending };