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): Selected options now updated when options change #232

Merged
merged 1 commit 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
9 changes: 8 additions & 1 deletion src/modules/select/components/multi-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,16 @@ export class SuiMultiSelect<T, U> extends SuiSelectBase<T, U> implements ICustom
}

protected optionsUpdateHook():void {
if (!this._writtenOptions && this.selectedOptions.length > 0) {
// We need to check the options still exist.
this.writeValue(this.selectedOptions.map(o => this.valueGetter(o)));
}

if (this._writtenOptions && this.searchService.options.length > 0) {
// If there were values written by ngModel before the options had been loaded, this runs to fix it.
this.selectedOptions = this._writtenOptions
// non-null assertion added here because Typescript doesn't recognise the non-null filter.
.map(v => this.searchService.options.find(o => v === this.valueGetter(o))!)
.map(v => this.findOption(this.searchService.options, v)!)
.filter(v => v != undefined);

if (this.selectedOptions.length === this._writtenOptions.length) {
Expand Down Expand Up @@ -206,6 +211,8 @@ export class SuiMultiSelect<T, U> extends SuiSelectBase<T, U> implements ICustom
if (values.length === 0) {
this.selectedOptions = [];
}
} else {
this.selectedOptions = [];
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/modules/select/components/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export class SuiSelect<T, U> extends SuiSelectBase<T, U> implements ICustomValue
}

protected optionsUpdateHook():void {
if (!this._writtenOption && this.selectedOption) {
// We need to check the option still exists.
this.writeValue(this.valueGetter(this.selectedOption));
}

if (this._writtenOption && this.searchService.options.length > 0) {
// If there was an value written by ngModel before the options had been loaded, this runs to fix it.
this.selectedOption = this.findOption(this.searchService.options, this._writtenOption);
Expand Down