Skip to content

Commit

Permalink
Fix Popover cannot be closed by changing isOpen (#3486)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Huang <[email protected]>
  • Loading branch information
jasonhuang-sky and Jason Huang authored Jun 6, 2024
1 parent f2c776a commit c15eb9f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/bpk-component-popover/src/BpkPopover-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,29 @@ describe('BpkPopover', () => {
expect(screen.getByText('My popover content')).toBeVisible();
});

it('should rerender correctly with "isOpen" provided', () => {
const props = {
id: 'my-popover',
onClose: () => null,
label: 'My popover',
closeButtonLabel: 'Close',
target: <button type="button">My target</button>,
};
const { rerender } = render(
<BpkPopover {...props} isOpen>
My popover content
</BpkPopover>,
);

rerender(
<BpkPopover {...props} isOpen={false}>
My popover content
</BpkPopover>,
);

expect(screen.queryByRole('My popover content')).not.toBeInTheDocument();
});

it('should render without an arrow', () => {
const target = (<button type="button">My target</button>);
render(
Expand Down
9 changes: 8 additions & 1 deletion packages/bpk-component-popover/src/BpkPopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

import type { SyntheticEvent, ReactNode, ReactElement } from 'react';
import { useState, useRef, cloneElement, isValidElement } from 'react';
import { useState, useEffect, useRef, cloneElement, isValidElement } from 'react';

import {
useFloating,
Expand Down Expand Up @@ -125,6 +125,13 @@ const BpkPopover = ({
...rest
}: Props) => {
const [isOpenState, setIsOpenState] = useState(isOpen);

useEffect(() => {
if (!isOpen) {
setIsOpenState(false)
}
}, [isOpen]);

const arrowRef = useRef(null);

const { context, floatingStyles, refs } = useFloating({
Expand Down

0 comments on commit c15eb9f

Please sign in to comment.