Skip to content

Commit

Permalink
Merge pull request #13 from Harvest-Dev/fix_writevalue_null
Browse files Browse the repository at this point in the history
Fix update of list when value is null/undefined/empty
  • Loading branch information
Zefling authored Oct 5, 2020
2 parents 5cec3f4 + 0e8226a commit c1f775e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
10 changes: 4 additions & 6 deletions projects/ng-select2-component/src/lib/select2.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import {
AfterViewInit, Attribute, ChangeDetectorRef, Component, DoCheck, ElementRef,
EventEmitter, HostBinding, Input, OnDestroy, OnInit, Optional,
Output, QueryList, Self, ViewChild, ViewChildren
AfterViewInit, Attribute, ChangeDetectorRef, Component, DoCheck, ElementRef, EventEmitter, HostBinding, Input,
OnDestroy, OnInit, Optional, Output, QueryList, Self, ViewChild, ViewChildren
} from '@angular/core';
import { ControlValueAccessor, FormGroupDirective, NgControl, NgForm } from '@angular/forms';

import { Subject } from 'rxjs';

import {
Select2Data, Select2Option, Select2UpdateEvent, Select2UpdateValue, Select2Utils, Select2Value,
timeout
Select2Data, Select2Option, Select2UpdateEvent, Select2UpdateValue, Select2Utils, Select2Value, timeout
} from './select2-utils';

let nextUniqueId = 0;
Expand Down Expand Up @@ -485,7 +483,7 @@ export class Select2 implements ControlValueAccessor, OnInit, OnDestroy, DoCheck
}
value = this.option.value;
}
} else if (this._control) {
} else {
this.option = null;
}

Expand Down
10 changes: 10 additions & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,14 @@ <h3>20. nostyle ({{value20}})</h3>
id="selec2-20">
</select2>
</div>
<h3>21. update to empty/null/undefined ({{value21}})</h3>
<select2 [data]="data21"
[value]="value21"
(update)="update21($event)"
listPosition="above"
id="selec2-21">
</select2>
<button (click)="value21 = ''">Update value to empty string</button>
<button (click)="value21 = null">Update value to null</button>
<button (click)="value21 = undefined">Update value to undefined</button>
</div>
11 changes: 7 additions & 4 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Component } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';

import {
data1, data13, data17, data18, data19, data2,
data3, data5
} from './app.data';
import { data1, data13, data17, data18, data19, data2, data3, data5 } from './app.data';

import { Select2Data, Select2Option, Select2UpdateEvent } from 'projects/ng-select2-component/src/lib/select2-utils';

Expand Down Expand Up @@ -33,6 +30,7 @@ export class AppComponent {
data18 = data18;
data19 = data19;
data20: Select2Data = JSON.parse(JSON.stringify(data19));
data21: Select2Data = JSON.parse(JSON.stringify(data19));

minCountForSearch = Infinity;

Expand All @@ -57,6 +55,7 @@ export class AppComponent {
value18 = '';
value19 = '';
value20 = '';
value21 = 'foo6';

limitSelection = 0;

Expand Down Expand Up @@ -177,6 +176,10 @@ export class AppComponent {
this.value20 = event.value;
}

update21(event: Select2UpdateEvent<string>) {
this.value21 = event.value;
}

resetForm() {
this.fg.reset(this.formData());
}
Expand Down

0 comments on commit c1f775e

Please sign in to comment.