vgapp 0.9.2 → 0.9.3

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.
@@ -87,7 +87,9 @@ const _handlersVGSelect = () => {
87
87
 
88
88
  container.querySelectorAll(`.${CLASS_NAME_OPTION}`).forEach(o => o.classList.remove('selected'));
89
89
  option.classList.add('selected');
90
- VGSelect.changeSelector(select, value, {
90
+
91
+ VGSelect.changeSelectorByIndex(select, idx, {
92
+ index: idx,
91
93
  value,
92
94
  title: option.textContent,
93
95
  ...Manipulator.get(option)
@@ -656,6 +656,47 @@ class VGSelect extends BaseModule {
656
656
  }
657
657
  }
658
658
 
659
+ /**
660
+ * Программно устанавливает выбор <select> по индексу option (надёжно даже при пустом/отсутствующем value)
661
+ * @param {HTMLSelectElement} select
662
+ * @param {number} index
663
+ * @param {Object} [data]
664
+ */
665
+ static changeSelectorByIndex(select, index, data = {}) {
666
+ const container = select.nextElementSibling;
667
+ const instance = container ? VGSelect.getInstance(container) : null;
668
+
669
+ select.setAttribute('data-updating', 'true');
670
+ try {
671
+ const opt = Number.isInteger(index) ? select.options[index] : null;
672
+ if (!opt) {
673
+ instance?._triggerEvent(EVENT_KEY_ERROR, { error: 'Option not found by index', index });
674
+ return;
675
+ }
676
+
677
+ const wasSelected = opt.selected;
678
+ const selectedText = opt.textContent.trim();
679
+ const value = opt.value;
680
+
681
+ [...select.options].forEach(o => o.selected = false);
682
+ opt.selected = true;
683
+ select.value = opt.value;
684
+
685
+ this.updateUI(select);
686
+
687
+ const e = new Event('change', { bubbles: true, cancelable: true });
688
+ select.dispatchEvent(e);
689
+
690
+ if (!wasSelected) {
691
+ EventHandler.trigger(select, EVENT_KEY_CHANGE, { data });
692
+ instance?._triggerEvent(EVENT_KEY_SELECT, { value, text: selectedText, data });
693
+ instance?._callCallback('onSelect', { value, text: selectedText, data });
694
+ }
695
+ } finally {
696
+ select.removeAttribute('data-updating');
697
+ }
698
+ }
699
+
659
700
  /**
660
701
  * Вызывает кастомное событие
661
702
  * @param {string} eventName - Имя события
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vgapp",
3
- "version": "0.9.2",
3
+ "version": "0.9.3",
4
4
  "description": "",
5
5
  "author": {
6
6
  "name": "Vegas Studio",