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