UNPKG

koval-ui

Version:

React components collection with minimalistic design. Supports theming, layout, and input validation.

1 lines 2.61 kB
{"version":3,"file":"useRowSelection.cjs","sources":["../../../../../src/lib/DataTable/tableFeatures/useRowSelection.ts"],"sourcesContent":["import {useCallback, useEffect, useMemo, useState} from 'react';\nimport type {RowSelectionState, Updater} from '@tanstack/react-table';\n\nimport {ProcessingModes} from './../types.ts';\n\nexport type Props = {\n onRowSelect: (selectionState: RowSelectionState) => void;\n rowSelectionProp: RowSelectionState;\n processingMode: keyof typeof ProcessingModes;\n};\n\nexport const useRowSelection = ({rowSelectionProp, onRowSelect, processingMode}: Props) => {\n const isClient = processingMode === ProcessingModes.internal;\n const [rowSelection, setRowSelection] = useState<RowSelectionState>(rowSelectionProp);\n useEffect(() => {\n setRowSelection(rowSelectionProp);\n }, [rowSelectionProp]);\n\n useEffect(() => {\n onRowSelect(rowSelection);\n }, [onRowSelect, rowSelection]);\n\n const clearSelection = useCallback(() => {\n processingMode === ProcessingModes.internal && setRowSelection({});\n onRowSelect({});\n }, [onRowSelect, processingMode]);\n\n const handleRowSelection = useMemo(() => {\n if (isClient) {\n return setRowSelection;\n } else {\n return (updater: Updater<RowSelectionState>) => {\n if (typeof updater === 'function') {\n onRowSelect(updater(rowSelection) as RowSelectionState);\n } else {\n onRowSelect(updater);\n }\n };\n }\n }, [isClient, onRowSelect, rowSelection]);\n\n return {rowSelection, setRowSelection: handleRowSelection, clearSelection};\n};\n"],"names":["useRowSelection","rowSelectionProp","onRowSelect","processingMode","isClient","ProcessingModes","rowSelection","setRowSelection","useState","useEffect","clearSelection","useCallback","handleRowSelection","useMemo","updater"],"mappings":"gJAWaA,EAAkB,CAAC,CAAC,iBAAAC,EAAkB,YAAAC,EAAa,eAAAC,KAA2B,CACjF,MAAAC,EAAWD,IAAmBE,EAAAA,gBAAgB,SAC9C,CAACC,EAAcC,CAAe,EAAIC,EAAAA,SAA4BP,CAAgB,EACpFQ,EAAAA,UAAU,IAAM,CACZF,EAAgBN,CAAgB,CAAA,EACjC,CAACA,CAAgB,CAAC,EAErBQ,EAAAA,UAAU,IAAM,CACZP,EAAYI,CAAY,CAAA,EACzB,CAACJ,EAAaI,CAAY,CAAC,EAExB,MAAAI,EAAiBC,EAAAA,YAAY,IAAM,CACrCR,IAAmBE,EAAgB,gBAAA,UAAYE,EAAgB,CAAA,CAAE,EACjEL,EAAY,CAAA,CAAE,CAAA,EACf,CAACA,EAAaC,CAAc,CAAC,EAE1BS,EAAqBC,EAAAA,QAAQ,IAC3BT,EACOG,EAECO,GAAwC,CAE5BZ,EADZ,OAAOY,GAAY,WACPA,EAAQR,CAAY,EAEpBQ,CAF0C,CAI9D,EAEL,CAACV,EAAUF,EAAaI,CAAY,CAAC,EAExC,MAAO,CAAC,aAAAA,EAAc,gBAAiBM,EAAoB,eAAAF,CAAc,CAC7E"}