fix(hooks): repair useStoreActions hook

This commit is contained in:
moklick
2021-01-21 19:35:04 +01:00
parent e9a7ade236
commit 252552a694
8 changed files with 63 additions and 37 deletions
+3 -2
View File
@@ -1,10 +1,11 @@
import React from 'react'; import React from 'react';
import { useStoreState, useStoreActions } from 'react-flow-renderer'; import { useStoreState, useStoreActions, isNode } from 'react-flow-renderer';
const Sidebar = () => { const Sidebar = () => {
const nodes = useStoreState((store) => store.nodes); const elements = useStoreState((store) => store.elements);
const transform = useStoreState((store) => store.transform); const transform = useStoreState((store) => store.transform);
const setSelectedElements = useStoreActions((actions) => actions.setSelectedElements); const setSelectedElements = useStoreActions((actions) => actions.setSelectedElements);
const nodes = elements.filter(isNode);
const selectAll = () => { const selectAll = () => {
setSelectedElements(nodes.map((node) => ({ id: node.id, type: node.type }))); setSelectedElements(nodes.map((node) => ({ id: node.id, type: node.type })));
+1 -1
View File
@@ -52,7 +52,7 @@ const NodeRenderer = (props: NodeRendererProps) => {
nodeElement: entry.target as HTMLDivElement, nodeElement: entry.target as HTMLDivElement,
})); }));
batchUpdateNodeDimensions({ updates }); batchUpdateNodeDimensions(updates);
}); });
}, []); }, []);
+9 -12
View File
@@ -1,20 +1,17 @@
import React, { FC } from 'react'; import React, { FC, useContext } from 'react';
import { Provider } from 'react-redux'; import { Provider, ReactReduxContext } from 'react-redux';
import store from '../../store'; import store from '../../store';
// import { useStore } from '../../store/hooks';
const Wrapper: FC = ({ children }) => { const Wrapper: FC = ({ children }) => {
// const reactFlowStore = useStore(); const contextValue = useContext(ReactReduxContext);
// const isWrapepdWithReactFlowProvider = reactFlowStore?.getState()?.reactFlowVersion; const isWrappedWithReactFlowProvider = contextValue?.store?.getState()?.reactFlowVersion;
// console.log(isWrapepdWithReactFlowProvider); if (isWrappedWithReactFlowProvider) {
// we need to wrap it with a fragment because t's not allowed for children to be a ReactNode
// if (isWrapepdWithReactFlowProvider) { // https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18051
// // we need to wrap it with a fragment because t's not allowed for children to be a ReactNode return <>{children}</>;
// // https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18051 }
// return <>{children}</>;
// }
return <Provider store={store}>{children}</Provider>; return <Provider store={store}>{children}</Provider>;
}; };
+3 -2
View File
@@ -3,7 +3,7 @@ import { zoomIdentity } from 'd3-zoom';
import { useStoreState, useStore } from '../store/hooks'; import { useStoreState, useStore } from '../store/hooks';
import { clamp } from '../utils'; import { clamp } from '../utils';
import { getRectOfNodes } from '../utils/graph'; import { getRectOfNodes, isNode } from '../utils/graph';
import { FitViewParams, FlowTransform, ZoomPanHelperFunctions, Rect, Transform } from '../types'; import { FitViewParams, FlowTransform, ZoomPanHelperFunctions, Rect, Transform } from '../types';
const initialZoomPanHelper: ZoomPanHelperFunctions = { const initialZoomPanHelper: ZoomPanHelperFunctions = {
@@ -54,7 +54,8 @@ const usePanZoomHelper = (): ZoomPanHelperFunctions => {
d3Zoom.transform(d3Selection, nextTransform); d3Zoom.transform(d3Selection, nextTransform);
}, },
fitView: (options: FitViewParams = { padding: 0.1 }) => { fitView: (options: FitViewParams = { padding: 0.1 }) => {
const { nodes, width, height, minZoom, maxZoom } = store.getState(); const { elements, width, height, minZoom, maxZoom } = store.getState();
const nodes = elements.filter(isNode);
if (!nodes.length) { if (!nodes.length) {
return; return;
+1 -3
View File
@@ -158,9 +158,7 @@ interface UpdateTransform {
interface UpdateSize { interface UpdateSize {
type: typeof UPDATE_SIZE; type: typeof UPDATE_SIZE;
payload: { payload: Dimensions;
size: Dimensions;
};
} }
interface InitD3Zoom { interface InitD3Zoom {
+38 -4
View File
@@ -161,10 +161,8 @@ export const updateTransform = (transform: Transform): ActionTypes => ({
export const updateSize = (size: Dimensions): ActionTypes => ({ export const updateSize = (size: Dimensions): ActionTypes => ({
type: UPDATE_SIZE, type: UPDATE_SIZE,
payload: { payload: {
size: { width: size.width || 500,
width: size.width || 500, height: size.height || 500,
height: size.height || 500,
},
}, },
}); });
@@ -241,3 +239,39 @@ export const setConnectionMode = (connectionMode: ConnectionMode): ActionTypes =
type: SET_CONNECTION_MODE, type: SET_CONNECTION_MODE,
payload: { connectionMode }, payload: { connectionMode },
}); });
export const actions = {
setOnConnect,
setOnConnectStart,
setOnConnectStop,
setOnConnectEnd,
setElements,
batchUpdateNodeDimensions,
updateNodeDimensions,
updateNodePos,
updateNodePosDiff,
setUserSelection,
updateUserSelection,
unsetUserSelection,
setSelection,
unsetNodesSelection,
resetSelectedElements,
setSelectedElements,
addSelectedElements,
updateTransform,
updateSize,
initD3Zoom,
setMinZoom,
setMaxZoom,
setTranslateExtent,
setConnectionPosition,
setConnectionNodeId,
setSnapToGrid,
setSnapGrid,
setInteractive,
setNodesDraggable,
setNodesConnectable,
setElementsSelectable,
setMultiSelectionActive,
setConnectionMode,
};
+7 -12
View File
@@ -1,24 +1,19 @@
import { bindActionCreators, ActionCreatorsMapObject } from 'redux'; import { bindActionCreators } from 'redux';
import { useStore as useStoreRedux, useSelector, useDispatch, TypedUseSelectorHook } from 'react-redux'; import { useStore as useStoreRedux, useSelector, useDispatch, TypedUseSelectorHook } from 'react-redux';
import { useMemo } from 'react'; import { useMemo } from 'react';
import { ReactFlowState, AppDispatch } from './index'; import { ReactFlowState, AppDispatch } from './index';
import { ActionTypes } from './action-types'; import { actions } from './actions';
export const useTypedSelector: TypedUseSelectorHook<ReactFlowState> = useSelector; export const useTypedSelector: TypedUseSelectorHook<ReactFlowState> = useSelector;
export function useActions(actions: ActionTypes, deps?: any): ActionTypes { export function useActions(actionSelector: any): any {
const dispatch: AppDispatch = useDispatch(); const dispatch: AppDispatch = useDispatch();
const action = useMemo( const action = useMemo(() => {
() => { const currAction = actionSelector(actions);
if (Array.isArray(actions)) { return bindActionCreators(currAction, dispatch);
return actions.map((a) => bindActionCreators(a, dispatch)); }, [dispatch, actionSelector]);
}
return bindActionCreators<ActionCreatorsMapObject<ActionTypes>>(actions, dispatch);
},
deps ? [dispatch, ...deps] : [dispatch]
);
return action; return action;
} }
+1 -1
View File
@@ -306,7 +306,7 @@ export function reactFlowReducer(state = initialState, action: ActionTypes): Rea
isDragging, isDragging,
position: { position: {
x: el.__rf.position.x + diff.x, x: el.__rf.position.x + diff.x,
y: el.__rf.position.x + diff.y, y: el.__rf.position.y + diff.y,
}, },
}, },
}; };