Skip to content

Commit

Permalink
Merge pull request #714 from smartxworks/fix/table
Browse files Browse the repository at this point in the history
fix(Table): fix  where the state was not updated correctly in some cases
  • Loading branch information
tanbowensg authored May 19, 2023
2 parents 4fb5d3b + dd3d034 commit 2a0b4d9
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions packages/arco-lib/src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export const Table = implementRuntimeComponent({
} = pagination;

const rowSelectionType = rowSelectionTypeMap[cProps.rowSelectionType];
const currentChecked = useRef<Record<string, unknown>[]>([]);
const currentChecked = useRef<(string | number)[]>([]);
const currentClickedRow = useRef<(string | number)[] | undefined>(undefined);

const [currentPage, setCurrentPage] = useStateValue<number>(
Expand Down Expand Up @@ -274,18 +274,22 @@ export const Table = implementRuntimeComponent({

// reset selectedRows state when data changed
useEffect(() => {
const currentCheckedRowKeys = currentChecked.current.map(
row => row[rowKey] as string
// under server-side paging and with checkCrossPage enabled, the previous data should be cached without having to update it
if (useCustomPagination && checkCrossPage) {
return;
}
const selectedRows = currentPageData.filter(d =>
currentChecked.current.includes(d[rowKey])
);
// TODO: Save clickedRow state when rowkey changes, save the UI of clickedRow when turning the page
const clickedRow = currentPageData.find(d => d[rowKey] === currentClickedRow.current);
if (!clickedRow) currentClickedRow.current = undefined;
mergeState({
selectedRowKeys: currentCheckedRowKeys,
selectedRows: currentChecked.current,
selectedRowKeys: selectedRows.map(r => r[rowKey]),
selectedRows,
clickedRow,
});
}, [currentPageData, mergeState, rowKey]);
}, [checkCrossPage, currentPageData, mergeState, rowKey, useCustomPagination]);

// If there is less data to display than the current page, reset to the first page
useEffect(() => {
Expand Down Expand Up @@ -573,7 +577,7 @@ export const Table = implementRuntimeComponent({
// This option is required to achieve multi-selection across pages when customizing paging
preserveSelectedRowKeys: useCustomPagination ? checkCrossPage : undefined,
onChange(selectedRowKeys, selectedRows) {
currentChecked.current = selectedRows;
currentChecked.current = selectedRowKeys;
mergeState({
selectedRowKeys: selectedRowKeys as string[],
selectedRows,
Expand Down

0 comments on commit 2a0b4d9

Please sign in to comment.