Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional dropdown triggers #2115

Merged
merged 5 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export interface DropdownProps {
closeOnSelect?: boolean;
closeOnOutsideClick?: boolean;
dropdownModifiers?: any[];
trigger?: "click" | "hover";
trigger?: "click" | "hover" | "all" | "manual";
strategy?: "absolute" | "fixed";
onClick?: () => void;
/** @deprecated Prop deprecated. Use `dropdownProps` prop instead*/
Expand Down
16 changes: 9 additions & 7 deletions src/components/Dropdown/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ const PLACEMENT = {
leftEnd: "left-end",
};

const TRIGGERS = { click: "click", hover: "mouseenter focus" };
const TRIGGERS = {
click: "click",
hover: "mouseenter focus",
all: "mouseenter focus click",
manual: "manual",
};

const hideOnEsc = {
name: "hideOnEsc",
Expand Down Expand Up @@ -121,7 +126,6 @@ const Dropdown = ({
maxWidth="none"
offset={0}
placement={position || PLACEMENT.bottomEnd}
plugins={plugins}
popperOptions={{ strategy, modifiers: dropdownModifiers }}
role="dropdown"
theme="light"
Expand Down Expand Up @@ -149,23 +153,21 @@ const Dropdown = ({
onClose();
setMounted(false);
}}
{...otherProps}
{...controlledProps}
{...{ plugins, ...otherProps, ...controlledProps }}
>
{customTarget ? (
<span onClick={onClick}>
<span {...{ onClick }}>
{typeof customTarget === "function" ? customTarget() : customTarget}
</span>
) : (
<Button
{...{ label, onClick }}
data-cy={`${hyphenize(label)}-dropdown-icon`}
disabled={disabled || buttonProps?.disabled}
icon={icon || Down}
iconPosition="right"
label={label}
size={size ?? buttonSize}
style={style ?? buttonStyle}
onClick={onClick}
{...buttonProps}
/>
)}
Expand Down
17 changes: 16 additions & 1 deletion tests/Dropdown.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe("Dropdown", () => {
it("should call onClose when Dropdown is closed", async () => {
const onClose = jest.fn();
const { getByText } = render(
<Dropdown closeOnOutsideClick label="Dropdown" onClose={onClose}>
<Dropdown {...{ onClose }} closeOnOutsideClick label="Dropdown">
{options}
</Dropdown>
);
Expand Down Expand Up @@ -188,4 +188,19 @@ describe("Dropdown", () => {
await userEvent.hover(getByText("Disabled button"));
expect(getByText("Disabled button's tooltip")).toBeInTheDocument();
});

it("should open menu on click and hover when trigger is all", async () => {
const { findByText } = render(
<Dropdown closeOnOutsideClick label="Dropdown" trigger="all">
{options}
</Dropdown>
);
await userEvent.hover(await findByText("Dropdown"));
expect(await findByText("option 1")).toBeInTheDocument();
await userEvent.click(document.body);

await userEvent.click(await findByText("Dropdown"));
expect(await findByText("option 1")).toBeInTheDocument();
await userEvent.click(document.body);
});
});
Loading