Skip to content

Commit

Permalink
close the Dropdown menu when pressing the split button
Browse files Browse the repository at this point in the history
  • Loading branch information
moathabuhamad-cengage committed Jan 24, 2025
1 parent bc8b97e commit 1209baa
Showing 1 changed file with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const DropdownSplitButton = React.forwardRef<
} = resolvedProps;

const ref = useForkedRef(forwardedRef, resolvedContext.toggleRef);
const splitButtonRef = React.useRef<HTMLButtonElement>(null);

resolvedContext.dropdownButtonId.current = useGenerateId(id);

Expand All @@ -70,12 +71,12 @@ export const DropdownSplitButton = React.forwardRef<
aria-hidden="true"
/>
) : (
<ArrowDropDownIcon
size={theme.iconSizes.medium}
testId="caretDown"
aria-hidden="true"
/>
);
<ArrowDropDownIcon
size={theme.iconSizes.medium}
testId="caretDown"
aria-hidden="true"
/>
);

function handleClick(event: React.SyntheticEvent) {
if (resolvedContext.isOpen) {
Expand All @@ -85,6 +86,15 @@ export const DropdownSplitButton = React.forwardRef<
}
}

function handleButtonClick(event: React.SyntheticEvent) {
onClick?.();

if (resolvedContext.isOpen) {
resolvedContext.closeDropdown(event);
splitButtonRef.current?.focus();
}
}

const i18n = React.useContext(I18nContext);

function buildIconButtonStyles(props) {
Expand All @@ -103,11 +113,12 @@ export const DropdownSplitButton = React.forwardRef<
{...other}
id={resolvedContext.dropdownButtonId.current}
isInverse={resolvedContext.isInverse}
onClick={onClick}
onClick={handleButtonClick}
shape={ButtonShape.leftCap}
style={{ borderRight: 0, marginRight: 0 }}
variant={variant}
tabIndex={0}
ref={splitButtonRef}
>
{children}
</Button>
Expand Down

0 comments on commit 1209baa

Please sign in to comment.