Skip to content

Commit

Permalink
fix(form): TextField and Select disabled behavior
Browse files Browse the repository at this point in the history
The Select component will no longer open the Listbox if it somehow gains
focus while disabled (_should_ have been impossible though) and the
TextField now correctly passes the disabled attribute to the input
field.
  • Loading branch information
mlaursen committed Jul 7, 2020
1 parent de6b56d commit e8f2c57
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/form/src/select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ const Select = forwardRef<HTMLDivElement, SelectProps>(function Select(
onKeyDown(event);
}

if (disabled) {
return;
}

switch (event.key) {
case " ":
case "ArrowUp":
Expand Down Expand Up @@ -296,7 +300,7 @@ const Select = forwardRef<HTMLDivElement, SelectProps>(function Select(
// no default
}
},
[onKeyDown, show]
[onKeyDown, disabled, show]
);

const selectRef = useRef<HTMLDivElement | null>(null);
Expand Down
19 changes: 19 additions & 0 deletions packages/form/src/select/__tests__/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,23 @@ describe("Select", () => {

expect(input.value).toBe("Option 1");
});

it("should not open the listbox while disabled", () => {
render(<Select {...PROPS} disabled />);
expect(getListbox).toThrow();
const select = getSelect();

fireEvent.click(select);
expect(getListbox).toThrow();

fireEvent.focus(select);
fireEvent.keyDown(select, { key: " " });
expect(getListbox).toThrow();

fireEvent.keyDown(select, { key: "ArrowDown" });
expect(getListbox).toThrow();

fireEvent.keyDown(select, { key: "ArrowUp" });
expect(getListbox).toThrow();
});
});
1 change: 1 addition & 0 deletions packages/form/src/text-field/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ const TextField = forwardRef<HTMLInputElement, TextFieldProps>(
{...props}
ref={ref}
type={type}
disabled={disabled}
onFocus={onFocus}
onBlur={onBlur}
onChange={onChange}
Expand Down
6 changes: 6 additions & 0 deletions packages/form/src/text-field/__tests__/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ describe("TextField", () => {

rerender(<TextField {...props} label="Label" placeholder="Placeholder" />);
expect(container).toMatchSnapshot();

rerender(
<TextField {...props} label="Label" placeholder="Placeholder" disabled />
);
expect(container).toMatchSnapshot();
expect(document.getElementById("field")).toHaveAttribute("disabled");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,25 @@ exports[`TextField should render correctly 2`] = `
</div>
</div>
`;

exports[`TextField should render correctly 3`] = `
<div>
<div
class="rmd-text-field-container rmd-text-field-container--outline rmd-text-field-container--disabled rmd-text-field-container--label"
>
<label
class="rmd-label rmd-label--disabled rmd-floating-label"
for="field"
>
Label
</label>
<input
class="rmd-text-field rmd-text-field--floating"
disabled=""
id="field"
placeholder="Placeholder"
type="text"
/>
</div>
</div>
`;

0 comments on commit e8f2c57

Please sign in to comment.