Skip to content

Commit e1dd299

Browse files
authored
Merge branch 'main' into feat/widgetize-app-layout-skeleton-event-base
2 parents 8f70504 + f40e0fc commit e1dd299

File tree

6 files changed

+42
-20
lines changed

6 files changed

+42
-20
lines changed

src/__tests__/snapshot-tests/__snapshots__/documenter.test.ts.snap

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13556,7 +13556,13 @@ exports[`Documenter definition for popover matches the snapshot: popover 1`] = `
1355613556
"events": [],
1355713557
"functions": [
1355813558
{
13559-
"description": "Sets focus on the popover's trigger.",
13559+
"description": "Dismisses the popover without focusing the trigger. Use only if an element other than the trigger needs to be focused after dismissing the popover.",
13560+
"name": "dismiss",
13561+
"parameters": [],
13562+
"returnType": "void",
13563+
},
13564+
{
13565+
"description": "Sets focus on the popover's trigger and dismisses the popover if open.",
1356013566
"name": "focus",
1356113567
"parameters": [],
1356213568
"returnType": "void",

src/popover/__tests__/popover.test.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,25 @@ describe('ref support', () => {
393393
const wrapper = renderPopover({ children: 'Trigger', content: 'Popover', ref });
394394
wrapper.findTrigger().getElement().click();
395395
expect(wrapper.findContent()?.getElement()).toBeInTheDocument();
396-
ref.current?.focus();
396+
act(() => {
397+
ref.current?.focus();
398+
});
397399
expect(wrapper.findContent()?.getElement()).toBeFalsy();
400+
expect(document.activeElement).toBe(wrapper.findTrigger().getElement());
401+
});
402+
403+
test('Calling dismiss should close the popover', () => {
404+
const ref: React.RefObject<PopoverProps.Ref> = { current: null };
405+
const wrapper = renderPopover({ children: 'Trigger', content: 'Popover', ref });
406+
407+
wrapper.findTrigger().click();
408+
expect(wrapper.findBody()).toBeTruthy();
409+
410+
act(() => {
411+
ref.current?.dismiss();
412+
});
413+
expect(wrapper.findBody()).toBeNull();
414+
expect(document.activeElement).toBe(document.body);
398415
});
399416
});
400417

src/popover/index.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
import React, { useRef } from 'react';
3+
import React from 'react';
44

55
import { warnOnce } from '@cloudscape-design/component-toolkit/internal';
66

7-
import useForwardFocus from '../internal/hooks/forward-focus';
87
import useBaseComponent from '../internal/hooks/use-base-component';
98
import { isDevelopment } from '../internal/is-development';
109
import { applyDisplayName } from '../internal/utils/apply-display-name';
1110
import { getExternalProps } from '../internal/utils/external-props';
1211
import { PopoverProps } from './interfaces';
13-
import InternalPopover, { InternalPopoverRef } from './internal';
12+
import InternalPopover from './internal';
1413

1514
export { PopoverProps };
1615

@@ -40,12 +39,9 @@ const Popover = React.forwardRef(
4039
});
4140
const externalProps = getExternalProps(rest);
4241

43-
const internalRef = useRef<InternalPopoverRef | null>(null);
44-
useForwardFocus(ref, internalRef);
45-
4642
return (
4743
<InternalPopover
48-
ref={internalRef}
44+
ref={ref}
4945
header={header}
5046
position={position}
5147
size={size}

src/popover/interfaces.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,13 @@ export namespace PopoverProps {
119119

120120
export interface Ref {
121121
/**
122-
* Sets focus on the popover's trigger.
122+
* Sets focus on the popover's trigger and dismisses the popover if open.
123123
*/
124124
focus(): void;
125+
126+
/**
127+
* Dismisses the popover without focusing the trigger. Use only if an element other than the trigger needs to be focused after dismissing the popover.
128+
*/
129+
dismiss(): void;
125130
}
126131
}

src/popover/internal.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ export interface InternalPopoverProps extends Omit<PopoverProps, 'triggerType' |
3030
isInline?: boolean;
3131
}
3232

33-
export interface InternalPopoverRef {
34-
dismissPopover: () => void;
35-
focus: HTMLElement['focus'];
36-
}
37-
3833
export default React.forwardRef(InternalPopover);
3934

4035
function InternalPopover(
@@ -60,7 +55,7 @@ function InternalPopover(
6055

6156
...restProps
6257
}: InternalPopoverProps,
63-
ref: React.Ref<InternalPopoverRef>
58+
ref: React.Ref<PopoverProps.Ref>
6459
) {
6560
const baseProps = getBaseProps(restProps);
6661
const triggerRef = useRef<HTMLElement | null>(null);
@@ -105,7 +100,9 @@ function InternalPopover(
105100
);
106101

107102
useImperativeHandle(ref, () => ({
108-
dismissPopover: onDismiss,
103+
dismiss: () => {
104+
setVisible(false);
105+
},
109106
focus: () => {
110107
setVisible(false);
111108
focusTrigger();

src/property-filter/filtering-token/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212

1313
import InternalIcon from '../../icon/internal';
1414
import { useListFocusController } from '../../internal/hooks/use-list-focus-controller';
15-
import InternalPopover, { InternalPopoverProps, InternalPopoverRef } from '../../popover/internal';
15+
import { PopoverProps } from '../../popover/interfaces';
16+
import InternalPopover, { InternalPopoverProps } from '../../popover/internal';
1617
import InternalSelect from '../../select/internal';
1718
import { GeneratedAnalyticsMetadataPropertyEditStart } from '../analytics-metadata/interfaces';
1819

@@ -96,7 +97,7 @@ const FilteringToken = forwardRef(
9697
fallbackSelector: `.${styles.root}`,
9798
});
9899

99-
const popoverRef = useRef<InternalPopoverRef>(null);
100+
const popoverRef = useRef<PopoverProps.Ref>(null);
100101
const popoverProps: InternalPopoverProps = {
101102
content: editorContent,
102103
triggerType: 'text',
@@ -108,7 +109,7 @@ const FilteringToken = forwardRef(
108109
__onOpen: onEditorOpen,
109110
__closeAnalyticsAction: 'editClose',
110111
};
111-
useImperativeHandle(ref, () => ({ closeEditor: () => popoverRef.current?.dismissPopover() }));
112+
useImperativeHandle(ref, () => ({ closeEditor: () => popoverRef.current?.focus() }));
112113

113114
return (
114115
<TokenGroup

0 commit comments

Comments
 (0)