Skip to content

Commit

Permalink
fix(tile-select): prevent focus outline when tile select is disabled
Browse files Browse the repository at this point in the history
Adds `useEffect` to set `hasFocus` to false if `disabled` prop is set

fix #4227
  • Loading branch information
edleeks87 committed Jul 9, 2021
1 parent f2634ee commit e6c9a17
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/components/tile-select/tile-select.component.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PropTypes from "prop-types";
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import I18n from "i18n-js";
import styledSystemPropTypes from "@styled-system/prop-types";
import tagComponent from "../../utils/helpers/tags/tags";
Expand Down Expand Up @@ -68,6 +68,12 @@ const TileSelect = ({
);
};

useEffect(() => {
if (disabled && hasFocus) {
setHasFocus(false);
}
}, [disabled, hasFocus]);

return (
<StyledTileSelectContainer
checked={checked}
Expand Down
18 changes: 18 additions & 0 deletions src/components/tile-select/tile-select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,24 @@ describe("TileSelectGroup", () => {
});
});

describe("when input becomes disabled", () => {
it("removes the focus outline", () => {
wrapper = mount(<TileSelect />);

wrapper.find(StyledTileSelectInput).first().simulate("focus");
expect(wrapper.find(StyledFocusWrapper).first().prop("hasFocus")).toEqual(
true
);

wrapper.setProps({ disabled: true });
wrapper.update();

expect(wrapper.find(StyledFocusWrapper).first().prop("hasFocus")).toEqual(
false
);
});
});

describe("propTypes", () => {
it("validates the incorrect children prop", () => {
jest.spyOn(global.console, "error").mockImplementation(() => {});
Expand Down

0 comments on commit e6c9a17

Please sign in to comment.