Skip to content

Commit

Permalink
Fix reset with multiple #69
Browse files Browse the repository at this point in the history
  • Loading branch information
Zefling committed Jul 23, 2024
1 parent 1e9d9e2 commit 3313c4f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
10 changes: 8 additions & 2 deletions projects/ng-select2-component/src/lib/select2.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export class Select2 implements ControlValueAccessor, OnInit, DoCheck, AfterView
}
}

reset(event: MouseEvent) {
reset(event?: MouseEvent) {
// const test = Select2Utils.getOptionByValue(this._data, this.resetSelectedValue);
// debugger;
this.select(
Expand All @@ -435,7 +435,9 @@ export class Select2 implements ControlValueAccessor, OnInit, DoCheck, AfterView
: null,
);

this.stopEvent(event);
if (event) {
this.stopEvent(event);
}
}

prevChange(event: Event) {
Expand Down Expand Up @@ -949,6 +951,10 @@ export class Select2 implements ControlValueAccessor, OnInit, DoCheck, AfterView
// value is not null. Preselect value
const selectedValues: any = Select2Utils.getOptionsByValue(this._data, value, this.multiple);
selectedValues.map(item => this.select(item));
} else if (value === null) {
// fix if value is null
this.value = [];
this.reset();
}
} else {
this.select(Select2Utils.getOptionByValue(this._data, value));
Expand Down
15 changes: 15 additions & 0 deletions src/app/app-examples.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,19 @@ <h3 id="ex-32">32. auto create when ({{ value30 }})</h3>
id="selec2-32"
(autoCreateItem)="update('value32', $event)"
></select2>

<h3 id="ex-33">33. reset form multiple({{ ctrlForm3.get('test33')?.value }})</h3>
<form [formGroup]="ctrlForm3">
<select2
[data]="data32"
placeholder="Select Name"
formControlName="test33"
multiple="true"
hideSelectedItems="true"
></select2>

<button (click)="reset()">Reset</button>
</form>


</div>
10 changes: 10 additions & 0 deletions src/app/app-examples.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ export class AppExamplesComponent {
data29b: Select2Data = JSON.parse(JSON.stringify(data1));
data30: Select2Data = JSON.parse(JSON.stringify(data1));
data31 = data31en;
data32: Select2Data = JSON.parse(JSON.stringify(data3));

minCountForSearch = Infinity;

ctrlForm: UntypedFormGroup;
ctrlForm2: UntypedFormGroup;
ctrlForm3: UntypedFormGroup;

value1 = 'CA';
value2 = 'CA';
Expand Down Expand Up @@ -120,6 +122,10 @@ export class AppExamplesComponent {
test5: new UntypedFormControl(0, Validators.required),
});

this.ctrlForm3 = this.fb.group({
test33: new UntypedFormControl(null),
});

this.fg.patchValue(this.formData());
}

Expand All @@ -132,6 +138,10 @@ export class AppExamplesComponent {
this.data6.pop();
}

reset() {
this.ctrlForm3.reset();
}

open(key: string, event: Event) {
console.log(key, event);
}
Expand Down

0 comments on commit 3313c4f

Please sign in to comment.