Skip to content

Commit

Permalink
Added support for select highlighting active item
Browse files Browse the repository at this point in the history
  • Loading branch information
edcarroll committed Mar 22, 2017
1 parent 8cf6014 commit 041aa70
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion components/select/select-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export abstract class SuiSelectBase<T, U> implements AfterContentInit {
this._renderedOptions.changes.subscribe(() => this.onAvailableOptionsRendered());
}

private onAvailableOptionsRendered() {
protected onAvailableOptionsRendered() {
// Unsubscribe from all previous subscriptions to avoid memory leaks on large selects.
this._renderedSubscriptions.forEach(rs => rs.unsubscribe());
this._renderedSubscriptions = [];
Expand Down
5 changes: 5 additions & 0 deletions components/select/select-option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type PropertyReader<T> = (obj:T) => string;

export interface ISelectRenderedOption<T> {
value:T;
isActive?:boolean;
readLabel:PropertyReader<T>;
usesTemplate:boolean;
templateSibling:ViewContainerRef;
Expand All @@ -29,6 +30,9 @@ export class SuiSelectOption<T> extends SuiDropdownMenuItem implements ISelectRe
@Output()
public onSelected:EventEmitter<T>;

@HostBinding('class.active')
public isActive:boolean;

// Returns the label from a given value.
public readLabel:(obj:T) => string;

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

this._optionClasses = true;
this.isActive = false;
this.onSelected = new EventEmitter<T>();

// By default we make this function return an empty string, for the brief moment when it isn't displaying the correct label.
Expand Down
10 changes: 10 additions & 0 deletions components/select/select.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Component, ViewContainerRef, ViewChild, Output, EventEmitter, ElementRef, Renderer, forwardRef, Directive} from '@angular/core';
import {SuiSelectBase} from './select-base';
import {NG_VALUE_ACCESSOR, ControlValueAccessor} from '@angular/forms';
import {ISelectRenderedOption} from './select-option';

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

this.drawSelectedOption();
// Adds the active property to the items.
this.onAvailableOptionsRendered();

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

protected initialiseRenderedOption(option:ISelectRenderedOption<T>) {
super.initialiseRenderedOption(option);

// Boldens the item so it appears selected in the dropdown.
option.isActive = option.value == this.selectedOption;
}

private drawSelectedOption() {
if (this.selectedOption && this.optionTemplate) {
this.drawTemplate(this._optionTemplateSibling, this.selectedOption);
Expand Down

0 comments on commit 041aa70

Please sign in to comment.