Skip to content

Commit 5009b9f

Browse files
committed
feat(listbox): added additional focus management logic and a getSelectedValues function.
1 parent 8221c12 commit 5009b9f

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/cdk-experimental/listbox/listbox.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,11 @@ describe('CdkOption and CdkListbox', () => {
659659
listboxInstance.writeValue(['arc', 'stasis']);
660660
fixture.detectChanges();
661661

662+
const selectedValues = listboxInstance.getSelectedValues();
663+
expect(selectedValues.length).toBe(2);
664+
expect(selectedValues[0]).toBe('arc');
665+
expect(selectedValues[1]).toBe('stasis');
666+
662667
expect(optionElements[0].hasAttribute('aria-selected')).toBeFalse();
663668
expect(optionElements[1].hasAttribute('aria-selected')).toBeFalse();
664669

src/cdk-experimental/listbox/listbox.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ export class CdkOption<T = unknown> implements ListKeyManagerOption, Highlightab
176176
}
177177
}
178178

179+
getElementRef() {
180+
return this._elementRef;
181+
}
182+
179183
/** Sets the active property to true to enable the active css class. */
180184
setActiveStyles() {
181185
this._active = true;
@@ -273,6 +277,7 @@ export class CdkListbox<T> implements AfterContentInit, OnDestroy, OnInit, Contr
273277
@Input('parentPanel') private readonly _explicitPanel: CdkComboboxPanel;
274278

275279
constructor(
280+
private readonly _elementRef: ElementRef,
276281
@Optional() @Inject(PANEL) readonly _parentPanel?: CdkComboboxPanel<T>,
277282
) { }
278283

@@ -421,6 +426,10 @@ export class CdkListbox<T> implements AfterContentInit, OnDestroy, OnInit, Contr
421426

422427
if (!this.useActiveDescendant) {
423428
this._activeOption.focus();
429+
} else {
430+
if (document.activeElement === this._activeOption.getElementRef().nativeElement) {
431+
this._elementRef.nativeElement.focus();
432+
}
424433
}
425434
}
426435

@@ -458,6 +467,7 @@ export class CdkListbox<T> implements AfterContentInit, OnDestroy, OnInit, Contr
458467
/** Updates the key manager's active item to the given option. */
459468
setActiveOption(option: CdkOption<T>) {
460469
this._listKeyManager.updateActiveItem(option);
470+
this._updateActiveOption();
461471
}
462472

463473
/**
@@ -488,6 +498,11 @@ export class CdkListbox<T> implements AfterContentInit, OnDestroy, OnInit, Contr
488498
this.disabled = isDisabled;
489499
}
490500

501+
/** Returns the values of the currently selected options. */
502+
getSelectedValues(): T[] {
503+
return this._options.filter(option => option.selected).map(option => option.value);
504+
}
505+
491506
/** Selects an option that has the corresponding given value. */
492507
private _setSelectionByValue(values: T | T[]) {
493508
for (const option of this._options.toArray()) {

0 commit comments

Comments
 (0)