Skip to content

Commit 00a3974

Browse files
committed
Update cleanup logic
1 parent 086be1d commit 00a3974

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

packages/eui/src/components/flyout/manager/flyout_managed.test.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ describe('EuiManagedFlyout', () => {
154154
expect(el).toHaveAttribute(PROPERTY_LEVEL, LEVEL_MAIN);
155155
});
156156

157-
it('calls the unregister callback prop when onClose', () => {
157+
it('calls closeAllFlyouts during cleanup when main flyout unmounts', () => {
158158
const onClose = jest.fn();
159159

160160
const { getByTestSubject, unmount } = renderInProvider(
@@ -174,6 +174,30 @@ describe('EuiManagedFlyout', () => {
174174
});
175175

176176
expect(mockCloseAllFlyouts).toHaveBeenCalled();
177+
expect(mockCloseFlyout).not.toHaveBeenCalled();
178+
});
179+
180+
it('calls closeFlyout during cleanup when child flyout unmounts', () => {
181+
const onClose = jest.fn();
182+
183+
const { getByTestSubject, unmount } = renderInProvider(
184+
<EuiManagedFlyout id="close-me" level={LEVEL_CHILD} onClose={onClose} />
185+
);
186+
187+
act(() => {
188+
userEvent.click(getByTestSubject('managed-flyout'));
189+
});
190+
191+
// The onClose should be called when the flyout is clicked
192+
expect(onClose).toHaveBeenCalled();
193+
194+
// The closeFlyout should be called when the component unmounts (cleanup)
195+
act(() => {
196+
unmount();
197+
});
198+
199+
expect(mockCloseFlyout).toHaveBeenCalledWith('close-me');
200+
expect(mockCloseAllFlyouts).not.toHaveBeenCalled();
177201
});
178202

179203
it('registers child flyout and sets data-level child', () => {

packages/eui/src/components/flyout/manager/flyout_managed.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,13 @@ export const EuiManagedFlyout = forwardRef<HTMLElement, EuiManagedFlyoutProps>(
195195
// Only call closeFlyout if it wasn't already called via onClose
196196
// This prevents duplicate removal when using Escape/X button
197197
if (flyoutExistsInManagerRef.current) {
198-
closeFlyout(flyoutId);
198+
level === LEVEL_MAIN ? closeAllFlyouts() : closeFlyout(flyoutId);
199199
}
200200

201201
// Reset navigation tracking when explicitly closed via isOpen=false
202202
wasRegisteredRef.current = false;
203203
};
204-
}, [flyoutId, title, level, size, addFlyout, closeFlyout]);
204+
}, [flyoutId, title, level, size, addFlyout, closeFlyout, closeAllFlyouts]);
205205

206206
// Detect when flyout has been removed from manager state (e.g., via Back button)
207207
// and trigger onClose callback to notify the parent component

0 commit comments

Comments
 (0)