fix/simplify fitSelectBox

This commit is contained in:
tophf 2021-01-07 12:35:44 +03:00
parent 11d311d1e8
commit 1308efb8d0

View File

@ -229,7 +229,7 @@ function fitSelectBox(...elems) {
const data = [];
for (const el of elems) {
const sel = el.selectedOptions[0];
if (!sel) return;
if (!sel) continue;
const oldWidth = parseFloat(el.style.width);
const text = [];
data.push({el, text, oldWidth});
@ -237,15 +237,13 @@ function fitSelectBox(...elems) {
text.push(elOpt.textContent);
if (elOpt !== sel) elOpt.textContent = '';
}
el.style.width = '';
el.style.width = 'min-content';
}
for (const {el, text, oldWidth} of data) {
const w = el.offsetWidth;
if (w && oldWidth !== w) el.style.width = w + 'px';
text.forEach((t, i) => (el.options[i].textContent = t));
}
requestAnimationFrame(() => {
for (const {el, text, oldWidth} of data) {
const w = el.offsetWidth;
if (w && oldWidth !== w) el.style.width = w + 'px';
text.forEach((t, i) => (el[i].textContent = t));
}
});
}
/* exported fitSelectBoxesIn */