From ed3dd9e6b5e02689f4deb18b7eaf6552829e68ab Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Wed, 20 Nov 2019 09:35:40 -0700 Subject: [PATCH] Add "default" sort/filter/groupBy options for non accessors, rename disableGrouping --- CHANGELOG.md | 13 +++++++++++++ docs/api.md | 16 ++++++++++++++++ src/plugin-hooks/useFilters.js | 10 ++++++++-- src/plugin-hooks/useGroupBy.js | 18 ++++++++++++------ src/plugin-hooks/useSortBy.js | 10 ++++++++-- 5 files changed, 57 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e856ac4..cc8e914 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,16 @@ +## 7.0.0-beta.13 + +- Added options + - `defaultCanSort` + - `defaultCanFilter` + - `defaultCanGroupBy` + - `column.defaultCanSort` + - `column.defaultCanFilter` + - `column.defaultCanGroupBy` +- Renamed + - `disableGrouping` to `disableGroupBy` + - `column.disableGrouping` to `column.disableGroupBy` + ## 7.0.0-beta.0 - Massive changes to the entire project and library. Please consult the README and documentation for more information regarding these changes. diff --git a/docs/api.md b/docs/api.md index 6a753a3..28ab702 100644 --- a/docs/api.md +++ b/docs/api.md @@ -361,6 +361,10 @@ The following options are supported via the main options object passed to `useTa - Enables sorting detection functionality, but does not automatically perform row sorting. Turn this on if you wish to implement your own sorting outside of the table (eg. server-side or manual row grouping/nesting) - `disableSorting: Bool` - Disables sorting for every column in the entire table. +- `defaultCanSort: Bool` + - Optional + - Defaults to `false` + - If set to `true`, all columns will be sortable, regardless if they have a valid `accessor` - `disableMultiSort: Bool` - Disables multi-sorting for the entire table. - `isMultiSortEvent: Function` @@ -385,6 +389,10 @@ The following options are supported via the main options object passed to `useTa The following options are supported on any `Column` object passed to the `columns` options in `useTable()` +- `defaultCanSort: Bool` + - Optional + - Defaults to `false` + - If set to `true`, this column will be sortable, regardless if it has a valid `accessor` - `disableSorting: Bool` - Optional - Defaults to `false` @@ -471,6 +479,10 @@ The following options are supported via the main options object passed to `useTa - Turn this on if you wish to implement your own row filter outside of the table (eg. server-side or manual row grouping/nesting) - `disableFilters: Bool` - Disables filtering for every column in the entire table. +- `defaultCanFilter: Bool` + - Optional + - Defaults to `false` + - If set to `true`, all columns will be filterable, regardless if they have a valid `accessor` - `filterTypes: Object` - Must be **memoized** - Allows overriding or adding additional filter types for columns to use. If a column's filter type isn't found on this object, it will default to using the built-in filter types. @@ -488,6 +500,10 @@ The following options are supported on any `Column` object passed to the `column - `disableFilters: Bool` - Optional - If set to `true`, will disable filtering for this column +- `defaultCanFilter: Bool` + - Optional + - Defaults to `false` + - If set to `true`, this column will be filterable, regardless if it has a valid `accessor` - `filter: String | Function` - Optional - Defaults to `text` diff --git a/src/plugin-hooks/useFilters.js b/src/plugin-hooks/useFilters.js index 5a22f9a..4182c7b 100755 --- a/src/plugin-hooks/useFilters.js +++ b/src/plugin-hooks/useFilters.js @@ -37,6 +37,7 @@ function useMain(instance) { flatColumns, filterTypes: userFilterTypes, manualFilters, + defaultCanFilter = false, disableFilters, state: { filters }, setState, @@ -108,7 +109,12 @@ function useMain(instance) { } flatColumns.forEach(column => { - const { id, accessor, disableFilters: columnDisableFilters } = column + const { + id, + accessor, + defaultCanFilter: columnDefaultCanFilter, + disableFilters: columnDisableFilters, + } = column // Determine if a column is filterable column.canFilter = accessor @@ -117,7 +123,7 @@ function useMain(instance) { disableFilters === true ? false : undefined, true ) - : false + : getFirstDefined(columnDefaultCanFilter, defaultCanFilter, false) // Provide the column a way of updating the filter value column.setFilter = val => setFilter(column.id, val) diff --git a/src/plugin-hooks/useGroupBy.js b/src/plugin-hooks/useGroupBy.js index 7f362ec..5341b81 100755 --- a/src/plugin-hooks/useGroupBy.js +++ b/src/plugin-hooks/useGroupBy.js @@ -27,13 +27,13 @@ const propTypes = { PropTypes.oneOfType([PropTypes.func, PropTypes.string]) ), ]), - disableGrouping: PropTypes.bool, + disableGroupBy: PropTypes.bool, Aggregated: PropTypes.any, }) ), groupByFn: PropTypes.func, manualGrouping: PropTypes.bool, - disableGrouping: PropTypes.bool, + disableGroupBy: PropTypes.bool, aggregations: PropTypes.object, } @@ -76,7 +76,8 @@ function useMain(instance) { flatHeaders, groupByFn = defaultGroupByFn, manualGroupBy, - disableGrouping, + defaultCanGroupBy, + disableGroupBy, aggregations: userAggregations = {}, hooks, plugins, @@ -87,17 +88,22 @@ function useMain(instance) { ensurePluginOrder(plugins, [], 'useGroupBy', ['useSortBy', 'useExpanded']) flatColumns.forEach(column => { - const { id, accessor, disableGrouping: columnDisableGrouping } = column + const { + id, + accessor, + defaultGroupBy: defaultColumnGroupBy, + disableGroupBy: columnDisableGrouping, + } = column column.isGrouped = groupBy.includes(id) column.groupedIndex = groupBy.indexOf(id) column.canGroupBy = accessor ? getFirstDefined( columnDisableGrouping === true ? false : undefined, - disableGrouping === true ? false : undefined, + disableGroupBy === true ? false : undefined, true ) - : false + : getFirstDefined(defaultColumnGroupBy, defaultCanGroupBy, false) if (column.canGroupBy) { column.toggleGroupBy = () => toggleGroupBy(column.id) diff --git a/src/plugin-hooks/useSortBy.js b/src/plugin-hooks/useSortBy.js index 5e43d4a..c47e567 100755 --- a/src/plugin-hooks/useSortBy.js +++ b/src/plugin-hooks/useSortBy.js @@ -55,6 +55,7 @@ function useMain(instance) { orderByFn = defaultOrderByFn, sortTypes: userSortTypes, manualSorting, + defaultCanSort, disableSorting, disableSortRemove, disableMultiRemove, @@ -162,7 +163,12 @@ function useMain(instance) { // Add the getSortByToggleProps method to columns and headers flatHeaders.forEach(column => { - const { accessor, disableSorting: columnDisableSorting, id } = column + const { + accessor, + canSort: defaultColumnCanSort, + disableSorting: columnDisableSorting, + id, + } = column const canSort = accessor ? getFirstDefined( @@ -170,7 +176,7 @@ function useMain(instance) { disableSorting === true ? false : undefined, true ) - : false + : getFirstDefined(defaultCanSort, defaultColumnCanSort, false) column.canSort = canSort