ng-select2-component 15.0.0 → 15.0.1

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.
@@ -375,8 +375,10 @@ class Select2 {
375
375
  set value(value) {
376
376
  if (this.testValueChange(this._value, value)) {
377
377
  setTimeout(() => {
378
- this._value = value;
379
- this.writeValue(value);
378
+ if (this._value === undefined) {
379
+ this._value = value ?? null;
380
+ }
381
+ this.writeValue(value ?? null);
380
382
  }, 10);
381
383
  }
382
384
  }
@@ -478,6 +480,7 @@ class Select2 {
478
480
  this._disabled = false;
479
481
  this._multiple = false;
480
482
  this._uid = `select2-${nextUniqueId++}`;
483
+ this._value = null;
481
484
  /** View -> model callback called when select has been touched */
482
485
  this._onTouched = () => {
483
486
  // do nothing
@@ -500,7 +503,7 @@ class Select2 {
500
503
  if (!this.ifParentContainsClass(target, 'select2-dropdown')) {
501
504
  this.toggleOpenAndClose();
502
505
  }
503
- if (!this.ifParentContainsId(target, this._id)) {
506
+ if (!this.overlay && !this.ifParentContainsId(target, this._id)) {
504
507
  this.clickExit();
505
508
  }
506
509
  }
@@ -509,6 +512,10 @@ class Select2 {
509
512
  this.clickExit();
510
513
  }
511
514
  }
515
+ else if (this.focused) {
516
+ const target = e.target;
517
+ this._focus(this.clickOnSelect2Element(target));
518
+ }
512
519
  }
513
520
  ngOnInit() {
514
521
  this._viewportRuler.change(100).subscribe(() => {
@@ -584,7 +591,9 @@ class Select2 {
584
591
  this.select(this.resetSelectedValue !== undefined
585
592
  ? Select2Utils.getOptionByValue(this._data, this.resetSelectedValue) ?? null
586
593
  : null);
587
- this.stopEvent(event);
594
+ if (event) {
595
+ this.stopEvent(event);
596
+ }
588
597
  }
589
598
  prevChange(event) {
590
599
  event.stopPropagation();
@@ -738,6 +747,26 @@ class Select2 {
738
747
  }
739
748
  return true;
740
749
  }
750
+ containAlmostOneClasses(element, cssClasses) {
751
+ if (!element.classList) {
752
+ return false;
753
+ }
754
+ let containAlmostOne = false;
755
+ for (const cssClass of cssClasses) {
756
+ if (element.classList.contains(cssClass)) {
757
+ containAlmostOne = true;
758
+ }
759
+ }
760
+ return containAlmostOne;
761
+ }
762
+ clickOnSelect2Element(element) {
763
+ return this.containAlmostOneClasses(element, [
764
+ 'select2-overlay-backdrop',
765
+ 'select2-label-content',
766
+ 'select2-selection__rendered',
767
+ 'select2-results__option',
768
+ ]);
769
+ }
741
770
  focusin() {
742
771
  if (!this.disabled) {
743
772
  this._focus(true);
@@ -749,7 +778,7 @@ class Select2 {
749
778
  this._onTouched();
750
779
  }
751
780
  }
752
- select(option) {
781
+ select(option, emit = true) {
753
782
  let value;
754
783
  if (option !== null && option !== undefined) {
755
784
  if (this.multiple) {
@@ -771,25 +800,36 @@ class Select2 {
771
800
  this.selectionElement?.focus();
772
801
  }
773
802
  value = this.option.value;
803
+ if (!option && this._value === null) {
804
+ this._value = value ?? null;
805
+ }
774
806
  }
775
807
  }
776
808
  else {
809
+ // when remove value
810
+ if (Array.isArray(this.option) ? this.option?.length : this.option) {
811
+ value = '';
812
+ }
777
813
  this.option = null;
778
814
  }
779
815
  if (this.multiple && this.hideSelectedItems) {
780
816
  this.updateFilteredData();
781
817
  }
818
+ emit &&= value !== undefined && this.testDiffValue(this._value, value);
782
819
  if (this._control) {
783
820
  this._onChange(value);
784
821
  }
785
- else {
786
- this._value = value;
822
+ if (emit) {
823
+ this._value = value ?? null;
824
+ this.update.emit({
825
+ component: this,
826
+ value: this._value,
827
+ options: Array.isArray(this.option) ? this.option : this.option ? [this.option] : null,
828
+ });
787
829
  }
788
- this.update.emit({
789
- component: this,
790
- value: value,
791
- options: Array.isArray(this.option) ? this.option : this.option ? [this.option] : null,
792
- });
830
+ }
831
+ testDiffValue(val1, val2) {
832
+ return Array.isArray(val1) ? val1?.length !== val2?.length : val1 !== val2;
793
833
  }
794
834
  keyDown(event, create = false) {
795
835
  if (create && this._testKey(event, ['Enter', 13])) {
@@ -868,7 +908,7 @@ class Select2 {
868
908
  this._onChange(value);
869
909
  }
870
910
  else {
871
- this._value = value;
911
+ this._value = value ?? null;
872
912
  }
873
913
  this.update.emit({
874
914
  component: this,
@@ -1022,10 +1062,33 @@ class Select2 {
1022
1062
  if (isArray) {
1023
1063
  // value is not null. Preselect value
1024
1064
  const selectedValues = Select2Utils.getOptionsByValue(this._data, value, this.multiple);
1025
- selectedValues.map(item => this.select(item));
1065
+ selectedValues.map(item => this.select(item, false));
1066
+ this._value ??= value;
1067
+ if (this.testDiffValue(this._value, value)) {
1068
+ this.update.emit({
1069
+ component: this,
1070
+ value: this._value,
1071
+ options: Array.isArray(this.option) ? this.option : this.option ? [this.option] : null,
1072
+ });
1073
+ }
1074
+ }
1075
+ else if (value === null) {
1076
+ // fix if value is null
1077
+ this.value = [];
1078
+ this.reset();
1079
+ this.select(null, false);
1080
+ if (this.testDiffValue(this._value, value)) {
1081
+ this._value = [];
1082
+ this.update.emit({
1083
+ component: this,
1084
+ value: this._value,
1085
+ options: Array.isArray(this.option) ? this.option : this.option ? [this.option] : null,
1086
+ });
1087
+ }
1026
1088
  }
1027
1089
  }
1028
1090
  else {
1091
+ this._value ??= value;
1029
1092
  this.select(Select2Utils.getOptionByValue(this._data, value));
1030
1093
  }
1031
1094
  }