All files / src/editor store.tsx

0% Statements 0/9
100% Branches 0/0
0% Functions 0/4
0% Lines 0/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36                                                                       
import { createContext, useContext, useReducer } from 'react';
 
type InitialState = {
  /**
   * When a callback function is passed in, edit functionality is enabled. The callback is invoked before edits are completed.
   * @returns {boolean}  Returning false from onEdit will prevent the change from being made.
   */
  onEdit?: (option: {
    value: unknown;
    oldValue: unknown;
    keyName?: string | number;
    parentName?: string | number;
    type?: 'value' | 'key';
  }) => boolean;
};
type Dispatch = React.Dispatch<InitialState>;
 
const initialState: InitialState = {};
export const Context = createContext<InitialState>(initialState);
 
const reducer = (state: InitialState, action: InitialState) => ({
  ...state,
  ...action,
});
 
export const Dispatch = createContext<Dispatch>(() => {});
Dispatch.displayName = 'JVR.Editor.Dispatch';
 
export const useStore = () => {
  return useContext(Context);
};
 
export function useStoreReducer(initialState: InitialState) {
  return useReducer(reducer, initialState);
}