| 44 | direction === "ASC" ? "DESC" : "ASC"; |
| 45 | |
| 46 | function sort({ |
| 47 | defaultSortDirection, |
| 48 | event, |
| 49 | sortBy: dataKey |
| 50 | }: MultiSortParams): void { |
| 51 | if (event.shiftKey) { |
| 52 | // Shift + click appends a column to existing criteria (or toggles its direction) |
| 53 | if (sortDirection[dataKey] !== undefined) { |
| 54 | sortDirection = { |
| 55 | ...sortDirection, |
| 56 | [dataKey]: toggle(sortDirection[dataKey]) |
| 57 | }; |
| 58 | } else { |
| 59 | sortBy = [...sortBy, dataKey]; |
| 60 | sortDirection = { ...sortDirection, [dataKey]: defaultSortDirection }; |
| 61 | } |
| 62 | } else if (event.ctrlKey || event.metaKey) { |
| 63 | // Control + click removes column from sort (if present) |
| 64 | if (sortBy.includes(dataKey)) { |
| 65 | sortBy = sortBy.filter((key) => key !== dataKey); |
| 66 | sortDirection = { ...sortDirection }; |
| 67 | delete sortDirection[dataKey]; |
| 68 | } |
| 69 | } else { |
| 70 | // Sort by the selected key only. |
| 71 | // If it is already selected, reverse its sort direction; else, use the default direction. |
| 72 | sortDirection = { |
| 73 | [dataKey]: |
| 74 | sortDirection[dataKey] !== undefined |
| 75 | ? toggle(sortDirection[dataKey]) |
| 76 | : defaultSortDirection |
| 77 | }; |
| 78 | sortBy = [dataKey]; |
| 79 | } |
| 80 | |
| 81 | // Notify application code |
| 82 | sortCallback({ |
| 83 | sortBy, |
| 84 | sortDirection |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | return { |
| 89 | sort, |