diff --git a/README.md b/README.md index 01c1272..32c3912 100644 --- a/README.md +++ b/README.md @@ -587,6 +587,11 @@ The following options are supported via the main options object passed to `useTa - Disables sorting for every column in the entire table. - `disableMultiSort: Bool` - Disables multi-sorting for the entire table. +- `isMultiSortEvent: Function` + - Allows to override default multisort behaviour(i.e. multisort applies when shift key is presssed), if this function is provided then returned boolean value from this function will make decision whether newly applied sort action will be considered as multisort or not. + - Receives `event` as argument. +- `maxMultiSortColCount: Number` + - Limit on max number of columns for multisort, e.g. if set to 3, and suppose table is sorted by `[A, B, C]` and then clicking `D` for sorting should result in table sorted by `[B, C , D]` - `disableSortRemove: Bool` - If true, the un-sorted state will not be available to columns once they have been sorted. - `disableMultiRemove: Bool` diff --git a/src/plugin-hooks/useSortBy.js b/src/plugin-hooks/useSortBy.js index 4323ae3..1c0daa8 100755 --- a/src/plugin-hooks/useSortBy.js +++ b/src/plugin-hooks/useSortBy.js @@ -31,6 +31,8 @@ const propTypes = { manualSorting: PropTypes.bool, disableSorting: PropTypes.bool, disableMultiSort: PropTypes.bool, + isMultiSortEvent: PropTypes.func, + maxMultiSortColCount: PropTypes.number, disableSortRemove: PropTypes.bool, disableMultiRemove: PropTypes.bool, } @@ -55,6 +57,8 @@ function useMain(instance) { disableSortRemove, disableMultiRemove, disableMultiSort, + isMultiSortEvent = (e) => e.shiftKey, + maxMultiSortColCount = Number.MAX_SAFE_INTEGER, hooks, state: [{ sortBy }, setState], plugins, @@ -128,6 +132,8 @@ function useMain(instance) { desc: hasDescDefined ? desc : sortDescFirst, }, ] + // Take latest n columns + newSortBy.splice(0, newSortBy.length - maxMultiSortColCount); } else if (action === 'toggle') { // This flips (or sets) the newSortBy = sortBy.map(d => { @@ -177,7 +183,7 @@ function useMain(instance) { e.persist() column.toggleSortBy( undefined, - !instance.disableMultiSort && e.shiftKey + !instance.disableMultiSort && isMultiSortEvent(e) ) } : undefined,