mirror of
https://github.com/gosticks/react-table.git
synced 2026-02-01 14:27:30 +00:00
useTableState was an early and hasty abstraction that hasn't proved useful in many ways. Anything you could do with useTableState, you could easily do using the same options (assuming they exist) in the useTable hook. For this reason, state is now a first class citizen of the useTable hook, along with more sane properties and option locations for anything pertaining to state.
79 lines
1.9 KiB
JavaScript
79 lines
1.9 KiB
JavaScript
import React from 'react'
|
|
import PropTypes from 'prop-types'
|
|
|
|
import { addActions, actions } from '../actions'
|
|
import { defaultState } from '../hooks/useTable'
|
|
|
|
defaultState.columnOrder = []
|
|
|
|
addActions('setColumnOrder')
|
|
|
|
const propTypes = {
|
|
initialRowStateAccessor: PropTypes.func,
|
|
}
|
|
|
|
export const useColumnOrder = hooks => {
|
|
hooks.columnsBeforeHeaderGroupsDeps.push((deps, instance) => {
|
|
return [...deps, instance.state.columnOrder]
|
|
})
|
|
hooks.columnsBeforeHeaderGroups.push(columnsBeforeHeaderGroups)
|
|
hooks.useMain.push(useMain)
|
|
}
|
|
|
|
useColumnOrder.pluginName = 'useColumnOrder'
|
|
|
|
function columnsBeforeHeaderGroups(columns, instance) {
|
|
const {
|
|
state: { columnOrder },
|
|
} = instance
|
|
|
|
// If there is no order, return the normal columns
|
|
if (!columnOrder || !columnOrder.length) {
|
|
return columns
|
|
}
|
|
|
|
const columnOrderCopy = [...columnOrder]
|
|
|
|
// If there is an order, make a copy of the columns
|
|
const columnsCopy = [...columns]
|
|
|
|
// And make a new ordered array of the columns
|
|
const columnsInOrder = []
|
|
|
|
// Loop over the columns and place them in order into the new array
|
|
while (columnsCopy.length && columnOrderCopy.length) {
|
|
const targetColumnID = columnOrderCopy.shift()
|
|
const foundIndex = columnsCopy.findIndex(d => d.id === targetColumnID)
|
|
if (foundIndex > -1) {
|
|
columnsInOrder.push(columnsCopy.splice(foundIndex, 1)[0])
|
|
}
|
|
}
|
|
|
|
// If there are any columns left, add them to the end
|
|
return [...columnsInOrder, ...columnsCopy]
|
|
}
|
|
|
|
function useMain(instance) {
|
|
PropTypes.checkPropTypes(propTypes, instance, 'property', 'useColumnOrder')
|
|
|
|
const { setState } = instance
|
|
|
|
const setColumnOrder = React.useCallback(
|
|
updater => {
|
|
return setState(old => {
|
|
return {
|
|
...old,
|
|
columnOrder:
|
|
typeof updater === 'function' ? updater(old.columnOrder) : updater,
|
|
}
|
|
}, actions.setColumnOrder)
|
|
},
|
|
[setState]
|
|
)
|
|
|
|
return {
|
|
...instance,
|
|
setColumnOrder,
|
|
}
|
|
}
|