From 32b6e15219f940a261cfbb761be051db283044e9 Mon Sep 17 00:00:00 2001 From: Iddan Aaronsohn Date: Sun, 26 May 2024 14:30:21 +0200 Subject: [PATCH] Cleanup binding actions code --- src/Spreadsheet.tsx | 57 +++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 36 deletions(-) diff --git a/src/Spreadsheet.tsx b/src/Spreadsheet.tsx index 70d0b3a9..58bacc1a 100644 --- a/src/Spreadsheet.tsx +++ b/src/Spreadsheet.tsx @@ -177,42 +177,27 @@ const Spreadsheet = ( const rootRef = React.useRef(null); - const copy = React.useCallback(() => dispatch(Actions.copy()), [dispatch]); - const cut = React.useCallback(() => dispatch(Actions.cut()), [dispatch]); - const paste = React.useCallback( - (data: string) => dispatch(Actions.paste(data)), - [dispatch] - ); - const onKeyDownAction = React.useCallback( - (event: React.KeyboardEvent) => dispatch(Actions.keyDown(event)), - [dispatch] - ); - const onKeyPress = React.useCallback( - (event: React.KeyboardEvent) => dispatch(Actions.keyPress(event)), - [dispatch] - ); - const onDragStart = React.useCallback( - () => dispatch(Actions.dragStart()), - [dispatch] - ); - const onDragEnd = React.useCallback( - () => dispatch(Actions.dragEnd()), - [dispatch] - ); - const setData = React.useCallback( - (data: Matrix.Matrix) => dispatch(Actions.setData(data)), - [dispatch] - ); - const setCreateFormulaParser = React.useCallback( - (createFormulaParser: Types.CreateFormulaParser) => - dispatch(Actions.setCreateFormulaParser(createFormulaParser)), - [dispatch] - ); - const blur = React.useCallback(() => dispatch(Actions.blur()), [dispatch]); - const setSelection = React.useCallback( - (selection: Selection) => dispatch(Actions.setSelection(selection)), - [dispatch] - ); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const useAction = Actions.Action>( + action: T + ) => { + return React.useCallback( + (...args: Parameters) => dispatch(action(...args)), + [action] + ); + }; + + const cut = useAction(Actions.cut); + const copy = useAction(Actions.copy); + const paste = useAction(Actions.paste); + const onKeyDownAction = useAction(Actions.keyDown); + const onKeyPress = useAction(Actions.keyPress); + const onDragStart = useAction(Actions.dragStart); + const onDragEnd = useAction(Actions.dragEnd); + const setData = useAction(Actions.setData); + const setCreateFormulaParser = useAction(Actions.setCreateFormulaParser); + const blur = useAction(Actions.blur); + const setSelection = useAction(Actions.setSelection); // Track active const prevActiveRef = React.useRef(state.active);