vgapp 0.9.1 → 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
|
-
|
|
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 - Имя события
|
|
@@ -873,6 +914,18 @@ class VGSelect extends BaseModule {
|
|
|
873
914
|
|
|
874
915
|
// Очистка старых опций
|
|
875
916
|
[...select.querySelectorAll('option')].forEach(opt => {
|
|
917
|
+
// Оставляем первый пустой скрытый placeholder:
|
|
918
|
+
// <option value="" selected hidden></option>
|
|
919
|
+
const isFirst = opt === select.options[0];
|
|
920
|
+
const isEmptyValue = (opt.getAttribute('value') ?? '') === '';
|
|
921
|
+
const isEmptyText = (opt.textContent || '').trim() === '';
|
|
922
|
+
const isHidden = opt.hidden === true || opt.hasAttribute('hidden');
|
|
923
|
+
const isSelectedAttr = opt.hasAttribute('selected');
|
|
924
|
+
|
|
925
|
+
if (isFirst && isEmptyValue && isEmptyText && isHidden && isSelectedAttr) {
|
|
926
|
+
return;
|
|
927
|
+
}
|
|
928
|
+
|
|
876
929
|
if (!opt.hasAttribute('data-preserve') && !opt.closest('optgroup[data-preserve]')) {
|
|
877
930
|
opt.remove();
|
|
878
931
|
}
|