Skip to content

Commit

Permalink
fix(Dropdown): disable Apply button when no changes are made
Browse files Browse the repository at this point in the history
  • Loading branch information
zettca authored and plagoa committed Oct 30, 2023
1 parent d24b610 commit 43bad71
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
31 changes: 29 additions & 2 deletions packages/core/src/components/Dropdown/Dropdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ describe("Dropdown", () => {
const DropdownHeader = screen.getByRole("combobox");

expect(DropdownHeader).toHaveAttribute("aria-expanded", "false");

expect(DropdownHeader).toHaveAttribute("aria-expanded", "false");
});

it("should be invalid", async () => {
Expand Down Expand Up @@ -132,4 +130,33 @@ describe("Dropdown", () => {

expect(DropdownHeader).toHaveAttribute("aria-expanded", "false");
});

it("has Apply button disabled when no changes are made", async () => {
render(
<HvDropdown
aria-label="Main sample"
multiSelect
values={[
{ label: "value 1" },
{ label: "value 2", selected: true },
{ label: "value 3" },
{ label: "value 4" },
]}
/>
);

await userEvent.click(screen.getByRole("combobox"));

const getApplyButton = () => screen.getByRole("button", { name: "Apply" });
const getCheckBox = () => screen.getByRole("checkbox", { name: "value 1" });

expect(getApplyButton()).toBeDisabled();

await userEvent.click(getCheckBox());
expect(getCheckBox()).toBeChecked();
expect(getApplyButton()).toBeEnabled();

await userEvent.click(getCheckBox());
expect(getApplyButton()).toBeDisabled();
});
});
11 changes: 10 additions & 1 deletion packages/core/src/components/Dropdown/List/List.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MouseEvent, useContext, useEffect, useState } from "react";
import { MouseEvent, useContext, useEffect, useMemo, useState } from "react";

import { theme } from "@hitachivantara/uikit-styles";

Expand Down Expand Up @@ -110,6 +110,10 @@ const cleanHidden = (lst: HvListValue[]) =>
const valuesExist = (values: HvListValue[]) =>
values != null && values?.length > 0;

/** Filter selected ordered element `id`s (or `label`) */
const getSelectedIds = (list: HvListValue[]) =>
getSelected(list).map((item) => item.id || item.label);

export const HvDropdownList = (props: HvDropdownListProps) => {
const {
id,
Expand All @@ -136,6 +140,10 @@ export const HvDropdownList = (props: HvDropdownListProps) => {
const [anySelected, setAnySelected] = useState<boolean>(false);
const { width, height } = useContext(BaseDropdownContext);

const hasChanges = useMemo(() => {
return String(getSelectedIds(values)) !== String(getSelectedIds(list));
}, [list, values]);

const newLabels = {
selectAll: labels?.selectAll,
selectionConjunction: labels?.multiSelectionConjunction,
Expand Down Expand Up @@ -307,6 +315,7 @@ export const HvDropdownList = (props: HvDropdownListProps) => {
<HvActionBar id={setId(id, "actions")}>
<HvButton
id={setId(id, "actions-apply")}
disabled={!hasChanges}
onClick={() => onChange(cleanHidden(list), true, true, true)}
variant="primaryGhost"
>
Expand Down

0 comments on commit 43bad71

Please sign in to comment.