Skip to content

Commit db5207c

Browse files
engijlrnielslyngsoeiOvergaard
authored
feat(uui-combobox): Add open functionality with space bar (#1170)
* fix(uui-combobox): Add open functionality with space bar * Remove z-index in the button * Revert "Remove z-index in the button" This reverts commit c9785d2. * Update packages/uui-combobox-list/lib/uui-combobox-list.element.ts Co-authored-by: Jacob Overgaard <[email protected]> * Changed e.key for e.code for more consistency. --------- Co-authored-by: Niels Lyngsø <[email protected]> Co-authored-by: Jacob Overgaard <[email protected]>
1 parent 170662a commit db5207c

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

packages/uui-combobox-list/lib/uui-combobox-list.element.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export class UUIComboboxListElement extends LitElement {
207207
private _onKeyDown = (e: KeyboardEvent) => {
208208
if (this._options.length <= 0) return;
209209

210-
switch (e.key) {
210+
switch (e.code) {
211211
case 'ArrowUp':
212212
e.preventDefault();
213213
if (e.ctrlKey) {
@@ -239,6 +239,14 @@ export class UUIComboboxListElement extends LitElement {
239239
break;
240240
}
241241

242+
//Space key
243+
case 'Space': {
244+
e.preventDefault();
245+
e.stopPropagation();
246+
this._getActiveElement?.click();
247+
break;
248+
}
249+
242250
case 'End': {
243251
e.preventDefault();
244252
this._goToIndex(this._options.length - 1);

packages/uui-combobox/lib/uui-combobox.element.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,23 @@ export class UUIComboboxElement extends UUIFormControlMixin(LitElement, '') {
298298
};
299299

300300
#onKeyDown = (e: KeyboardEvent) => {
301-
if (this.open === false && e.key === 'Enter') {
301+
if (this.open === false && e.code === 'Enter') {
302302
e.preventDefault(); // TODO: could we avoid this.
303303
e.stopImmediatePropagation(); // TODO: could we avoid this.
304304
}
305305

306-
if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
306+
if (e.code === 'ArrowUp' || e.code === 'ArrowDown') {
307307
this.#onOpen();
308308
}
309309

310-
if (e.key === 'Escape' || e.key === 'Enter') {
310+
if (e.code === 'Space') {
311+
if (this._isOpen) return;
312+
e.preventDefault();
313+
e.stopImmediatePropagation();
314+
this.#onOpen();
315+
}
316+
317+
if (e.code === 'Escape' || e.code === 'Enter') {
311318
this.#onClose();
312319
}
313320
};

packages/uui-combobox/lib/uui-combobox.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@ describe('UUIComboboxElement', () => {
123123

124124
describe('keyboard navigation', () => {
125125
it('moves `active`-focus to second option on pressing the arrow down key', async () => {
126-
element.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' }));
127-
element.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' }));
126+
element.dispatchEvent(
127+
new KeyboardEvent('keydown', { key: 'ArrowDown', code: 'ArrowDown' }),
128+
);
129+
element.dispatchEvent(
130+
new KeyboardEvent('keydown', { key: 'ArrowDown', code: 'ArrowDown' }),
131+
);
128132

129133
await elementUpdated(element);
130134

0 commit comments

Comments
 (0)