Skip to content

Commit

Permalink
Added sort() callback as required param to createMultiSort
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn committed Jan 13, 2018
1 parent 6d8ebe3 commit 39255f1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
15 changes: 10 additions & 5 deletions docs/multiColumnSortTable.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ import {
Table,
} from 'react-virtualized';

const sortState = createMultiSort();
function sortCallback({
sortBy,
sortDirection,
}) {
// Sort your collection however you'd like.
// 'sortBy' is an ordered Array of fields.
// 'sortDirection' is a map of field name to "ASC" or "DESC" directions.
}

const sortState = createMultiSort(sortCallback);

// When rendering your header columns,
// Use the sort state exposed by sortState:
Expand All @@ -29,10 +38,6 @@ const headerRenderer = ({ dataKey, label }) => {
<Table {...tableProps} sort={sortState.sort}>
<Column {...columnProps} headerRenderer={headerRenderer} />
</Table>

// Sort your collection however you'd like.
// sortState.sortBy is an ordered Array of fields.
// sortState.sortDirection is a map of field name to "ASC" or "DESC" directions.
```

The `createMultiSort` utility also accepts default sort-by values:
Expand Down
20 changes: 12 additions & 8 deletions source/Table/createMultiSort.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ describe('createMultiSort', () => {
});
}

it('errors if the user did not specify a sort callback', () => {
expect(createMultiSort).toThrow();
});

it('sets the correct default values', () => {
const multiSort = createMultiSort({
const multiSort = createMultiSort(jest.fn(), {
defaultSortBy: ['a', 'b'],
defaultSortDirection: {
a: 'ASC',
Expand All @@ -31,7 +35,7 @@ describe('createMultiSort', () => {
});

it('sets the correct default sparse values', () => {
const multiSort = createMultiSort({
const multiSort = createMultiSort(jest.fn(), {
defaultSortBy: ['a', 'b'],
});
expect(multiSort.sortBy).toEqual(['a', 'b']);
Expand All @@ -41,7 +45,7 @@ describe('createMultiSort', () => {

describe('on click', () => {
it('sets the correct default value for a field', () => {
const multiSort = createMultiSort();
const multiSort = createMultiSort(jest.fn());

simulate(multiSort.sort, 'a');
expect(multiSort.sortBy).toEqual(['a']);
Expand All @@ -53,7 +57,7 @@ describe('createMultiSort', () => {
});

it('toggles a field value', () => {
const multiSort = createMultiSort();
const multiSort = createMultiSort(jest.fn());

simulate(multiSort.sort, 'a');
expect(multiSort.sortBy).toEqual(['a']);
Expand All @@ -75,7 +79,7 @@ describe('createMultiSort', () => {

describe('on shift click', () => {
it('appends a field to the sort by list', () => {
const multiSort = createMultiSort();
const multiSort = createMultiSort(jest.fn());

simulate(multiSort.sort, 'a');
expect(multiSort.sortBy).toEqual(['a']);
Expand All @@ -88,7 +92,7 @@ describe('createMultiSort', () => {
});

it('toggles an appended field value', () => {
const multiSort = createMultiSort();
const multiSort = createMultiSort(jest.fn());

simulate(multiSort.sort, 'a');
expect(multiSort.sortBy).toEqual(['a']);
Expand All @@ -108,7 +112,7 @@ describe('createMultiSort', () => {

describe('control click', () => {
it('removes a field from the sort by list', () => {
const multiSort = createMultiSort({
const multiSort = createMultiSort(jest.fn(), {
defaultSortBy: ['a', 'b'],
});
expect(multiSort.sortBy).toEqual(['a', 'b']);
Expand All @@ -121,7 +125,7 @@ describe('createMultiSort', () => {
});

it('ignores fields not in the list on control click', () => {
const multiSort = createMultiSort({
const multiSort = createMultiSort(jest.fn(), {
defaultSortBy: ['a', 'b'],
});
expect(multiSort.sortBy).toEqual(['a', 'b']);
Expand Down
16 changes: 13 additions & 3 deletions source/Table/createMultiSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type SortParams = {

type SortDirectionMap = {[string]: SortDirection};

type MultiSortParams = {
type MultiSortOptions = {
defaultSortBy: ?Array<string>,
defaultSortDirection: ?SortDirectionMap,
};
Expand All @@ -34,10 +34,14 @@ type MultiSortReturn = {
sortDirection: SortDirectionMap,
};

export default function createMultiSort({
export default function createMultiSort(sortCallback: Function, {
defaultSortBy,
defaultSortDirection = {},
}: MultiSortParams = {}): MultiSortReturn {
}: MultiSortOptions = {}): MultiSortReturn {
if (!sortCallback) {
throw Error(`Required parameter "sortCallback" not specified`);
}

const sortBy = defaultSortBy || [];
const sortDirection = {};

Expand Down Expand Up @@ -78,6 +82,12 @@ export default function createMultiSort({
sortBy.push(dataKey);
}
}

// Notify application code
sortCallback({
sortBy,
sortDirection,
});
}

return {
Expand Down

0 comments on commit 39255f1

Please sign in to comment.