diff --git a/src/components/data-table/DataTable.tsx b/src/components/data-table/DataTable.tsx index 12190dd..bbf206a 100644 --- a/src/components/data-table/DataTable.tsx +++ b/src/components/data-table/DataTable.tsx @@ -25,6 +25,7 @@ export function DataTable({ totalCount: serverTotalCount, pagination: externalPagination, onPaginationChange: onServerPaginationChange, + sorting: externalSorting, onSortingChange: onServerSortingChange, selectMode = 'none', onRowClick, @@ -41,7 +42,7 @@ export function DataTable({ bottomPinnedRows = EMPTY_PINNED_ROWS, }: Readonly>) { const [internalPagination, setInternalPagination] = useState({ pageIndex: 0, pageSize: defaultPageSize }) - const [sorting, setSorting] = useState([]) + const [internalSorting, setInternalSorting] = useState([]) const [rowSelection, setRowSelection] = useState({}) const scrollContainerRef = useRef(null) const drag = usePreventClickOnDrag() @@ -78,6 +79,7 @@ export function DataTable({ }, [columns, selectMode, selectionEnabled]) const pagination = isClient ? internalPagination : (externalPagination ?? internalPagination) + const sorting = !isClient && externalSorting ? externalSorting : internalSorting useEffect(() => { setRowSelection({}) @@ -100,6 +102,20 @@ export function DataTable({ [isClient, onServerPaginationChange], ) + const handleSortingChange = useCallback>( + (updater) => { + const nextSorting = typeof updater === 'function' ? updater(sorting) : updater + + if (!isClient && onServerSortingChange) { + onServerSortingChange(nextSorting) + return + } + + setInternalSorting(nextSorting) + }, + [isClient, onServerSortingChange, sorting], + ) + const combinedData = useMemo( () => [...topPinnedRows, ...data, ...bottomPinnedRows], [topPinnedRows, data, bottomPinnedRows], @@ -128,7 +144,7 @@ export function DataTable({ enableRowPinning: true, keepPinnedRows: true, onRowSelectionChange: setRowSelection, - onSortingChange: setSorting, + onSortingChange: handleSortingChange, onPaginationChange: handlePaginationChange, getCoreRowModel: getCoreRowModel(), getSortedRowModel: isClient ? getSortedRowModel() : undefined, @@ -139,10 +155,6 @@ export function DataTable({ getRowId: internalGetRowId, }) - useEffect(() => { - if (!isClient) onServerSortingChange?.(sorting) - }, [isClient, onServerSortingChange, sorting]) - useEffect(() => { if (!selectionEnabled) return onRowSelectionChange?.(table.getSelectedRowModel().rows.map((row) => row.original)) diff --git a/src/components/data-table/DataTable.type.ts b/src/components/data-table/DataTable.type.ts index c43caba..7f618b1 100644 --- a/src/components/data-table/DataTable.type.ts +++ b/src/components/data-table/DataTable.type.ts @@ -33,6 +33,7 @@ export interface DataTableProps { totalCount?: number pagination?: PaginationState onPaginationChange?: (updater: Updater) => void + sorting?: SortingState onSortingChange?: (sorting: SortingState) => void selectMode?: SelectMode onRowClick?: (row: TData) => void diff --git a/src/components/list/ListSearchTable.tsx b/src/components/list/ListSearchTable.tsx index 57b5afe..5eaaa55 100644 --- a/src/components/list/ListSearchTable.tsx +++ b/src/components/list/ListSearchTable.tsx @@ -667,6 +667,8 @@ export function ListSearchTable({ }, [pagination, onPaginationChange]) useEffect(() => { + if (listHeaders.length === 0) return + setFilterRows((prev) => { const next = prev.filter((row) => { const header = accessibleFilterHeaderById.get(row.headerId) @@ -674,7 +676,7 @@ export function ListSearchTable({ }) return next.length === prev.length ? prev : next }) - }, [accessibleFilterHeaderById, lockedListFilterHeaderTypes]) + }, [accessibleFilterHeaderById, listHeaders.length, lockedListFilterHeaderTypes]) useEffect(() => { if (!refreshKey || handledRefreshKeyRef.current === refreshKey) return @@ -805,6 +807,7 @@ export function ListSearchTable({ totalCount={totalCount} pagination={pagination} onPaginationChange={setPagination} + sorting={sorting} onSortingChange={handleSortingChange} selectMode={selectMode} onRowSelectionChange={handleRowSelectionChange}