Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix create multi sort bug #1051

Merged
merged 4 commits into from
Jul 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions source/Table/createMultiSort.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,29 @@ describe('createMultiSort', () => {
simulate(multiSort.sort, 'a');
expect(multiSort.sortBy).toEqual(['a']);
});

it('resets sort-direction fields', () => {
const multiSort = createMultiSort(jest.fn(), {
defaultSortBy: ['a', 'b'],
defaultSortDirection: {
a: 'DESC',
b: 'ASC',
},
});
expect(multiSort.sortBy).toEqual(['a', 'b']);
expect(multiSort.sortDirection.a).toEqual('DESC');
expect(multiSort.sortDirection.b).toEqual('ASC');

simulate(multiSort.sort, 'a');
expect(multiSort.sortBy).toEqual(['a']);
expect(multiSort.sortDirection.a).toEqual('ASC');
expect(multiSort.sortDirection.b).toEqual(undefined);

simulate(multiSort.sort, 'b');
expect(multiSort.sortBy).toEqual(['b']);
expect(multiSort.sortDirection.a).toEqual(undefined);
expect(multiSort.sortDirection.b).toEqual('ASC');
});
});

describe('on shift click', () => {
Expand Down Expand Up @@ -124,6 +147,28 @@ describe('createMultiSort', () => {
expect(multiSort.sortDirection.a).toBe('ASC');
expect(multiSort.sortDirection.b).toBe('ASC');
});

it('able to shift+click more than once', () => {
const multiSort = createMultiSort(jest.fn());

simulate(multiSort.sort, 'a');
expect(multiSort.sortBy).toEqual(['a']);
expect(multiSort.sortDirection.a).toBe('ASC');

simulate(multiSort.sort, 'b', 'shift');
expect(multiSort.sortBy).toEqual(['a', 'b']);
expect(multiSort.sortDirection.a).toBe('ASC');
expect(multiSort.sortDirection.b).toBe('ASC');

simulate(multiSort.sort, 'b');
expect(multiSort.sortBy).toEqual(['b']);
expect(multiSort.sortDirection.b).toBe('DESC');

simulate(multiSort.sort, 'a', 'shift');
expect(multiSort.sortBy).toEqual(['b', 'a']);
expect(multiSort.sortDirection.a).toBe('ASC');
expect(multiSort.sortDirection.b).toBe('DESC');
});
});

['control', 'meta'].forEach(modifier => {
Expand Down
9 changes: 9 additions & 0 deletions source/Table/createMultiSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,18 @@ export default function createMultiSort(
delete sortDirection[dataKey];
}
} else {
// Clear sortBy array of all non-selected keys
sortBy.length = 0;
sortBy.push(dataKey);

// Clear sortDirection object of all non-selected keys
const sortDirectionKeys = Object.keys(sortDirection);
sortDirectionKeys.forEach(key => {
if (key !== dataKey) delete sortDirection[key];
});

// If key is already selected, reverse sort direction.
// Else, set sort direction to default direction.
if (sortDirection.hasOwnProperty(dataKey)) {
sortDirection[dataKey] =
sortDirection[dataKey] === 'ASC' ? 'DESC' : 'ASC';
Expand Down