update table
This commit is contained in:
@@ -13,8 +13,10 @@ import {
|
||||
} from '@tanstack/react-table'
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
type ReactNode
|
||||
} from 'react'
|
||||
@@ -41,7 +43,7 @@ interface DataTableProps<TData, TValue> {
|
||||
sort: SortingState
|
||||
pageSize: number
|
||||
rowCount: number
|
||||
hiddenColumn?: string[]
|
||||
columnInvisibilityInit?: string[]
|
||||
}
|
||||
|
||||
interface TableContextProps<TData> {
|
||||
@@ -69,88 +71,126 @@ export function DataTable<TData, TValue>({
|
||||
pageSize,
|
||||
rowCount,
|
||||
setSelectedRows,
|
||||
hiddenColumn = []
|
||||
columnInvisibilityInit = []
|
||||
}: DataTableProps<TData, TValue>) {
|
||||
const [dataTable, setDataTable] = useState<TData[]>(data)
|
||||
const columnVisibilityInit = Object.fromEntries(
|
||||
hiddenColumn.map((column) => [column, false])
|
||||
const columnInvisibility = useMemo<VisibilityState>(
|
||||
() =>
|
||||
Object.fromEntries(
|
||||
columnInvisibilityInit.map((column) => [column, false])
|
||||
),
|
||||
[columnInvisibilityInit]
|
||||
)
|
||||
const [searchParams, setSearchParams] = useSearchParams()
|
||||
const [columnVisibility, setColumnVisibility] =
|
||||
useState<VisibilityState>(columnVisibilityInit)
|
||||
useState<VisibilityState>(columnInvisibility)
|
||||
const [rowSelection, setRowSelection] = useState<RowSelectionState>({})
|
||||
const sortParam = searchParams.get('sort')
|
||||
const sorting = sortParam
|
||||
? sortParam.split(',').map((s) => {
|
||||
const [id, dir] = s.split(':')
|
||||
return { id, desc: dir === 'desc' }
|
||||
|
||||
const sorting = useMemo(() => {
|
||||
const sortParam = searchParams.get('sort')
|
||||
|
||||
if (!sortParam) {
|
||||
return sort
|
||||
}
|
||||
|
||||
return sortParam.split(',').map((s) => {
|
||||
const [id, dir] = s.split(':')
|
||||
return { id, desc: dir === 'desc' }
|
||||
})
|
||||
}, [searchParams, sort])
|
||||
|
||||
const setPagination = useCallback(
|
||||
(updater: any) => {
|
||||
const newState =
|
||||
typeof updater === 'function'
|
||||
? updater({ pageIndex, pageSize })
|
||||
: updater
|
||||
|
||||
setRowSelection({})
|
||||
setSearchParams((prev) => {
|
||||
prev.set('p', newState?.pageIndex.toString())
|
||||
prev.set('perPage', newState?.pageSize.toString())
|
||||
return prev
|
||||
})
|
||||
: sort
|
||||
},
|
||||
[pageIndex, pageSize, setSearchParams]
|
||||
)
|
||||
|
||||
const setPagination = (updater: any) => {
|
||||
const newState =
|
||||
typeof updater === 'function' ? updater({ pageIndex, pageSize }) : updater
|
||||
|
||||
setRowSelection({})
|
||||
setSearchParams((searchParams) => {
|
||||
searchParams.set('p', newState?.pageIndex.toString())
|
||||
searchParams.set('perPage', newState?.pageSize.toString())
|
||||
return searchParams
|
||||
})
|
||||
}
|
||||
|
||||
const setSorting = (updater: any) => {
|
||||
const newSorting =
|
||||
typeof updater === 'function' ? updater(sorting) : updater
|
||||
|
||||
setSearchParams((searchParams) => {
|
||||
if (newSorting.length) {
|
||||
const sort = newSorting
|
||||
.map((s: ColumnSort) => `${s.id}:${s.desc ? 'desc' : 'asc'}`)
|
||||
.join(',')
|
||||
searchParams.set('sort', sort)
|
||||
}
|
||||
return searchParams
|
||||
})
|
||||
}
|
||||
const setSorting = useCallback(
|
||||
(updater: any) => {
|
||||
const newSorting =
|
||||
typeof updater === 'function' ? updater(sorting) : updater
|
||||
|
||||
setSearchParams((prev) => {
|
||||
if (newSorting.length) {
|
||||
const sort = newSorting
|
||||
.map((s: ColumnSort) => `${s.id}:${s.desc ? 'desc' : 'asc'}`)
|
||||
.join(',')
|
||||
prev.set('sort', sort)
|
||||
} else {
|
||||
prev.delete('sort')
|
||||
}
|
||||
return prev
|
||||
})
|
||||
},
|
||||
[sorting, setSearchParams]
|
||||
)
|
||||
useEffect(() => {
|
||||
setDataTable(data)
|
||||
}, [data])
|
||||
|
||||
const table = useReactTable({
|
||||
data: dataTable,
|
||||
columns,
|
||||
rowCount,
|
||||
state: {
|
||||
const tableConfig = useMemo(
|
||||
() => ({
|
||||
data: dataTable,
|
||||
columns,
|
||||
rowCount,
|
||||
state: {
|
||||
sorting,
|
||||
rowSelection,
|
||||
columnVisibility,
|
||||
pagination: {
|
||||
pageIndex,
|
||||
pageSize
|
||||
}
|
||||
},
|
||||
manualSorting: true,
|
||||
manualPagination: true,
|
||||
enableRowSelection: true,
|
||||
getRowId: (row: any) => row.id,
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
onRowSelectionChange: setRowSelection,
|
||||
onSortingChange: setSorting,
|
||||
onColumnVisibilityChange: setColumnVisibility,
|
||||
onPaginationChange: setPagination,
|
||||
meta: {
|
||||
removeRow: (rowId: string) => {
|
||||
setDataTable((rows) => rows.filter((row: any) => row?.id !== rowId))
|
||||
}
|
||||
}
|
||||
}),
|
||||
[
|
||||
dataTable,
|
||||
columns,
|
||||
rowCount,
|
||||
sorting,
|
||||
rowSelection,
|
||||
columnVisibility,
|
||||
pagination: {
|
||||
pageIndex,
|
||||
pageSize
|
||||
}
|
||||
},
|
||||
manualSorting: true,
|
||||
manualPagination: true,
|
||||
enableRowSelection: true,
|
||||
getRowId: (row: any) => row.id,
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
onRowSelectionChange: setRowSelection,
|
||||
onSortingChange: setSorting,
|
||||
onColumnVisibilityChange: setColumnVisibility,
|
||||
onPaginationChange: setPagination,
|
||||
meta: {
|
||||
removeRow: (rowId: string) => {
|
||||
setDataTable((rows) => rows.filter((row: any) => row?.id !== rowId))
|
||||
}
|
||||
}
|
||||
})
|
||||
setColumnVisibility,
|
||||
pageIndex,
|
||||
pageSize,
|
||||
setSorting,
|
||||
setPagination
|
||||
]
|
||||
)
|
||||
|
||||
const table = useReactTable(tableConfig)
|
||||
|
||||
useEffect(() => {
|
||||
if (!setSelectedRows) return
|
||||
|
||||
const selected = table.getSelectedRowModel().flatRows.map((r) => r.original)
|
||||
setSelectedRows?.(selected)
|
||||
}, [rowSelection])
|
||||
setSelectedRows(selected)
|
||||
}, [rowSelection, setSelectedRows, table])
|
||||
|
||||
return (
|
||||
<TableContext value={{ table }}>
|
||||
|
||||
Reference in New Issue
Block a user