feat(control): add optional callbacks

This commit is contained in:
Ivan S
2020-09-22 14:17:34 -04:00
parent aca7080f68
commit ab92766af5
2 changed files with 43 additions and 5 deletions
+4
View File
@@ -458,6 +458,10 @@ const FlowWithControls = () => (
- `showZoom`: boolean - default: true - `showZoom`: boolean - default: true
- `showFitView`: boolean - default: true - `showFitView`: boolean - default: true
- `showInteractive`: boolean - default: true - `showInteractive`: boolean - default: true
- `onZoomIn`: function that gets triggered when the zoom in button is pressed
- `onZoomOut`: function that gets triggered when the zoom out button is pressed
- `onFitView`: function that gets triggered when the fit-to-view button is pressed
- `onInteractiveChange`: function that gets triggered when the lock button is pressed - passes the new value
- `style`: css properties - `style`: css properties
- `className`: additional class name - `className`: additional class name
+39 -5
View File
@@ -15,9 +15,23 @@ interface ControlProps extends React.HTMLAttributes<HTMLDivElement> {
showZoom?: boolean; showZoom?: boolean;
showFitView?: boolean; showFitView?: boolean;
showInteractive?: boolean; showInteractive?: boolean;
onZoomIn?: () => void;
onZoomOut?: () => void;
onFitView?: () => void;
onInteractiveChange?: (interactiveStatus: boolean) => void;
} }
const Controls = ({ style, showZoom = true, showFitView = true, showInteractive = true, className }: ControlProps) => { const Controls = ({
style,
showZoom = true,
showFitView = true,
showInteractive = true,
onZoomIn,
onZoomOut,
onFitView,
onInteractiveChange,
className,
}: ControlProps) => {
const setInteractive = useStoreActions((actions) => actions.setInteractive); const setInteractive = useStoreActions((actions) => actions.setInteractive);
const fitView = useStoreActions((actions) => actions.fitView); const fitView = useStoreActions((actions) => actions.fitView);
const zoomIn = useStoreActions((actions) => actions.zoomIn); const zoomIn = useStoreActions((actions) => actions.zoomIn);
@@ -30,10 +44,20 @@ const Controls = ({ style, showZoom = true, showFitView = true, showInteractive
<div className={mapClasses} style={style}> <div className={mapClasses} style={style}>
{showZoom && ( {showZoom && (
<> <>
<div className="react-flow__controls-button react-flow__controls-zoomin" onClick={() => zoomIn()}> <div className="react-flow__controls-button react-flow__controls-zoomin" onClick={() => {
zoomIn();
if (onZoomIn) {
onZoomIn();
}
}}>
<PlusIcon /> <PlusIcon />
</div> </div>
<div className="react-flow__controls-button react-flow__controls-zoomout" onClick={() => zoomOut()}> <div className="react-flow__controls-button react-flow__controls-zoomout" onClick={() => {
zoomOut();
if (onZoomOut) {
onZoomOut();
}
}}>
<MinusIcon /> <MinusIcon />
</div> </div>
</> </>
@@ -41,7 +65,12 @@ const Controls = ({ style, showZoom = true, showFitView = true, showInteractive
{showFitView && ( {showFitView && (
<div <div
className="react-flow__controls-button react-flow__controls-fitview" className="react-flow__controls-button react-flow__controls-fitview"
onClick={() => fitView({ padding: 0.1 })} onClick={() => {
fitView({ padding: 0.1 });
if (onFitView) {
onFitView();
}
}}
> >
<FitviewIcon /> <FitviewIcon />
</div> </div>
@@ -49,7 +78,12 @@ const Controls = ({ style, showZoom = true, showFitView = true, showInteractive
{showInteractive && ( {showInteractive && (
<div <div
className="react-flow__controls-button react-flow__controls-interactive" className="react-flow__controls-button react-flow__controls-interactive"
onClick={() => setInteractive(!isInteractive)} onClick={() => {
setInteractive(!isInteractive);
if (onInteractiveChange) {
onInteractiveChange(!isInteractive);
}
}}
> >
{isInteractive ? <UnlockIcon /> : <LockIcon />} {isInteractive ? <UnlockIcon /> : <LockIcon />}
</div> </div>