Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(select): Manually run change detector when option updates #236

Merged
merged 2 commits into from
Aug 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/modules/select/classes/select-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DropdownService, SuiDropdownMenu } from "../../dropdown";
import { SearchService, LookupFn, FilterFn } from "../../search";
import { Util, ITemplateRefContext, HandledEvent, KeyCode } from "../../../misc/util";
import { ISelectLocaleValues, RecursivePartial, SuiLocalizationService } from "../../../behaviors/localization";
import { SuiSelectOption, ISelectRenderedOption } from "../components/select-option";
import { SuiSelectOption } from "../components/select-option";
import { SuiSelectSearch } from "../directives/select-search";

export interface IOptionContext<T> extends ITemplateRefContext<T> {
Expand Down Expand Up @@ -150,7 +150,7 @@ export abstract class SuiSelectBase<T, U> implements AfterContentInit {
this.queryUpdateHook();
this.updateQuery(query);
// Update the rendered text as query has changed.
this._renderedOptions.forEach(ro => ro.formatter = this.configuredFormatter);
this._renderedOptions.forEach(ro => this.initialiseRenderedOption(ro));

if (this.searchInput) {
this.searchInput.query = query;
Expand Down Expand Up @@ -314,13 +314,15 @@ export abstract class SuiSelectBase<T, U> implements AfterContentInit {
}
}

protected initialiseRenderedOption(option:ISelectRenderedOption<T>):void {
protected initialiseRenderedOption(option:SuiSelectOption<T>):void {
option.usesTemplate = !!this.optionTemplate;
option.formatter = this.configuredFormatter;

if (option.usesTemplate) {
this.drawTemplate(option.templateSibling, option.value);
}

option.changeDetector.detectChanges();
}

public abstract selectOption(option:T):void;
Expand Down
4 changes: 2 additions & 2 deletions src/modules/select/components/multi-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, HostBinding, ElementRef, EventEmitter, Output, Input, Direct
import { ICustomValueAccessorHost, KeyCode, customValueAccessorFactory, CustomValueAccessor } from "../../../misc/util";
import { SuiLocalizationService } from "../../../behaviors/localization";
import { SuiSelectBase } from "../classes/select-base";
import { ISelectRenderedOption } from "./select-option";
import { SuiSelectOption } from "./select-option";

@Component({
selector: "sui-multi-select",
Expand Down Expand Up @@ -158,7 +158,7 @@ export class SuiMultiSelect<T, U> extends SuiSelectBase<T, U> implements ICustom
}
}

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

// Boldens the item so it appears selected in the dropdown.
Expand Down
16 changes: 4 additions & 12 deletions src/modules/select/components/select-option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@ import {
import { SuiDropdownMenuItem } from "../../dropdown";
import { HandledEvent } from "../../../misc/util";

export interface ISelectRenderedOption<T> {
value:T;
isActive?:boolean;
formatter:(o:T) => string;
usesTemplate:boolean;
templateSibling:ViewContainerRef;
}

@Component({
selector: "sui-select-option",
template: `
<span #templateSibling></span>
<span [innerHTML]="renderedText"></span>
`
})
export class SuiSelectOption<T> extends SuiDropdownMenuItem implements ISelectRenderedOption<T> {
export class SuiSelectOption<T> extends SuiDropdownMenuItem {
// Sets the Semantic UI classes on the host element.
@HostBinding("class.item")
private _optionClasses:boolean;
Expand Down Expand Up @@ -51,7 +43,7 @@ export class SuiSelectOption<T> extends SuiDropdownMenuItem implements ISelectRe
@ViewChild("templateSibling", { read: ViewContainerRef })
public templateSibling:ViewContainerRef;

constructor(renderer:Renderer2, element:ElementRef) {
constructor(renderer:Renderer2, element:ElementRef, public changeDetector:ChangeDetectorRef) {
// We inherit SuiDropdownMenuItem to automatically gain all keyboard navigation functionality.
// This is not done via adding the .item class because it isn't supported by Angular.
super(renderer, element);
Expand All @@ -60,8 +52,8 @@ export class SuiSelectOption<T> extends SuiDropdownMenuItem implements ISelectRe
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.
this.formatter = o => "";
// By default we make the default text an empty label, for the brief moment when it isn't displaying the correct one.
this.renderedText = "";

this.usesTemplate = false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/select/components/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, ViewContainerRef, ViewChild, Output, EventEmitter, ElementRe
import { ICustomValueAccessorHost, customValueAccessorFactory, CustomValueAccessor } from "../../../misc/util";
import { SuiLocalizationService } from "../../../behaviors/localization";
import { SuiSelectBase } from "../classes/select-base";
import { ISelectRenderedOption } from "./select-option";
import { SuiSelectOption } from "./select-option";

@Component({
selector: "sui-select",
Expand Down Expand Up @@ -122,7 +122,7 @@ export class SuiSelect<T, U> extends SuiSelectBase<T, U> implements ICustomValue
}
}

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

// Boldens the item so it appears selected in the dropdown.
Expand Down