Skip to content

Commit

Permalink
Fix focus ring doesn't show up when Dropdown is wrapped in Form Field (
Browse files Browse the repository at this point in the history
  • Loading branch information
origami-z authored Apr 6, 2023
1 parent 8d29c01 commit ae3c837
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-ducks-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@salt-ds/lab": patch
---

Fix focus ring doesn't show up when Dropdown is wrapped in Form Field
17 changes: 17 additions & 0 deletions packages/lab/src/__tests__/__e2e__/dropdown/Dropdown.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Dropdown, FormField } from "@salt-ds/lab";

const testSource = ["Bar", "Foo", "Foo Bar", "Baz"];

describe("GIVEN a Dropdown component", () => {
describe("WHEN the Dropdown is rendered within FormField", () => {
it("THEN it should show focus ring around form field when focused", () => {
cy.mount(
<FormField label="Dropdown" id="dropdown-in-form-field">
<Dropdown source={testSource} />
</FormField>
);
cy.findByLabelText("Dropdown").focus();
cy.get(".saltFormField").should("have.class", "saltFormField-focused");
});
});
});
30 changes: 20 additions & 10 deletions packages/lab/src/dropdown/useDropdownBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ export const useDropdownBase = ({

const {
inFormField,
// onFocus: formFieldOnFocus,
// onBlur: formFieldOnBlur,
setFocused: setFormFieldFocused,
a11yProps: { "aria-labelledby": ariaLabelledBy, ...restA11yProps } = {},
} = useFormFieldProps();

Expand All @@ -65,13 +64,23 @@ export const useDropdownBase = ({
});

const handleTriggerFocus = useCallback(() => {
setIsOpen(true);
onOpenChange?.(true);
// Suppress response to click if click was the cause of focus
justFocused.current = window.setTimeout(() => {
justFocused.current = null;
}, 1000);
}, [onOpenChange, setIsOpen]);
if (!disabled) {
setFormFieldFocused?.(true);

if (openOnFocus) {
setIsOpen(true);
onOpenChange?.(true);
// Suppress response to click if click was the cause of focus
justFocused.current = window.setTimeout(() => {
justFocused.current = null;
}, 1000);
}
}
}, [disabled, onOpenChange, openOnFocus, setFormFieldFocused, setIsOpen]);

const handleTriggerBlur = useCallback(() => {
setFormFieldFocused?.(false);
}, [setFormFieldFocused]);

const handleTriggerToggle = useCallback(
(e: MouseEvent) => {
Expand Down Expand Up @@ -132,7 +141,8 @@ export const useDropdownBase = ({
"aria-owns": isOpen ? componentId : undefined,
id: `${id}-control`,
onClick: disabled || openOnFocus ? undefined : handleTriggerToggle,
onFocus: openOnFocus && !disabled ? handleTriggerFocus : undefined,
onFocus: handleTriggerFocus,
onBlur: handleTriggerBlur,
role: "listbox",
onKeyDown: disabled ? undefined : handleKeydown,
style: { width: fullWidth ? undefined : width },
Expand Down

0 comments on commit ae3c837

Please sign in to comment.