react-table#functionalUpdate JavaScript Examples

The following examples show how to use react-table#functionalUpdate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useColumnSummary.js    From react-table-plugins with MIT License 5 votes vote down vote up
// Reducer
function reducer(state, action, previousState, instance) {
  if (action.type === actions.init) {
    return {
      columnSummary: {},
      ...state,
    };
  }

  if (action.type === actions.resetColumnSummary) {
    return {
      ...state,
      columnSummary: instance.initialState.columnSummary || {},
    };
  }

  if (action.type === actions.setColumnSummary) {
    return {
      ...state,
      columnSummary: functionalUpdate(
        action.columnSummary,
        state.columnSummary
      ),
    };
  }
}
Example #2
Source File: useCellRangeSelection.js    From react-table-plugins with MIT License 2 votes vote down vote up
// currentSelectedCellIds: Is for currently selected range
// selectedCellIds: Contains all selected cells
// On cellRangeSelectionEnd: we move currentSelectedCellIds to selectedCellIds
function reducer (state, action, previousState, instance) {
  if (action.type === actions.init) {
    return {
      ...state,
      selectedCellIds: { ...instance.initialState.selectedCellIds } || {},
      isSelectingCells: false,
      startCellSelection: null,
      endCellSelection: null,
      currentSelectedCellIds: {}
    }
  }

  if (action.type === actions.cellRangeSelectionStart) {
    const { startCell, event } = action

    let newState = Object.assign(state.selectedCellIds, {})
    if (event.ctrlKey === true) {
      if (newState[startCell]) {
        delete newState[startCell]
      } else {
        newState[startCell] = true
      }
    } else {
      newState = {}
    }

    return {
      ...state,
      selectedCellIds:
        {
          ...newState
        } || {},
      isSelectingCells: true,
      startCellSelection: startCell
    }
  }

  if (action.type === actions.cellRangeSelecting) {
    const { selectingEndCell } = action
    const {
      state: { startCellSelection },
      getCellsBetweenId
    } = instance

    // Get cells between cell ids (range)
    let newState = getCellsBetweenId(startCellSelection, selectingEndCell)

    return {
      ...state,
      endCellSelection: selectingEndCell,
      currentSelectedCellIds: newState
    }
  }

  if (action.type === actions.cellRangeSelectionEnd) {
    const {
      state: { selectedCellIds, currentSelectedCellIds }
    } = instance

    return {
      ...state,
      selectedCellIds: { ...selectedCellIds, ...currentSelectedCellIds },
      isSelectingCells: false,
      currentSelectedCellIds: {},
      startCellSelection: null,
      endCellSelection: null
    }
  }

  if (action.type === actions.setSelectedCellIds) {
    const selectedCellIds = functionalUpdate(
      action.selectedCellIds,
      state.selectedCellIds
    )

    return {
      ...state,
      selectedCellIds: selectedCellIds
    }
  }
}