Skip to content

Commit

Permalink
fix(bootstrap): select empty option when value is null (#1652)
Browse files Browse the repository at this point in the history
fix #1625
  • Loading branch information
aitboudad committed Jul 6, 2019
1 parent 165ac75 commit 50a1138
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/bootstrap/src/lib/types/select.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component } from '@angular/core';
import { Component, ViewChild, AfterViewChecked, ElementRef } from '@angular/core';
import { FieldType } from '@ngx-formly/core';

@Component({
Expand All @@ -22,6 +22,7 @@ import { FieldType } from '@ngx-formly/core';
<ng-template #singleSelect>
<select class="form-control"
#select
[formControl]="formControl"
[class.custom-select]="to.customSelect"
[class.is-invalid]="showError"
Expand All @@ -39,8 +40,21 @@ import { FieldType } from '@ngx-formly/core';
</ng-template>
`,
})
export class FormlyFieldSelect extends FieldType {
export class FormlyFieldSelect extends FieldType implements AfterViewChecked {
@ViewChild('select') select!: ElementRef<HTMLSelectElement>;
defaultOptions = {
templateOptions: { options: [] },
};

// workaround for https://github.com/angular/angular/issues/10010
ngAfterViewChecked() {
if (!this.to.multiple && !this.to.placeholder && this.formControl.value === null) {
const selectEl = this.select.nativeElement;
if (selectEl.selectedIndex !== -1
&& (!selectEl.options[selectEl.selectedIndex] || selectEl.options[selectEl.selectedIndex].value !== null)
) {
this.select.nativeElement.selectedIndex = -1;
}
}
}
}

0 comments on commit 50a1138

Please sign in to comment.