Dropdown: Fixes double call to onVisibilityChange (#87607)

* Dropdown: Fixes double call to onVisibilityChange

* Update

* update
This commit is contained in:
Torkel Ödegaard
2024-05-10 14:06:53 +02:00
committed by GitHub
parent ad78dec7c3
commit 459673b539

View File

@@ -10,7 +10,7 @@ import {
useFloating,
useInteractions,
} from '@floating-ui/react';
import React, { useEffect, useRef, useState } from 'react';
import React, { useCallback, useRef, useState } from 'react';
import { CSSTransition } from 'react-transition-group';
import { GrafanaTheme2 } from '@grafana/data';
@@ -34,9 +34,13 @@ export const Dropdown = React.memo(({ children, overlay, placement, offset, onVi
const [show, setShow] = useState(false);
const transitionRef = useRef(null);
useEffect(() => {
onVisibleChange?.(show);
}, [onVisibleChange, show]);
const handleOpenChange = useCallback(
(newState: boolean) => {
setShow(newState);
onVisibleChange?.(newState);
},
[onVisibleChange]
);
// the order of middleware is important!
const middleware = [
@@ -56,7 +60,7 @@ export const Dropdown = React.memo(({ children, overlay, placement, offset, onVi
const { context, refs, floatingStyles } = useFloating({
open: show,
placement: getPlacement(placement),
onOpenChange: setShow,
onOpenChange: handleOpenChange,
middleware,
whileElementsMounted: autoUpdate,
});