Skip to content

Commit 041aa70

Browse files
committed
Added support for select highlighting active item
1 parent 8cf6014 commit 041aa70

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

components/select/select-base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export abstract class SuiSelectBase<T, U> implements AfterContentInit {
146146
this._renderedOptions.changes.subscribe(() => this.onAvailableOptionsRendered());
147147
}
148148

149-
private onAvailableOptionsRendered() {
149+
protected onAvailableOptionsRendered() {
150150
// Unsubscribe from all previous subscriptions to avoid memory leaks on large selects.
151151
this._renderedSubscriptions.forEach(rs => rs.unsubscribe());
152152
this._renderedSubscriptions = [];

components/select/select-option.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type PropertyReader<T> = (obj:T) => string;
55

66
export interface ISelectRenderedOption<T> {
77
value:T;
8+
isActive?:boolean;
89
readLabel:PropertyReader<T>;
910
usesTemplate:boolean;
1011
templateSibling:ViewContainerRef;
@@ -29,6 +30,9 @@ export class SuiSelectOption<T> extends SuiDropdownMenuItem implements ISelectRe
2930
@Output()
3031
public onSelected:EventEmitter<T>;
3132

33+
@HostBinding('class.active')
34+
public isActive:boolean;
35+
3236
// Returns the label from a given value.
3337
public readLabel:(obj:T) => string;
3438

@@ -44,6 +48,7 @@ export class SuiSelectOption<T> extends SuiDropdownMenuItem implements ISelectRe
4448
super(renderer, element);
4549

4650
this._optionClasses = true;
51+
this.isActive = false;
4752
this.onSelected = new EventEmitter<T>();
4853

4954
// By default we make this function return an empty string, for the brief moment when it isn't displaying the correct label.

components/select/select.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Component, ViewContainerRef, ViewChild, Output, EventEmitter, ElementRef, Renderer, forwardRef, Directive} from '@angular/core';
22
import {SuiSelectBase} from './select-base';
33
import {NG_VALUE_ACCESSOR, ControlValueAccessor} from '@angular/forms';
4+
import {ISelectRenderedOption} from './select-option';
45

56
@Component({
67
selector: 'sui-select',
@@ -75,6 +76,8 @@ export class SuiSelect<T, U> extends SuiSelectBase<T, U> {
7576
this.searchService.updateQueryDelayed("");
7677

7778
this.drawSelectedOption();
79+
// Adds the active property to the items.
80+
this.onAvailableOptionsRendered();
7881

7982
// Automatically refocus the search input for better keyboard accessibility.
8083
this.focusInput();
@@ -95,6 +98,13 @@ export class SuiSelect<T, U> extends SuiSelectBase<T, U> {
9598
this.drawSelectedOption();
9699
}
97100

101+
protected initialiseRenderedOption(option:ISelectRenderedOption<T>) {
102+
super.initialiseRenderedOption(option);
103+
104+
// Boldens the item so it appears selected in the dropdown.
105+
option.isActive = option.value == this.selectedOption;
106+
}
107+
98108
private drawSelectedOption() {
99109
if (this.selectedOption && this.optionTemplate) {
100110
this.drawTemplate(this._optionTemplateSibling, this.selectedOption);

0 commit comments

Comments
 (0)