MCPcopy Index your code
hub / github.com/bvaughn/react-virtualized / createMultiSort

Function createMultiSort

source/Table/createMultiSort.js:37–109  ·  view source on GitHub ↗
(
  sortCallback: Function,
  {defaultSortBy, defaultSortDirection = {}}: MultiSortOptions = {},
)

Source from the content-addressed store, hash-verified

35};
36
37export default function createMultiSort(
38 sortCallback: Function,
39 {defaultSortBy, defaultSortDirection = {}}: MultiSortOptions = {},
40): MultiSortReturn {
41 if (!sortCallback) {
42 throw Error(`Required parameter "sortCallback" not specified`);
43 }
44
45 const sortBy = defaultSortBy || [];
46 const sortDirection = {};
47
48 sortBy.forEach(dataKey => {
49 sortDirection[dataKey] =
50 defaultSortDirection[dataKey] !== undefined
51 ? defaultSortDirection[dataKey]
52 : 'ASC';
53 });
54
55 function sort({
56 defaultSortDirection,
57 event,
58 sortBy: dataKey,
59 }: SortParams): void {
60 if (event.shiftKey) {
61 // Shift + click appends a column to existing criteria
62 if (sortDirection[dataKey] !== undefined) {
63 sortDirection[dataKey] =
64 sortDirection[dataKey] === 'ASC' ? 'DESC' : 'ASC';
65 } else {
66 sortDirection[dataKey] = defaultSortDirection;
67 sortBy.push(dataKey);
68 }
69 } else if (event.ctrlKey || event.metaKey) {
70 // Control + click removes column from sort (if pressent)
71 const index = sortBy.indexOf(dataKey);
72 if (index >= 0) {
73 sortBy.splice(index, 1);
74 delete sortDirection[dataKey];
75 }
76 } else {
77 // Clear sortBy array of all non-selected keys
78 sortBy.length = 0;
79 sortBy.push(dataKey);
80
81 // Clear sortDirection object of all non-selected keys
82 const sortDirectionKeys = Object.keys(sortDirection);
83 sortDirectionKeys.forEach(key => {
84 if (key !== dataKey) delete sortDirection[key];
85 });
86
87 // If key is already selected, reverse sort direction.
88 // Else, set sort direction to default direction.
89 if (sortDirection[dataKey] !== undefined) {
90 sortDirection[dataKey] =
91 sortDirection[dataKey] === 'ASC' ? 'DESC' : 'ASC';
92 } else {
93 sortDirection[dataKey] = defaultSortDirection;
94 }

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…