Skip to content

Commit a0af6e7

Browse files
author
Katie George
committed
PR comments
1 parent 89f6169 commit a0af6e7

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

src/internal/events/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
// SPDX-License-Identifier: Apache-2.0
33
export type CancelableEventHandler<Detail = object> = (event: CustomEvent<Detail>) => void;
44

5+
// eslint-disable-next-line @typescript-eslint/ban-types
6+
export type NonCancelableEventHandler<Detail = {}> = (event: NonCancelableCustomEvent<Detail>) => void;
7+
8+
export type NonCancelableCustomEvent<DetailType> = Omit<CustomEvent<DetailType>, "preventDefault">;
9+
510
export interface ClickDetail {
611
button: number;
712
ctrlKey: boolean;
@@ -49,3 +54,11 @@ export function fireCancelableEvent<T>(
4954
}
5055
return event.defaultPrevented;
5156
}
57+
58+
export function fireNonCancelableEvent<T = null>(handler: NonCancelableEventHandler<T> | undefined, detail?: T) {
59+
if (!handler) {
60+
return;
61+
}
62+
const event = createCustomEvent({ cancelable: false, detail });
63+
handler(event);
64+
}

src/support-prompt-group/focus-helpers.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ export function getNextFocusTarget(
1010
const buttons: HTMLButtonElement[] = Array.from(
1111
containerObjectRef?.current.querySelectorAll(`.${styles["support-prompt"]}`),
1212
);
13-
return buttons.find((button) => button.dataset.itemid === focusedIdRef.current) ?? buttons[0] ?? null;
13+
14+
if (focusedIdRef.current) {
15+
const buttonWithId = buttons.find((button) => button.dataset.itemid === focusedIdRef.current);
16+
if (buttonWithId) {
17+
return buttonWithId;
18+
}
19+
}
20+
21+
return buttons[0] ?? null;
1422
}
1523
return null;
1624
}

src/support-prompt-group/interfaces.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { CancelableEventHandler, ClickDetail as _ClickDetail } from "../internal/events";
4+
import { ClickDetail as _ClickDetail, NonCancelableEventHandler } from "../internal/events";
55

66
export interface SupportPromptGroupProps {
77
/**
@@ -20,7 +20,7 @@ export interface SupportPromptGroupProps {
2020
/**
2121
* Called when the user clicks on a support prompt. The event detail object contains the ID of the clicked item.
2222
*/
23-
onItemClick: CancelableEventHandler<SupportPromptGroupProps.ItemClickDetail>;
23+
onItemClick: NonCancelableEventHandler<SupportPromptGroupProps.ItemClickDetail>;
2424

2525
/**
2626
* Adds an aria label to the support prompt group.

src/support-prompt-group/internal.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515

1616
import { getDataAttributes } from "../internal/base-component/get-data-attributes";
1717
import { InternalBaseComponentProps } from "../internal/base-component/use-base-component";
18-
import { fireCancelableEvent } from "../internal/events";
18+
import { fireNonCancelableEvent } from "../internal/events";
1919
import { useMergeRefs } from "../internal/utils/use-merge-refs";
2020
import { getNextFocusTarget, onUnregisterActive } from "./focus-helpers";
2121
import { SupportPromptGroupProps } from "./interfaces";
@@ -54,7 +54,7 @@ export const InternalSupportPromptGroup = forwardRef(
5454
const handleClick = (event: React.MouseEvent, id: string) => {
5555
const { altKey, button, ctrlKey, metaKey, shiftKey } = event;
5656

57-
fireCancelableEvent(onItemClick, { id, altKey, button, ctrlKey, metaKey, shiftKey }, event);
57+
fireNonCancelableEvent(onItemClick, { id, altKey, button, ctrlKey, metaKey, shiftKey }, event);
5858
};
5959

6060
useEffect(() => {

0 commit comments

Comments
 (0)