@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
1 lines • 6.21 kB
Source Map (JSON)
{"version":3,"file":"use-list-state.mjs","names":[],"sources":["../../src/use-list-state/use-list-state.ts"],"sourcesContent":["import { useCallback, useMemo, useState } from 'react';\n\nexport interface UseListStateHandlers<T> {\n setState: React.Dispatch<React.SetStateAction<T[]>>;\n append: (...items: T[]) => void;\n prepend: (...items: T[]) => void;\n insert: (index: number, ...items: T[]) => void;\n pop: () => void;\n shift: () => void;\n apply: (fn: (item: T, index?: number) => T) => void;\n applyWhere: (\n condition: (item: T, index: number) => boolean,\n fn: (item: T, index?: number) => T\n ) => void;\n remove: (...indices: number[]) => void;\n reorder: ({ from, to }: { from: number; to: number }) => void;\n swap: ({ from, to }: { from: number; to: number }) => void;\n setItem: (index: number, item: T) => void;\n setItemProp: <K extends keyof T, U extends T[K]>(index: number, prop: K, value: U) => void;\n filter: (fn: (item: T, i: number) => boolean) => void;\n}\n\nexport type UseListStateReturnValue<T> = [T[], UseListStateHandlers<T>];\n\nexport function useListState<T>(initialValue: T[] | (() => T[]) = []): UseListStateReturnValue<T> {\n const [state, setState] = useState(initialValue);\n\n const append = useCallback((...items: T[]) => setState((current) => [...current, ...items]), []);\n\n const prepend = useCallback((...items: T[]) => setState((current) => [...items, ...current]), []);\n\n const insert = useCallback(\n (index: number, ...items: T[]) =>\n setState((current) => [...current.slice(0, index), ...items, ...current.slice(index)]),\n []\n );\n\n const apply = useCallback(\n (fn: (item: T, index?: number) => T) =>\n setState((current) => current.map((item, index) => fn(item, index))),\n []\n );\n\n const remove = useCallback(\n (...indices: number[]) =>\n setState((current) => current.filter((_, index) => !indices.includes(index))),\n []\n );\n\n const pop = useCallback(\n () =>\n setState((current) => {\n const cloned = [...current];\n cloned.pop();\n return cloned;\n }),\n []\n );\n\n const shift = useCallback(\n () =>\n setState((current) => {\n const cloned = [...current];\n cloned.shift();\n return cloned;\n }),\n []\n );\n\n const reorder = useCallback(\n ({ from, to }: { from: number; to: number }) =>\n setState((current) => {\n const cloned = [...current];\n const item = current[from];\n\n cloned.splice(from, 1);\n cloned.splice(to, 0, item);\n\n return cloned;\n }),\n []\n );\n\n const swap = useCallback(\n ({ from, to }: { from: number; to: number }) =>\n setState((current) => {\n const cloned = [...current];\n const fromItem = cloned[from];\n const toItem = cloned[to];\n\n cloned.splice(to, 1, fromItem);\n cloned.splice(from, 1, toItem);\n\n return cloned;\n }),\n []\n );\n\n const setItem = useCallback(\n (index: number, item: T) =>\n setState((current) => {\n const cloned = [...current];\n cloned[index] = item;\n return cloned;\n }),\n []\n );\n\n const setItemProp = useCallback(\n <K extends keyof T, U extends T[K]>(index: number, prop: K, value: U) =>\n setState((current) => {\n const cloned = [...current];\n cloned[index] = { ...cloned[index], [prop]: value };\n return cloned;\n }),\n []\n );\n\n const applyWhere = useCallback(\n (condition: (item: T, index: number) => boolean, fn: (item: T, index?: number) => T) =>\n setState((current) =>\n current.map((item, index) => (condition(item, index) ? fn(item, index) : item))\n ),\n []\n );\n\n const filter = useCallback((fn: (item: T, i: number) => boolean) => {\n setState((current) => current.filter(fn));\n }, []);\n\n const handlers = useMemo(\n () => ({\n setState,\n append,\n prepend,\n insert,\n pop,\n shift,\n apply,\n applyWhere,\n remove,\n reorder,\n swap,\n setItem,\n setItemProp,\n filter,\n }),\n []\n );\n\n return [state, handlers];\n}\n\nexport namespace useListState {\n export type ReturnValue<T> = UseListStateReturnValue<T>;\n export type Handlers<T> = UseListStateHandlers<T>;\n}\n"],"mappings":";;;AAwBA,SAAgB,aAAgB,eAAkC,EAAE,EAA8B;CAChG,MAAM,CAAC,OAAO,YAAY,SAAS,aAAa;CAEhD,MAAM,SAAS,aAAa,GAAG,UAAe,UAAU,YAAY,CAAC,GAAG,SAAS,GAAG,MAAM,CAAC,EAAE,EAAE,CAAC;CAEhG,MAAM,UAAU,aAAa,GAAG,UAAe,UAAU,YAAY,CAAC,GAAG,OAAO,GAAG,QAAQ,CAAC,EAAE,EAAE,CAAC;CAEjG,MAA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