Skip to content

Commit 5b331f1

Browse files
authored
[ch-combo-box-render] Fix for focus not returning to the ch-combo-box-render when closing it with the Escape, Enter, or Tab keys (#547)
1 parent 6372972 commit 5b331f1

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

src/components/combo-box/combo-box.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ export class ChComboBoxRender
118118
#captionToItemInfo: Map<string, ComboBoxItemModelExtended> = new Map();
119119
#itemImages: Map<string, ComboBoxItemImagesModel> | undefined;
120120

121+
#shouldFocusTheComboBox = false;
122+
121123
// Filters info
122124
#applyFilters = false;
123125
#queuedInputValueUpdate: NodeJS.Timeout;
@@ -223,7 +225,7 @@ export class ChComboBoxRender
223225
Enter: () => {
224226
// The focus must return to the Host when closing the popover
225227
if (this.expanded) {
226-
this.el.focus();
228+
this.#shouldFocusTheComboBox = true;
227229
}
228230

229231
this.expanded = !this.expanded;
@@ -242,7 +244,7 @@ export class ChComboBoxRender
242244
if (this.expanded) {
243245
event.preventDefault();
244246

245-
this.el.focus();
247+
this.#shouldFocusTheComboBox = true;
246248
this.expanded = false;
247249
}
248250
}
@@ -562,7 +564,7 @@ export class ChComboBoxRender
562564

563565
// The focus must return to the Host when tabbing with the popover
564566
// expanded
565-
this.el.focus();
567+
this.#shouldFocusTheComboBox = true;
566568
event.preventDefault();
567569

568570
// "Traditional selection". A value was selected pressing the enter key
@@ -666,7 +668,7 @@ export class ChComboBoxRender
666668
// Return the focus to the control if the popover was closed with the
667669
// escape key or by clicking again the combo-box
668670
if (focusComposedPath().includes(this.el)) {
669-
this.el.focus();
671+
this.#shouldFocusTheComboBox = true;
670672
}
671673

672674
if (this.suggest) {
@@ -866,6 +868,11 @@ export class ChComboBoxRender
866868
});
867869
}
868870
}
871+
872+
if (this.#shouldFocusTheComboBox) {
873+
this.#shouldFocusTheComboBox = false;
874+
this.el.focus();
875+
}
869876
}
870877

871878
render() {

src/components/combo-box/tests/opening.e2e.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ const COMBO_BOX_KEYS_TO_CLOSE: KeyToPress[] = [
7575

7676
const COMBO_BOX_KEYS_TO_CLOSE_AND_RETURN_FOCUS: KeyToPress[] = [
7777
// TODO: Fix these tests
78-
// "Enter",
79-
// "Escape"
80-
// "NumpadEnter",
78+
"Enter",
79+
"Escape",
80+
"Tab"
81+
// "NumpadEnter"
8182
];
8283

8384
const STRICT_FILTERS: ComboBoxSuggestOptions = { strict: true };
@@ -87,7 +88,8 @@ const testOpeningClosing = (suggest: boolean, strict?: true) => {
8788
suggest ? "suggest" : "combo-box"
8889
}]${strict ? "[strict]" : ""}`;
8990

90-
const focusableElementTagName = suggest ? "input" : "div";
91+
// TODO: Fix this. The focusable element should not change
92+
// const focusableElementTagName = suggest ? "input" : "div";
9193

9294
describe(testDescription, () => {
9395
let page: E2EPage;
@@ -103,8 +105,6 @@ const testOpeningClosing = (suggest: boolean, strict?: true) => {
103105

104106
// TODO: Add Mouse click in the test
105107
const keysToClose: KeyToPress[] = COMBO_BOX_KEYS_TO_CLOSE;
106-
const keysToCloseAndReturnFocus: KeyToPress[] =
107-
COMBO_BOX_KEYS_TO_CLOSE_AND_RETURN_FOCUS;
108108

109109
const checkPopoverDefined = async (shouldBeDefined: boolean) => {
110110
const popoverRef = await page.find("ch-combo-box-render >>> ch-popover");
@@ -204,23 +204,21 @@ const testOpeningClosing = (suggest: boolean, strict?: true) => {
204204
});
205205
});
206206

207-
keysToCloseAndReturnFocus.forEach(key => {
207+
COMBO_BOX_KEYS_TO_CLOSE_AND_RETURN_FOCUS.forEach(key => {
208208
it(`should close the popover when pressing KEY = "${key}" and return the focus to the combo-box`, async () => {
209209
await page.click("ch-combo-box-render");
210210
await page.waitForChanges();
211211
await pressKey(key);
212212
await checkPopoverDefined(false);
213213

214214
const focusedElementTagName = await page.evaluate(() =>
215-
document
216-
.querySelector("ch-combo-box-render")
217-
.shadowRoot.activeElement?.tagName.toLowerCase()
215+
document.activeElement.tagName.toLowerCase()
218216
);
219-
expect(focusedElementTagName).toBe(focusableElementTagName);
217+
expect(focusedElementTagName).toBe("ch-combo-box-render");
220218
});
221219
});
222220

223-
keysToCloseAndReturnFocus.forEach(key => {
221+
COMBO_BOX_KEYS_TO_CLOSE_AND_RETURN_FOCUS.forEach(key => {
224222
it(`should close the popover when pressing KEY = "${key}" and return the focus to the combo-box even if the selection is updated`, async () => {
225223
await page.click("ch-combo-box-render");
226224
await page.waitForChanges();
@@ -229,11 +227,9 @@ const testOpeningClosing = (suggest: boolean, strict?: true) => {
229227
await checkPopoverDefined(false);
230228

231229
const focusedElementTagName = await page.evaluate(() =>
232-
document
233-
.querySelector("ch-combo-box-render")
234-
.shadowRoot.activeElement?.tagName.toLowerCase()
230+
document.activeElement.tagName.toLowerCase()
235231
);
236-
expect(focusedElementTagName).toBe(focusableElementTagName);
232+
expect(focusedElementTagName).toBe("ch-combo-box-render");
237233
});
238234
});
239235

0 commit comments

Comments
 (0)