Merged PR 165: Move useEffects to named functions
Move useEffects to named functions
This commit is contained in:
parent
29625dce28
commit
ec3fddec9d
6 changed files with 150 additions and 91 deletions
|
@ -1,5 +1,51 @@
|
|||
import { RefObject, Dispatch, SetStateAction, useEffect } from 'react';
|
||||
import { IPoint } from '../../Interfaces/IPoint';
|
||||
|
||||
export function useMouseEvents(
|
||||
isContextMenuOpen: boolean,
|
||||
elementRef: RefObject<HTMLDivElement>,
|
||||
setIsContextMenuOpen: Dispatch<SetStateAction<boolean>>,
|
||||
setOnClickSymbolId: Dispatch<SetStateAction<string>>,
|
||||
setContextMenuPosition: Dispatch<SetStateAction<IPoint>>
|
||||
): void {
|
||||
useEffect(() => {
|
||||
const onContextMenu = (event: MouseEvent): void => handleRightClick(
|
||||
event,
|
||||
setIsContextMenuOpen,
|
||||
setOnClickSymbolId,
|
||||
setContextMenuPosition
|
||||
);
|
||||
|
||||
const onLeftClick = (): void => handleLeftClick(
|
||||
isContextMenuOpen,
|
||||
setIsContextMenuOpen,
|
||||
setOnClickSymbolId
|
||||
);
|
||||
|
||||
elementRef.current?.addEventListener(
|
||||
'contextmenu',
|
||||
onContextMenu
|
||||
);
|
||||
|
||||
window.addEventListener(
|
||||
'click',
|
||||
onLeftClick
|
||||
);
|
||||
|
||||
return () => {
|
||||
elementRef.current?.removeEventListener(
|
||||
'contextmenu',
|
||||
onContextMenu
|
||||
);
|
||||
|
||||
window.removeEventListener(
|
||||
'click',
|
||||
onLeftClick
|
||||
);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function handleRightClick(
|
||||
event: MouseEvent,
|
||||
setIsContextMenuOpen: React.Dispatch<React.SetStateAction<boolean>>,
|
||||
|
|
|
@ -2,7 +2,7 @@ import * as React from 'react';
|
|||
import { FixedSizeList as List } from 'react-window';
|
||||
import { Menu } from '../Menu/Menu';
|
||||
import { MenuItem } from '../Menu/MenuItem';
|
||||
import { handleLeftClick, handleRightClick } from './MouseEventHandlers';
|
||||
import { handleLeftClick, handleRightClick, useMouseEvents } from './MouseEventHandlers';
|
||||
import { IPoint } from '../../Interfaces/IPoint';
|
||||
import { ISymbolModel } from '../../Interfaces/ISymbolModel';
|
||||
import { SymbolProperties } from '../SymbolProperties/SymbolProperties';
|
||||
|
@ -29,42 +29,13 @@ export const SymbolsSidebar: React.FC<ISymbolsSidebarProps> = (props: ISymbolsSi
|
|||
const elementRef = React.useRef<HTMLDivElement>(null);
|
||||
|
||||
// Event listeners
|
||||
React.useEffect(() => {
|
||||
const onContextMenu = (event: MouseEvent): void => handleRightClick(
|
||||
event,
|
||||
setIsContextMenuOpen,
|
||||
setOnClickSymbolId,
|
||||
setContextMenuPosition
|
||||
);
|
||||
|
||||
const onLeftClick = (): void => handleLeftClick(
|
||||
isContextMenuOpen,
|
||||
setIsContextMenuOpen,
|
||||
setOnClickSymbolId
|
||||
);
|
||||
|
||||
elementRef.current?.addEventListener(
|
||||
'contextmenu',
|
||||
onContextMenu
|
||||
);
|
||||
|
||||
window.addEventListener(
|
||||
'click',
|
||||
onLeftClick
|
||||
);
|
||||
|
||||
return () => {
|
||||
elementRef.current?.removeEventListener(
|
||||
'contextmenu',
|
||||
onContextMenu
|
||||
);
|
||||
|
||||
window.removeEventListener(
|
||||
'click',
|
||||
onLeftClick
|
||||
);
|
||||
};
|
||||
});
|
||||
useMouseEvents(
|
||||
isContextMenuOpen,
|
||||
elementRef,
|
||||
setIsContextMenuOpen,
|
||||
setOnClickSymbolId,
|
||||
setContextMenuPosition
|
||||
);
|
||||
|
||||
// Render
|
||||
let isOpenClasses = '-right-64';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue