Skip to content

Commit

Permalink
fix: fix column sorting for keys that use dot notation
Browse files Browse the repository at this point in the history
This commit f599fd2 made a change to
column sorting to make sorting case insenstive. However, this change
introduced a bug where column keys that used dot notation didn't work
correctly for sorting anymore.
  • Loading branch information
jeremykirc committed Oct 24, 2023
1 parent 2cbd82f commit 5a0536b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 31 deletions.
6 changes: 5 additions & 1 deletion src/components/Table/UncontrolledTable.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import isEqual from 'lodash.isequal';
import orderBy from 'lodash.orderby';
import get from 'lodash.get';

Check failure on line 3 in src/components/Table/UncontrolledTable.js

View workflow job for this annotation

GitHub Actions / test

'lodash.get' should be listed in the project's dependencies. Run 'npm i -S lodash.get' to add it

Check warning on line 3 in src/components/Table/UncontrolledTable.js

View workflow job for this annotation

GitHub Actions / test

`lodash.get` import should occur before import of `lodash.isequal`
import some from 'lodash.some';
import PropTypes from 'prop-types';
import React from 'react';
Expand Down Expand Up @@ -53,7 +54,10 @@ export default class UncontrolledTable extends React.Component {
sortedData = (rows, column, ascending) =>
orderBy(
rows,
(row) => (typeof row[column] === 'string' ? row[column].toLowerCase() : row[column]),
(row) => {
const value = get(row, column);
return typeof value === 'string' ? value.toLowerCase() : value;
},
[ascending ? 'asc' : 'desc']
);

Expand Down
71 changes: 41 additions & 30 deletions src/components/Table/UncontrolledTable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,14 @@ describe('<UncontrolledTable />', () => {
const columns = [
{ header: 'Name', key: 'name', cell: (row) => row },
{ header: 'Age', key: 'age', cell: (row) => row },
{ header: 'Pet Name', key: 'pet.name', call: (row) => row },
];
const rows = [
{ name: 'Alpha', age: 35 },
{ name: 'Bravo', age: 30 },
{ name: 'Charlie', age: 25 },
{ name: 'amanda', age: 50 },
{ name: '2 Chainz', age: 40 },
{ name: 'Alpha', age: 35, pet: { name: 'Kitty' } },
{ name: 'Bravo', age: 30, pet: { name: 'Fluffy' } },
{ name: 'Charlie', age: 25, pet: { name: 'Doggy' } },
{ name: 'amanda', age: 50, pet: { name: 'fido' } },
{ name: '2 Chainz', age: 40, pet: { name: 'Garfield' } },
];
const wrapper = shallow(
<UncontrolledTable
Expand All @@ -386,51 +387,61 @@ describe('<UncontrolledTable />', () => {
);

assert.deepStrictEqual(wrapper.find(SortableTable).prop('rows'), [
{ name: 'Charlie', age: 25 },
{ name: 'Bravo', age: 30 },
{ name: 'amanda', age: 50 },
{ name: 'Alpha', age: 35 },
{ name: '2 Chainz', age: 40 },
{ name: 'Charlie', age: 25, pet: { name: 'Doggy' } },
{ name: 'Bravo', age: 30, pet: { name: 'Fluffy' } },
{ name: 'amanda', age: 50, pet: { name: 'fido' } },
{ name: 'Alpha', age: 35, pet: { name: 'Kitty' } },
{ name: '2 Chainz', age: 40, pet: { name: 'Garfield' } },
]);

wrapper.find(SortableTable).prop('columns')[0].onSort(true); // Simulate sort by ascending order by name (string)
wrapper.update();
assert.deepStrictEqual(wrapper.find(SortableTable).prop('rows'), [
{ name: '2 Chainz', age: 40 },
{ name: 'Alpha', age: 35 },
{ name: 'amanda', age: 50 },
{ name: 'Bravo', age: 30 },
{ name: 'Charlie', age: 25 },
{ name: '2 Chainz', age: 40, pet: { name: 'Garfield' } },
{ name: 'Alpha', age: 35, pet: { name: 'Kitty' } },
{ name: 'amanda', age: 50, pet: { name: 'fido' } },
{ name: 'Bravo', age: 30, pet: { name: 'Fluffy' } },
{ name: 'Charlie', age: 25, pet: { name: 'Doggy' } },
]);

wrapper.find(SortableTable).prop('columns')[0].onSort(false); // Simulate sort by descending order by name (string)
wrapper.update();
assert.deepStrictEqual(wrapper.find(SortableTable).prop('rows'), [
{ name: 'Charlie', age: 25 },
{ name: 'Bravo', age: 30 },
{ name: 'amanda', age: 50 },
{ name: 'Alpha', age: 35 },
{ name: '2 Chainz', age: 40 },
{ name: 'Charlie', age: 25, pet: { name: 'Doggy' } },
{ name: 'Bravo', age: 30, pet: { name: 'Fluffy' } },
{ name: 'amanda', age: 50, pet: { name: 'fido' } },
{ name: 'Alpha', age: 35, pet: { name: 'Kitty' } },
{ name: '2 Chainz', age: 40, pet: { name: 'Garfield' } },
]);

wrapper.find(SortableTable).prop('columns')[1].onSort(true); // Simulate sort by ascending order by age (integer)
wrapper.update();
assert.deepStrictEqual(wrapper.find(SortableTable).prop('rows'), [
{ name: 'Charlie', age: 25 },
{ name: 'Bravo', age: 30 },
{ name: 'Alpha', age: 35 },
{ name: '2 Chainz', age: 40 },
{ name: 'amanda', age: 50 },
{ name: 'Charlie', age: 25, pet: { name: 'Doggy' } },
{ name: 'Bravo', age: 30, pet: { name: 'Fluffy' } },
{ name: 'Alpha', age: 35, pet: { name: 'Kitty' } },
{ name: '2 Chainz', age: 40, pet: { name: 'Garfield' } },
{ name: 'amanda', age: 50, pet: { name: 'fido' } },
]);

wrapper.find(SortableTable).prop('columns')[1].onSort(false); // Simulate sort by descending order by age (integer)
wrapper.update();
assert.deepStrictEqual(wrapper.find(SortableTable).prop('rows'), [
{ name: 'amanda', age: 50 },
{ name: '2 Chainz', age: 40 },
{ name: 'Alpha', age: 35 },
{ name: 'Bravo', age: 30 },
{ name: 'Charlie', age: 25 },
{ name: 'amanda', age: 50, pet: { name: 'fido' } },
{ name: '2 Chainz', age: 40, pet: { name: 'Garfield' } },
{ name: 'Alpha', age: 35, pet: { name: 'Kitty' } },
{ name: 'Bravo', age: 30, pet: { name: 'Fluffy' } },
{ name: 'Charlie', age: 25, pet: { name: 'Doggy' } },
]);

wrapper.find(SortableTable).prop('columns')[2].onSort(true); // Simulate sort by ascending order by pet.name (dot notation)
wrapper.update();
assert.deepStrictEqual(wrapper.find(SortableTable).prop('rows'), [
{ name: 'Charlie', age: 25, pet: { name: 'Doggy' } },
{ name: 'amanda', age: 50, pet: { name: 'fido' } },
{ name: 'Bravo', age: 30, pet: { name: 'Fluffy' } },
{ name: '2 Chainz', age: 40, pet: { name: 'Garfield' } },
{ name: 'Alpha', age: 35, pet: { name: 'Kitty' } },
]);
});

Expand Down

0 comments on commit 5a0536b

Please sign in to comment.