From 824ee34ba96b85125c9dfb05c36762424e1e35ef Mon Sep 17 00:00:00 2001 From: tannerlinsley Date: Tue, 23 Jul 2019 12:20:32 -0600 Subject: [PATCH] Refactor sortBy logic --- README.md | 2 ++ src/plugin-hooks/useSortBy.js | 65 +++++++++++++++++------------------ src/sortTypes.js | 2 +- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index ccd4a30..0eb3ee1 100644 --- a/README.md +++ b/README.md @@ -524,6 +524,8 @@ The following options are supported via the main options object passed to `useTa - If true, the first default direction for sorting will be descending. This may also be overridden at the column level. - `disableSortRemove: Bool` - If true, the un-sorted state will not be available to columns once they have been sorted. +- `disableMultiRemove: Bool` + - If true, the un-sorted state will not be available to multi-sorted columns. - `orderByFn: Function` - Must be **memoizd** - Defaults to the built-in [default orderBy function](TODO) diff --git a/src/plugin-hooks/useSortBy.js b/src/plugin-hooks/useSortBy.js index addf4e0..8f0b10c 100755 --- a/src/plugin-hooks/useSortBy.js +++ b/src/plugin-hooks/useSortBy.js @@ -34,6 +34,7 @@ const propTypes = { defaultSortDesc: PropTypes.bool, disableMultiSort: PropTypes.bool, disableSortRemove: PropTypes.bool, + disableMultiRemove: PropTypes.bool, } export const useSortBy = props => { @@ -50,6 +51,7 @@ export const useSortBy = props => { disableSorting, defaultSortDesc, disableSortRemove, + disableMultiRemove, disableMultiSort, hooks, state: [{ sortBy }, setState], @@ -80,36 +82,42 @@ export const useSortBy = props => { // Find any existing sortBy for this column const existingSortBy = sortBy.find(d => d.id === columnID) + const existingIndex = sortBy.findIndex(d => d.id == columnID) const hasDescDefined = typeof desc !== 'undefined' && desc !== null let newSortBy = [] - // What should we do with this filter? + // What should we do with this sort action? let action - if (disableMultiSort || !multi) { - if (sortBy.length <= 1 && existingSortBy) { - if ( - (existingSortBy.desc && !resolvedDefaultSortDesc) || - (!existingSortBy.desc && resolvedDefaultSortDesc) - ) { - action = disableSortRemove ? 'toggle' : 'remove' - } else { - action = 'toggle' - } + if (!disableMultiSort && multi) { + if (existingSortBy) { + action = 'toggle' + } else { + action = 'add' + } + } else { + // Normal mode + if (existingIndex !== sortBy.length - 1) { + action = 'replace' + } else if (existingSortBy) { + action = 'toggle' } else { action = 'replace' } - } else { - if (!existingSortBy) { - action = 'add' - } else { - if (hasDescDefined) { - action = 'set' - } else { - action = 'toggle' - } - } + } + + // Handle toggle states that will remove the sortBy + if ( + action === 'toggle' && // Must be toggling + !disableSortRemove && // If disableSortRemove, disable in general + !hasDescDefined && // Must not be setting desc + (multi ? !disableMultiRemove : true) && // If multi, don't allow if disableMultiRemove + ((existingSortBy && // Finally, detect if it should indeed be removed + (existingSortBy.desc && !resolvedDefaultSortDesc)) || + (!existingSortBy.desc && resolvedDefaultSortDesc)) + ) { + action = 'remove' } if (action === 'replace') { @@ -127,28 +135,19 @@ export const useSortBy = props => { desc: hasDescDefined ? desc : resolvedDefaultSortDesc, }, ] - } else if (action === 'set') { - newSortBy = sortBy.map(d => { - if (d.id === columnID) { - return { - ...d, - desc, - } - } - return d - }) } else if (action === 'toggle') { + // This flips (or sets) the newSortBy = sortBy.map(d => { if (d.id === columnID) { return { ...d, - desc: !existingSortBy.desc, + desc: hasDescDefined ? desc : !existingSortBy.desc, } } return d }) } else if (action === 'remove') { - newSortBy = [] + newSortBy = sortBy.filter(d => d.id !== columnID) } return { diff --git a/src/sortTypes.js b/src/sortTypes.js index ec7cf37..3ddc735 100644 --- a/src/sortTypes.js +++ b/src/sortTypes.js @@ -3,7 +3,7 @@ const reSplitAlphaNumeric = /([0-9]+)/gm // Mixed sorting is slow, but very inclusive of many edge cases. // It handles numbers, mixed alphanumeric combinations, and even // null, undefined, and Infinity -export const alphaNumeric = (a, b) => { +export const alphanumeric = (a, b) => { // Force to strings (or "" for unsupported types) a = toString(a) b = toString(b)