Skip to content

Commit 1f7cd4d

Browse files
authored
add sorted columns to onSort (#1047)
1 parent 14e2e7f commit 1f7cd4d

File tree

5 files changed

+416
-556
lines changed

5 files changed

+416
-556
lines changed

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-data-table-component",
3-
"version": "7.5.0",
3+
"version": "7.5.1",
44
"description": "A simple to use declarative react based data table",
55
"main": "dist/index.cjs.js",
66
"module": "dist/index.es.js",
@@ -46,12 +46,12 @@
4646
"@material-ui/icons": "^4.11.2",
4747
"@rollup/plugin-commonjs": "^21.0.1",
4848
"@rollup/plugin-node-resolve": "^13.1.1",
49-
"@storybook/addon-a11y": "^6.4.9",
50-
"@storybook/addon-essentials": "^6.4.9",
51-
"@storybook/addon-storysource": "^6.4.9",
52-
"@storybook/addons": "^6.4.9",
53-
"@storybook/react": "^6.4.9",
54-
"@storybook/theming": "^6.4.9",
49+
"@storybook/addon-a11y": "^6.4.22",
50+
"@storybook/addon-essentials": "^6.4.22",
51+
"@storybook/addon-storysource": "^6.4.22",
52+
"@storybook/addons": "^6.4.22",
53+
"@storybook/react": "^6.4.22",
54+
"@storybook/theming": "^6.4.22",
5555
"@testing-library/react": "^12.1.2",
5656
"@types/faker": "^5.5.9",
5757
"@types/jest": "^27.0.3",
@@ -74,7 +74,7 @@
7474
"eslint-plugin-prettier": "^4.0.0",
7575
"eslint-plugin-react": "^7.27.1",
7676
"eslint-plugin-react-hooks": "^4.3.0",
77-
"eslint-plugin-storybook": "^0.5.3",
77+
"eslint-plugin-storybook": "^0.5.11",
7878
"faker": "^5.5.3",
7979
"gh-pages": "^3.2.3",
8080
"jest": "^27.4.5",
@@ -84,9 +84,9 @@
8484
"memoize-one": "^6.0.0",
8585
"moment": "^2.29.1",
8686
"prettier": "^2.5.1",
87-
"react": "^17.0.2",
87+
"react": "^18.1.0",
8888
"react-app-polyfill": "^3.0.0",
89-
"react-dom": "^17.0.2",
89+
"react-dom": "^18.1.0",
9090
"rimraf": "^3.0.2",
9191
"rollup": "^2.61.1",
9292
"rollup-plugin-terser": "^7.0.2",
@@ -98,7 +98,7 @@
9898
"stylelint-config-styled-components": "^0.1.1",
9999
"stylelint-processor-styled-components": "^1.10.0",
100100
"ts-jest": "^27.1.2",
101-
"typescript": "^4.5.4"
101+
"typescript": "^4.6.4"
102102
},
103103
"dependencies": {
104104
"deepmerge": "^4.2.2"

src/DataTable/DataTable.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ function DataTable<T>(props: TableProps<T>): JSX.Element {
281281
}, [toggleOnSelectedRowsChange]);
282282

283283
useDidUpdateEffect(() => {
284-
onSort(selectedColumn, sortDirection);
285-
}, [selectedColumn, sortDirection]);
284+
onSort(selectedColumn, sortDirection, sortedData);
285+
}, [selectedColumn, sortDirection, sortedData]);
286286

287287
useDidUpdateEffect(() => {
288288
onChangePage(currentPage, paginationTotalRows || sortedData.length);

src/DataTable/__tests__/DataTable.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ describe('DataTable::sorting', () => {
675675

676676
fireEvent.click(container.querySelector('div[data-sort-id="1"]') as HTMLElement);
677677

678-
expect(onSortMock).toBeCalledWith({ id: 1, ...mock.columns[0] }, SortOrder.ASC);
678+
expect(onSortMock).toBeCalledWith({ id: 1, ...mock.columns[0] }, SortOrder.ASC, mock.data.slice(0).sort());
679679
});
680680

681681
test('should call onSort with the correct params if the sort is clicked twice', () => {
@@ -684,10 +684,10 @@ describe('DataTable::sorting', () => {
684684
const { container } = render(<DataTable data={mock.data} columns={mock.columns} onSort={onSortMock} />);
685685

686686
fireEvent.click(container.querySelector('div[data-sort-id="1"]') as HTMLElement);
687-
expect(onSortMock).toBeCalledWith({ id: 1, ...mock.columns[0] }, SortOrder.ASC);
687+
expect(onSortMock).toBeCalledWith({ id: 1, ...mock.columns[0] }, SortOrder.ASC, mock.data.slice(0).sort());
688688

689689
fireEvent.click(container.querySelector('div[data-sort-id="1"]') as HTMLElement);
690-
expect(onSortMock).toBeCalledWith({ id: 1, ...mock.columns[0] }, SortOrder.DESC);
690+
expect(onSortMock).toBeCalledWith({ id: 1, ...mock.columns[0] }, SortOrder.DESC, mock.data.slice(0).reverse());
691691
});
692692

693693
test('should render correctly with a custom sortIcon', () => {

src/DataTable/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export type TableProps<T> = {
7070
onRowMouseLeave?: (row: T, e: React.MouseEvent) => void;
7171
onRowExpandToggled?: ExpandRowToggled<T>;
7272
onSelectedRowsChange?: (selected: { allSelected: boolean; selectedCount: number; selectedRows: T[] }) => void;
73-
onSort?: (selectedColumn: TableColumn<T>, sortDirection: SortOrder) => void;
73+
onSort?: (selectedColumn: TableColumn<T>, sortDirection: SortOrder, sortedRows: T[]) => void;
7474
onColumnOrderChange?: (nextOrder: TableColumn<T>[]) => void;
7575
pagination?: boolean;
7676
paginationComponent?: PaginationComponent;

0 commit comments

Comments
 (0)