Relocate columns and row logic, fix columns and useGroupBy to be more pure

Since useColumns was relying on groupBy logic, this was code smell. I wanted useGroupBy to be able to add that logic all by itself and not have to have dependencies in the core of the table.

To fix that, I've moved the core column and row logic to the useTable hook and added a new hook 'columnsBeforeHeaderGroups' to allow useGroupBy to do what i needs in a more pure way.
This commit is contained in:
tannerlinsley
2019-07-29 11:00:07 -06:00
parent cadd8bf62a
commit dc73347003
9 changed files with 587 additions and 451 deletions

View File

@@ -22,6 +22,7 @@ const propTypes = {
PropTypes.shape({
sortType: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
sortDescFirst: PropTypes.bool,
disableSorting: PropTypes.bool,
})
),
orderByFn: PropTypes.func,
@@ -67,10 +68,10 @@ export const useSortBy = props => {
}
columns.forEach(column => {
const { accessor, canSortBy } = column
const { accessor, disableSorting: columnDisableSorting } = column
column.canSortBy = accessor
? getFirstDefined(
canSortBy,
columnDisableSorting,
disableSorting === true ? false : undefined,
true
)
@@ -78,7 +79,7 @@ export const useSortBy = props => {
})
// Updates sorting based on a columnID, desc flag and multi flag
const toggleSortByID = (columnID, desc, multi) => {
const toggleSortBy = (columnID, desc, multi) => {
return setState(old => {
const { sortBy } = old
@@ -167,7 +168,7 @@ export const useSortBy = props => {
columns.forEach(column => {
if (column.canSortBy) {
column.toggleSortBy = (desc, multi) =>
toggleSortByID(column.id, desc, multi)
toggleSortBy(column.id, desc, multi)
}
})
return columns
@@ -279,6 +280,8 @@ export const useSortBy = props => {
return {
...props,
toggleSortBy,
rows: sortedRows,
preSortedRows: rows,
}
}