mirror of
https://github.com/gosticks/react-table.git
synced 2025-10-16 11:55:36 +00:00
Change actions and addActions to be simpler to use
This commit is contained in:
parent
e82d2d4b36
commit
c7d6562d04
@ -1,11 +1,18 @@
|
||||
const actions = {}
|
||||
const types = new Set()
|
||||
|
||||
export { actions, types }
|
||||
export { actions }
|
||||
|
||||
export const addActions = acts => {
|
||||
Object.keys(acts).forEach(key => {
|
||||
types.add(acts[key])
|
||||
actions[key] = acts[key]
|
||||
export const addActions = (...acts) => {
|
||||
acts.forEach(action => {
|
||||
if (actions[action]) {
|
||||
throw new Error(
|
||||
`An React Table action type called ${action} has already been registered!`
|
||||
)
|
||||
}
|
||||
// Action values are formatted this way to discourage
|
||||
// you (the dev) from interacting with them in any way
|
||||
// other than importing `{ actions } from 'react-table'`
|
||||
// and referencing an action via `actions[actionName]`
|
||||
actions[action] = `React Table Action: ${action}`
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import React from 'react'
|
||||
//
|
||||
import { types } from '../actions'
|
||||
import { actions } from '../actions'
|
||||
|
||||
export const defaultState = {}
|
||||
|
||||
@ -32,11 +32,12 @@ export const useTableState = (
|
||||
const reducedSetState = React.useCallback(
|
||||
(updater, type) => {
|
||||
return setState(old => {
|
||||
if (!types.has(type)) {
|
||||
if (!actions[type]) {
|
||||
console.info({
|
||||
currentState: old,
|
||||
stateUpdaterFn: updater,
|
||||
actionType: type,
|
||||
currentState: old,
|
||||
supportedActions: actions,
|
||||
})
|
||||
throw new Error('Detected an unknown table action! (Details Above)')
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import * as utils from './utils';
|
||||
export { utils };
|
||||
import * as utils from './utils'
|
||||
export { utils }
|
||||
export { useTable } from './hooks/useTable'
|
||||
export { useColumns } from './hooks/useColumns'
|
||||
export { useRows } from './hooks/useRows'
|
||||
|
||||
@ -7,10 +7,7 @@ import { defaultState } from '../hooks/useTableState'
|
||||
|
||||
defaultState.expanded = {}
|
||||
|
||||
addActions({
|
||||
toggleExpanded: '__toggleExpanded__',
|
||||
useExpanded: '__useExpanded__',
|
||||
})
|
||||
addActions('toggleExpanded', 'useExpanded')
|
||||
|
||||
const propTypes = {
|
||||
manualExpandedKey: PropTypes.string,
|
||||
|
||||
@ -7,10 +7,7 @@ import { addActions, actions } from '../actions'
|
||||
import { defaultState } from '../hooks/useTableState'
|
||||
|
||||
defaultState.filters = {}
|
||||
addActions({
|
||||
setFilter: '__setFilter__',
|
||||
setAllFilters: '__setAllFilters__',
|
||||
})
|
||||
addActions('setFilter', 'setAllFilters')
|
||||
|
||||
const propTypes = {
|
||||
// General
|
||||
|
||||
@ -13,9 +13,7 @@ import {
|
||||
|
||||
defaultState.groupBy = []
|
||||
|
||||
addActions({
|
||||
toggleGroupBy: '__toggleGroupBy__',
|
||||
})
|
||||
addActions('toggleGroupBy')
|
||||
|
||||
const propTypes = {
|
||||
// General
|
||||
|
||||
@ -8,10 +8,7 @@ import { defaultState } from '../hooks/useTableState'
|
||||
defaultState.pageSize = 10
|
||||
defaultState.pageIndex = 0
|
||||
|
||||
addActions({
|
||||
pageChange: '__pageChange__',
|
||||
pageSizeChange: '__pageSizeChange__',
|
||||
})
|
||||
addActions('pageChange', 'pageSizeChange')
|
||||
|
||||
const propTypes = {
|
||||
// General
|
||||
|
||||
@ -14,9 +14,7 @@ import {
|
||||
|
||||
defaultState.sortBy = []
|
||||
|
||||
addActions({
|
||||
sortByChange: '__sortByChange__',
|
||||
})
|
||||
addActions('sortByChange')
|
||||
|
||||
const propTypes = {
|
||||
// General
|
||||
|
||||
Loading…
Reference in New Issue
Block a user