Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(filterable-select): ensure list does not reopen when user clicks option and openOnFocus prop set #6476

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,34 @@ context("Tests for FilterableSelect component", () => {
selectListWrapper().should("be.visible");
});

it("should not reopen list when openOnFocus set and user selects an option via click", () => {
CypressMountWithProviders(
<stories.FilterableSelectComponent openOnFocus />
);

commonDataElementInputPreview().focus();
selectInput().should("have.attr", "aria-expanded", "true");
selectListWrapper().should("be.visible");
selectOption(positionOfElement("first")).click();
selectListWrapper().should("not.be.visible");
});

it("should open list when openOnFocus set, user selects an option via enter key and then input is blurred then focussed again", () => {
CypressMountWithProviders(
<stories.FilterableSelectComponent openOnFocus />
);

commonDataElementInputPreview().focus();
selectInput().should("have.attr", "aria-expanded", "true");
selectListWrapper().should("be.visible");
selectInput().realPress("ArrowDown");
selectInput().realPress("Enter");
selectListWrapper().should("not.be.visible");
commonDataElementInputPreview().blur();
commonDataElementInputPreview().focus();
selectListWrapper().should("be.visible");
});

it("should check list is open when input is clicked and openOnFocus is set", () => {
CypressMountWithProviders(
<stories.FilterableSelectComponent openOnFocus />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export const FilterableSelect = React.forwardRef(
label,
});
const focusTimer = useRef<null | ReturnType<typeof setTimeout>>(null);
const openOnFocusFlagBlock = useRef(false);

if (!deprecateInputRefWarnTriggered && inputRef) {
deprecateInputRefWarnTriggered = true;
Expand Down Expand Up @@ -478,12 +479,14 @@ export const FilterableSelect = React.forwardRef(
setActiveDescendantId(selectedOptionId);

if (selectionType !== "navigationKey") {
openOnFocusFlagBlock.current = !!openOnFocus;
setOpen(false);
textboxRef?.focus();
textboxRef?.select();
openOnFocusFlagBlock.current = false;
}
},
[textboxRef, triggerChange]
[textboxRef, triggerChange, openOnFocus]
);

const onSelectListClose = useCallback(() => {
Expand Down Expand Up @@ -521,6 +524,10 @@ export const FilterableSelect = React.forwardRef(
clearTimeout(focusTimer.current);
}

if (openOnFocusFlagBlock.current) {
return;
}

// we need to use a timeout here as there is a race condition when rendered in a modal
// whereby the select list isn't visible when the select is auto focused straight away
focusTimer.current = setTimeout(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function getSelect(props: Partial<FilterableSelectProps> = {}) {
);
}

function renderSelect(props = {}, renderer = mount) {
return renderer(getSelect(props));
function renderSelect(props = {}, renderer = mount, opts = {}) {
return renderer(getSelect(props), opts);
}

describe("FilterableSelect", () => {
Expand Down Expand Up @@ -1097,7 +1097,7 @@ describe("FilterableSelect", () => {

describe('when the "openOnFocus" prop is set', () => {
describe("and the Textbox Input is focused", () => {
it("the SelectList should be rendered", () => {
it("should render the SelectList", () => {
const wrapper = renderSelect({ openOnFocus: true });

act(() => {
Expand All @@ -1109,6 +1109,38 @@ describe("FilterableSelect", () => {
.forEach((option) => expect(option.getDOMNode()).toBeVisible());
});

it("should not reopen the SelectList when a user selects and Option by clicking", () => {
const container = document.createElement("div");
container.id = "enzymeContainer";
document.body.appendChild(container);

const wrapper = renderSelect({ openOnFocus: true }, mount, {
attachTo: document.getElementById("enzymeContainer"),
});

act(() => {
wrapper.find("input").simulate("focus");
jest.runOnlyPendingTimers();
});
wrapper
.find(Option)
.forEach((option) => expect(option.getDOMNode()).toBeVisible());
act(() => {
wrapper.find(SelectList).prop("onSelect")({
value: "opt1",
text: "red",
selectionType: "click",
selectionConfirmed: true,
});
});
wrapper
.update()
.find(Option)
.forEach((option) => expect(option.getDOMNode()).not.toBeVisible());

container?.parentNode?.removeChild(container);
});

describe.each(["readOnly", "disabled"])(
'with the "%s" prop passed',
(prop) => {
Expand Down
Loading