ng-select2-component 13.0.11 → 13.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -382,8 +382,10 @@ class Select2 {
382
382
  set value(value) {
383
383
  if (this.testValueChange(this._value, value)) {
384
384
  setTimeout(() => {
385
- this._value = value;
386
- this.writeValue(value);
385
+ if (this._value === undefined) {
386
+ this._value = value ?? null;
387
+ }
388
+ this.writeValue(value ?? null);
387
389
  }, 10);
388
390
  }
389
391
  }
@@ -485,6 +487,7 @@ class Select2 {
485
487
  this._disabled = false;
486
488
  this._multiple = false;
487
489
  this._uid = `select2-${nextUniqueId++}`;
490
+ this._value = null;
488
491
  /** View -> model callback called when select has been touched */
489
492
  this._onTouched = () => {
490
493
  // do nothing
@@ -595,7 +598,9 @@ class Select2 {
595
598
  this.select(this.resetSelectedValue !== undefined
596
599
  ? Select2Utils.getOptionByValue(this._data, this.resetSelectedValue) ?? null
597
600
  : null);
598
- this.stopEvent(event);
601
+ if (event) {
602
+ this.stopEvent(event);
603
+ }
599
604
  }
600
605
  prevChange(event) {
601
606
  event.stopPropagation();
@@ -780,7 +785,7 @@ class Select2 {
780
785
  this._onTouched();
781
786
  }
782
787
  }
783
- select(option) {
788
+ select(option, emit = true) {
784
789
  let value;
785
790
  if (option !== null && option !== undefined) {
786
791
  if (this.multiple) {
@@ -802,25 +807,36 @@ class Select2 {
802
807
  this.selectionElement?.focus();
803
808
  }
804
809
  value = this.option.value;
810
+ if (!option && this._value === null) {
811
+ this._value = value ?? null;
812
+ }
805
813
  }
806
814
  }
807
815
  else {
816
+ // when remove value
817
+ if (Array.isArray(this.option) ? this.option?.length : this.option) {
818
+ value = '';
819
+ }
808
820
  this.option = null;
809
821
  }
810
822
  if (this.multiple && this.hideSelectedItems) {
811
823
  this.updateFilteredData();
812
824
  }
825
+ emit &&= value !== undefined && this.testDiffValue(this._value, value);
813
826
  if (this._control) {
814
827
  this._onChange(value);
815
828
  }
816
- else {
817
- this._value = value;
829
+ if (emit) {
830
+ this._value = value ?? null;
831
+ this.update.emit({
832
+ component: this,
833
+ value: this._value,
834
+ options: Array.isArray(this.option) ? this.option : this.option ? [this.option] : null,
835
+ });
818
836
  }
819
- this.update.emit({
820
- component: this,
821
- value: value,
822
- options: Array.isArray(this.option) ? this.option : this.option ? [this.option] : null,
823
- });
837
+ }
838
+ testDiffValue(val1, val2) {
839
+ return Array.isArray(val1) ? val1?.length !== val2?.length : val1 !== val2;
824
840
  }
825
841
  keyDown(event, create = false) {
826
842
  if (create && this._testKey(event, ['Enter', 13])) {
@@ -899,7 +915,7 @@ class Select2 {
899
915
  this._onChange(value);
900
916
  }
901
917
  else {
902
- this._value = value;
918
+ this._value = value ?? null;
903
919
  }
904
920
  this.update.emit({
905
921
  component: this,
@@ -1053,10 +1069,33 @@ class Select2 {
1053
1069
  if (isArray) {
1054
1070
  // value is not null. Preselect value
1055
1071
  const selectedValues = Select2Utils.getOptionsByValue(this._data, value, this.multiple);
1056
- selectedValues.map(item => this.select(item));
1072
+ selectedValues.map(item => this.select(item, false));
1073
+ this._value ??= value;
1074
+ if (this.testDiffValue(this._value, value)) {
1075
+ this.update.emit({
1076
+ component: this,
1077
+ value: this._value,
1078
+ options: Array.isArray(this.option) ? this.option : this.option ? [this.option] : null,
1079
+ });
1080
+ }
1081
+ }
1082
+ else if (value === null) {
1083
+ // fix if value is null
1084
+ this.value = [];
1085
+ this.reset();
1086
+ this.select(null, false);
1087
+ if (this.testDiffValue(this._value, value)) {
1088
+ this._value = [];
1089
+ this.update.emit({
1090
+ component: this,
1091
+ value: this._value,
1092
+ options: Array.isArray(this.option) ? this.option : this.option ? [this.option] : null,
1093
+ });
1094
+ }
1057
1095
  }
1058
1096
  }
1059
1097
  else {
1098
+ this._value ??= value;
1060
1099
  this.select(Select2Utils.getOptionByValue(this._data, value));
1061
1100
  }
1062
1101
  }