Move default sortBy and orderBy to plugin files

This commit is contained in:
Tanner Linsley 2020-03-09 07:58:42 -06:00
parent bfcc08e7eb
commit 3aa016e056
4 changed files with 27 additions and 29 deletions

View File

@ -3,8 +3,8 @@ export { useTable } from './hooks/useTable'
export { useExpanded } from './plugin-hooks/useExpanded'
export { useFilters } from './plugin-hooks/useFilters'
export { useGlobalFilter } from './plugin-hooks/useGlobalFilter'
export { useGroupBy } from './plugin-hooks/useGroupBy'
export { useSortBy } from './plugin-hooks/useSortBy'
export { useGroupBy, defaultGroupByFn } from './plugin-hooks/useGroupBy'
export { useSortBy, defaultOrderByFn } from './plugin-hooks/useSortBy'
export { usePagination } from './plugin-hooks/usePagination'
export { _UNSTABLE_usePivotColumns } from './plugin-hooks/_UNSTABLE_usePivotColumns'
export { useRowSelect } from './plugin-hooks/useRowSelect'

View File

@ -7,7 +7,6 @@ import { getFirstDefined, flattenBy } from '../utils'
import {
actions,
makePropGetter,
defaultGroupByFn,
ensurePluginOrder,
useMountedLayoutEffect,
useGetLatest,
@ -408,3 +407,14 @@ function prepareRow(row) {
cell.isAggregated = !cell.isGrouped && !cell.isPlaceholder && row.canExpand
})
}
export function defaultGroupByFn(rows, columnId) {
return rows.reduce((prev, row, i) => {
// TODO: Might want to implement a key serializer here so
// irregular column values can still be grouped if needed?
const resKey = `${row.values[columnId]}`
prev[resKey] = Array.isArray(prev[resKey]) ? prev[resKey] : []
prev[resKey].push(row)
return prev
}, {})
}

View File

@ -5,7 +5,6 @@ import {
ensurePluginOrder,
defaultColumn,
makePropGetter,
defaultOrderByFn,
useGetLatest,
useMountedLayoutEffect,
} from '../publicUtils'
@ -360,3 +359,17 @@ function useInstance(instance) {
toggleSortBy,
})
}
export function defaultOrderByFn(arr, funcs, dirs) {
return [...arr].sort((rowA, rowB) => {
for (let i = 0; i < funcs.length; i += 1) {
const sortFn = funcs[i]
const desc = dirs[i] === false || dirs[i] === 'desc'
const sortInt = sortFn(rowA, rowB)
if (sortInt !== 0) {
return desc ? -sortInt : sortInt
}
}
return dirs[0] ? rowA.index - rowB.index : rowB.index - rowA.index
})
}

View File

@ -13,31 +13,6 @@ export const defaultColumn = {
maxWidth: Number.MAX_SAFE_INTEGER,
}
export function defaultOrderByFn(arr, funcs, dirs) {
return [...arr].sort((rowA, rowB) => {
for (let i = 0; i < funcs.length; i += 1) {
const sortFn = funcs[i]
const desc = dirs[i] === false || dirs[i] === 'desc'
const sortInt = sortFn(rowA, rowB)
if (sortInt !== 0) {
return desc ? -sortInt : sortInt
}
}
return dirs[0] ? rowA.index - rowB.index : rowB.index - rowA.index
})
}
export function defaultGroupByFn(rows, columnId) {
return rows.reduce((prev, row, i) => {
// TODO: Might want to implement a key serializer here so
// irregular column values can still be grouped if needed?
const resKey = `${row.values[columnId]}`
prev[resKey] = Array.isArray(prev[resKey]) ? prev[resKey] : []
prev[resKey].push(row)
return prev
}, {})
}
function mergeProps(...propList) {
return propList.reduce((props, next) => {
const { style, className, ...rest } = next