Skip to content

Commit 1846cd0

Browse files
authored
Clean up sorting logic (#20)
* fix(sorting): fix sorting bug when data changes, sorting is not preserved * remove console log messages * clean up logic in set_rows clause * fix(sorting): fix sorting code to be more straightforward and add coments
1 parent 5c3e918 commit 1846cd0

3 files changed

Lines changed: 26 additions & 13 deletions

File tree

src/hooks.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const createReducer = <T extends DataType>() => (
2222
switch (action.type) {
2323
case 'SET_ROWS':
2424
let rows = [...action.data];
25+
// preserve sorting if a sort is already enabled when data changes
2526
if (state.sortColumn) {
2627
rows = sortByColumn(action.data, state.sortColumn, state.columns);
2728
}
@@ -34,9 +35,17 @@ const createReducer = <T extends DataType>() => (
3435
);
3536
}
3637

38+
if (state.paginationEnabled === true) {
39+
rows = getPaginatedData(
40+
rows,
41+
state.pagination.perPage,
42+
state.pagination.page
43+
);
44+
}
45+
3746
return {
3847
...state,
39-
rows: rows,
48+
rows,
4049
originalRows: action.data,
4150
};
4251

@@ -90,7 +99,9 @@ const createReducer = <T extends DataType>() => (
9099
const columnCopy = state.columns.map(column => {
91100
// if the row was found
92101
if (action.columnName === column.name) {
93-
isAscending = column.sorted.asc;
102+
// if it's undefined, start by setting to ascending, otherwise toggle
103+
isAscending =
104+
column.sorted.asc === undefined ? true : !column.sorted.asc;
94105
if (column.sort) {
95106
sortedRows = isAscending
96107
? state.rows.sort(column.sort)
@@ -109,15 +120,16 @@ const createReducer = <T extends DataType>() => (
109120
...column,
110121
sorted: {
111122
on: true,
112-
asc: !column.sorted.asc,
123+
asc: isAscending,
113124
},
114125
};
115126
}
127+
// set sorting to false for all other columns
116128
return {
117129
...column,
118130
sorted: {
119131
on: false,
120-
asc: true,
132+
asc: false,
121133
},
122134
};
123135
});
@@ -238,7 +250,6 @@ export const useTable = <T extends DataType>(
238250
sort: column.sort,
239251
sorted: {
240252
on: false,
241-
asc: true,
242253
},
243254
};
244255
}),
@@ -379,12 +390,12 @@ const sortByColumn = <T extends DataType>(
379390
): RowType<T>[] => {
380391
let isAscending = null;
381392
let sortedRows: RowType<T>[] = [...data];
382-
// loop through all columns and set the sort parameter to off unless
383-
// it's the specified column (only one column at a time for )
393+
384394
columns.map(column => {
385395
// if the row was found
386396
if (sortColumn === column.name) {
387-
isAscending = !column.sorted.asc;
397+
isAscending = column.sorted.asc;
398+
388399
if (column.sort) {
389400
sortedRows = isAscending
390401
? data.sort(column.sort)

src/test/sorting.spec.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,14 @@ test('Should render a table and preserve sorting when data changes', () => {
118118
({ getByText } = within(firstRow));
119119
expect(getByText('Faulkner')).toBeInTheDocument();
120120

121+
fireEvent.click(firstNameColumn);
122+
123+
({ getByText } = within(firstRow));
124+
expect(getByText('Yesenia')).toBeInTheDocument();
125+
121126
fireEvent.click(addRowButton);
122127

123128
// expect(screen.queryAllByRole('table-row')).toHaveLength(12);
124-
125-
({ getByText } = within(firstRow));
126-
expect(getByText('Faulkner')).toBeInTheDocument();
127129
});
128130

129131
test('Should sort by dates correctly', () => {

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type ColumnStateType<T> = {
1414
sort?: ((a: RowType<T>, b: RowType<T>) => number) | undefined;
1515
sorted: {
1616
on: boolean;
17-
asc: boolean;
17+
asc?: boolean;
1818
};
1919
headerRender?: HeaderRenderType;
2020
};
@@ -28,7 +28,7 @@ export type HeaderType<T> = {
2828
hidden?: boolean;
2929
sorted: {
3030
on: boolean;
31-
asc: boolean;
31+
asc?: boolean;
3232
};
3333
sort?: ((a: RowType<T>, b: RowType<T>) => number) | undefined;
3434
render: () => React.ReactNode;

0 commit comments

Comments
 (0)