Skip to content

Commit 07cd40e

Browse files
committed
fix: few more fixes
- ms-drop shouldn't have a [name] attribute since it breaks form submit example, let's use [data-name] instead - auto-width needs a CPU cycle delay to work properly, without it the width is 0px - virtual-scroll was throwing some divided by zero errors
1 parent db919f5 commit 07cd40e

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

demo/src/options/options10.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ <h2 class="bd-title">
1717
</span>
1818
</span>
1919
</h2>
20-
<div class="demo-subtitle">Use <code>displayTitle: true</code> option to display title of selected text.</div>
20+
<div class="demo-subtitle">Use <code>displayTitle: true</code> option to display title (for tooltip) of selected text.</div>
2121
</div>
2222
</div>
2323

lib/src/MultipleSelectInstance.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ export class MultipleSelectInstance {
6565
this.initSelected(true);
6666
this.initFilter();
6767
this.initDrop();
68-
this.initView();
69-
this.options.onAfterCreate();
68+
69+
// wait a CPU cycle for the select auto-width to return a width which is over 0
70+
setTimeout(() => {
71+
this.initView();
72+
this.options.onAfterCreate();
73+
});
7074
}
7175

7276
/**
@@ -206,9 +210,9 @@ export class MultipleSelectInstance {
206210
className: `ms-drop ${this.options.position}`,
207211
});
208212

209-
// add name attribute when defined
213+
// add data-name attribute when name option is defined
210214
if (name) {
211-
this.dropElm.setAttribute('name', name);
215+
this.dropElm.dataset.name = name;
212216
}
213217

214218
this.closeElm = this.choiceElm.querySelector('.icon-close');
@@ -356,7 +360,7 @@ export class MultipleSelectInstance {
356360
}
357361

358362
let length = 0;
359-
for (const option of this.data) {
363+
for (const option of this.data || []) {
360364
if (option.type === 'optgroup') {
361365
length += (option as OptGroupRowData).children.length;
362366
} else {
@@ -382,10 +386,11 @@ export class MultipleSelectInstance {
382386
}
383387

384388
if (this.options.selectAll && !this.options.single) {
389+
const selectName = this.elm.getAttribute('name') || this.options.name || '';
385390
this.selectAllParentElm = createDomElement('div', { className: 'ms-select-all' });
386391
const saLabelElm = createDomElement('label');
387392
const saInputElm = createDomElement('input', { type: 'checkbox', checked: this.allSelected });
388-
saInputElm.dataset.name = 'selectAll';
393+
saInputElm.dataset.name = `selectAll${selectName}`;
389394
saLabelElm.appendChild(saInputElm);
390395
saLabelElm.appendChild(createDomElement('span', { textContent: this.formatSelectAll() }));
391396
this.selectAllParentElm.appendChild(saLabelElm);
@@ -416,9 +421,7 @@ export class MultipleSelectInstance {
416421
}
417422

418423
if (rows.length > Constants.BLOCK_ROWS * Constants.CLUSTER_BLOCKS) {
419-
if (this.virtualScroll) {
420-
this.virtualScroll.destroy();
421-
}
424+
this.virtualScroll?.destroy();
422425

423426
const dropVisible = this.dropElm.style.display !== 'none';
424427
if (!dropVisible) {
@@ -427,13 +430,15 @@ export class MultipleSelectInstance {
427430
}
428431

429432
const updateDataOffset = () => {
430-
this.updateDataStart = this.virtualScroll!.dataStart + offset;
431-
this.updateDataEnd = this.virtualScroll!.dataEnd + offset;
432-
if (this.updateDataStart < 0) {
433-
this.updateDataStart = 0;
434-
}
435-
if (this.updateDataEnd > this.data.length) {
436-
this.updateDataEnd = this.data.length;
433+
if (this.virtualScroll) {
434+
this.updateDataStart = this.virtualScroll.dataStart + offset;
435+
this.updateDataEnd = this.virtualScroll.dataEnd + offset;
436+
if (this.updateDataStart < 0) {
437+
this.updateDataStart = 0;
438+
}
439+
if (this.updateDataEnd > this.data.length) {
440+
this.updateDataEnd = this.data.length;
441+
}
437442
}
438443
};
439444

@@ -459,10 +464,10 @@ export class MultipleSelectInstance {
459464
} else {
460465
if (this.ulElm) {
461466
this.ulElm.innerHTML = this.options.sanitizer ? this.options.sanitizer(rows.join('')) : rows.join('');
462-
this.updateDataStart = 0;
463-
this.updateDataEnd = this.updateData.length;
464-
this.virtualScroll = null;
465467
}
468+
this.updateDataStart = 0;
469+
this.updateDataEnd = this.updateData.length;
470+
this.virtualScroll = null;
466471
}
467472
this.events();
468473
}
@@ -566,7 +571,7 @@ export class MultipleSelectInstance {
566571
protected initSelected(ignoreTrigger = false) {
567572
let selectedTotal = 0;
568573

569-
for (const row of this.data) {
574+
for (const row of this.data || []) {
570575
if (row.type === 'optgroup') {
571576
const selectedCount = (row as OptGroupRowData).children.filter((child) => {
572577
return child && child.selected && !child.disabled && child.visible;
@@ -1031,7 +1036,7 @@ export class MultipleSelectInstance {
10311036
// value html, or text, default: 'value'
10321037
getSelects(type = 'value') {
10331038
const values = [];
1034-
for (const row of this.data) {
1039+
for (const row of this.data || []) {
10351040
if (row.type === 'optgroup') {
10361041
const selectedChildren = (row as OptGroupRowData).children.filter((child) => child?.selected);
10371042
if (!selectedChildren.length) {
@@ -1079,7 +1084,7 @@ export class MultipleSelectInstance {
10791084
}
10801085
};
10811086

1082-
for (const row of this.data) {
1087+
for (const row of this.data || []) {
10831088
if (row.type === 'optgroup') {
10841089
_setSelects((row as OptGroupRowData).children);
10851090
} else {
@@ -1137,7 +1142,7 @@ export class MultipleSelectInstance {
11371142
}
11381143

11391144
protected _checkAll(checked: boolean, ignoreUpdate?: boolean) {
1140-
for (const row of this.data) {
1145+
for (const row of this.data || []) {
11411146
if (row.type === 'optgroup') {
11421147
this._checkGroup(row, checked, true);
11431148
} else if (!row.disabled && !row.divider && (ignoreUpdate || row.visible)) {
@@ -1171,7 +1176,7 @@ export class MultipleSelectInstance {
11711176
if (this.options.single) {
11721177
return;
11731178
}
1174-
for (const row of this.data) {
1179+
for (const row of this.data || []) {
11751180
if (row.type === 'optgroup') {
11761181
for (const child of (row as OptGroupRowData).children) {
11771182
if (child) {
@@ -1215,7 +1220,7 @@ export class MultipleSelectInstance {
12151220
}
12161221
this.filterText = text;
12171222

1218-
for (const row of this.data) {
1223+
for (const row of this.data || []) {
12191224
if (row.type === 'optgroup') {
12201225
if (this.options.filterGroup) {
12211226
const rowLabel = `${row?.label ?? ''}`;

lib/src/services/virtual-scroll.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ export class VirtualScroll {
9999

100100
getNum() {
101101
this.scrollTop = this.scrollEl.scrollTop;
102-
return Math.floor(this.scrollTop / (this.clusterHeight! - this.blockHeight!)) || 0;
102+
const blockSize = (this.clusterHeight || 0) - (this.blockHeight || 0);
103+
if (blockSize) {
104+
return Math.floor(this.scrollTop / blockSize) || 0;
105+
}
106+
return 0;
103107
}
104108

105109
initData(rows: string[], num: number) {

0 commit comments

Comments
 (0)