Change actions and addActions to be simpler to use

This commit is contained in:
tannerlinsley 2019-07-23 12:30:15 -06:00
parent e82d2d4b36
commit c7d6562d04
8 changed files with 24 additions and 29 deletions

View File

@ -1,11 +1,18 @@
const actions = {} const actions = {}
const types = new Set()
export { actions, types } export { actions }
export const addActions = acts => { export const addActions = (...acts) => {
Object.keys(acts).forEach(key => { acts.forEach(action => {
types.add(acts[key]) if (actions[action]) {
actions[key] = acts[key] 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}`
}) })
} }

View File

@ -1,6 +1,6 @@
import React from 'react' import React from 'react'
// //
import { types } from '../actions' import { actions } from '../actions'
export const defaultState = {} export const defaultState = {}
@ -32,11 +32,12 @@ export const useTableState = (
const reducedSetState = React.useCallback( const reducedSetState = React.useCallback(
(updater, type) => { (updater, type) => {
return setState(old => { return setState(old => {
if (!types.has(type)) { if (!actions[type]) {
console.info({ console.info({
currentState: old,
stateUpdaterFn: updater, stateUpdaterFn: updater,
actionType: type, actionType: type,
currentState: old, supportedActions: actions,
}) })
throw new Error('Detected an unknown table action! (Details Above)') throw new Error('Detected an unknown table action! (Details Above)')
} }

View File

@ -1,5 +1,5 @@
import * as utils from './utils'; import * as utils from './utils'
export { utils }; export { utils }
export { useTable } from './hooks/useTable' export { useTable } from './hooks/useTable'
export { useColumns } from './hooks/useColumns' export { useColumns } from './hooks/useColumns'
export { useRows } from './hooks/useRows' export { useRows } from './hooks/useRows'

View File

@ -7,10 +7,7 @@ import { defaultState } from '../hooks/useTableState'
defaultState.expanded = {} defaultState.expanded = {}
addActions({ addActions('toggleExpanded', 'useExpanded')
toggleExpanded: '__toggleExpanded__',
useExpanded: '__useExpanded__',
})
const propTypes = { const propTypes = {
manualExpandedKey: PropTypes.string, manualExpandedKey: PropTypes.string,

View File

@ -7,10 +7,7 @@ import { addActions, actions } from '../actions'
import { defaultState } from '../hooks/useTableState' import { defaultState } from '../hooks/useTableState'
defaultState.filters = {} defaultState.filters = {}
addActions({ addActions('setFilter', 'setAllFilters')
setFilter: '__setFilter__',
setAllFilters: '__setAllFilters__',
})
const propTypes = { const propTypes = {
// General // General

View File

@ -13,9 +13,7 @@ import {
defaultState.groupBy = [] defaultState.groupBy = []
addActions({ addActions('toggleGroupBy')
toggleGroupBy: '__toggleGroupBy__',
})
const propTypes = { const propTypes = {
// General // General

View File

@ -8,10 +8,7 @@ import { defaultState } from '../hooks/useTableState'
defaultState.pageSize = 10 defaultState.pageSize = 10
defaultState.pageIndex = 0 defaultState.pageIndex = 0
addActions({ addActions('pageChange', 'pageSizeChange')
pageChange: '__pageChange__',
pageSizeChange: '__pageSizeChange__',
})
const propTypes = { const propTypes = {
// General // General

View File

@ -14,9 +14,7 @@ import {
defaultState.sortBy = [] defaultState.sortBy = []
addActions({ addActions('sortByChange')
sortByChange: '__sortByChange__',
})
const propTypes = { const propTypes = {
// General // General