mirror of
https://github.com/gosticks/react-table.git
synced 2025-10-16 11:55:36 +00:00
Move default sortBy and orderBy to plugin files
This commit is contained in:
parent
bfcc08e7eb
commit
3aa016e056
@ -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'
|
||||
|
||||
@ -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
|
||||
}, {})
|
||||
}
|
||||
|
||||
@ -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
|
||||
})
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user