Skip to content

Commit

Permalink
Fixed ComboBox and Dropdown calling onBlur when an option in the list…
Browse files Browse the repository at this point in the history
… is clicked (#4012)
  • Loading branch information
joshwooding authored Aug 20, 2024
1 parent 16717fc commit 3ff4448
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/smooth-candles-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@salt-ds/core": patch
---

- Fixed ComboBox calling onBlur when an option in the list is clicked.
- Fixed Dropdown calling onBlur when an option in the list is clicked.
7 changes: 7 additions & 0 deletions packages/core/src/__tests__/__e2e__/combo-box/ComboBox.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -723,4 +723,11 @@ describe("Given a ComboBox", () => {
);
cy.findByRole("combobox").should("have.value", "Alaska");
});
it("should not call onBlur when an option in the list is clicked", () => {
const blurSpy = cy.stub().as("blurSpy");
cy.mount(<Default onBlur={blurSpy} />);
cy.findByRole("combobox").realClick();
cy.findAllByRole("option").eq(0).realClick();
cy.get("@blurSpy").should("not.have.been.called");
});
});
8 changes: 8 additions & 0 deletions packages/core/src/__tests__/__e2e__/dropdown/Dropdown.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,12 @@ describe("Given a Dropdown", () => {

cy.findByTestId(FLOATING_TEST_ID).should("exist");
});

it("should not call onBlur when an option in the list is clicked", () => {
const blurSpy = cy.stub().as("blurSpy");
cy.mount(<Default onBlur={blurSpy} />);
cy.findByRole("combobox").realClick();
cy.findAllByRole("option").eq(0).realClick();
cy.get("@blurSpy").should("not.have.been.called");
});
});
4 changes: 3 additions & 1 deletion packages/core/src/combo-box/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,9 @@ export const ComboBox = forwardRef(function ComboBox<Item>(

const handleBlur = (event: FocusEvent<HTMLInputElement>) => {
event.persist();
onBlur?.(event);
if (!listRef.current || !listRef.current.contains(event.relatedTarget)) {
onBlur?.(event);
}
};

const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,9 @@ export const Dropdown = forwardRef(function Dropdown<Item>(

const handleBlur = (event: FocusEvent<HTMLButtonElement>) => {
setFocusedState(false);
onBlur?.(event);
if (!listRef.current || !listRef.current.contains(event.relatedTarget)) {
onBlur?.(event);
}
};

const handleListMouseOver = () => {
Expand Down

0 comments on commit 3ff4448

Please sign in to comment.