Skip to content

Commit aad8f64

Browse files
authored
fix: make sure select drop list isn't null when adjusting drop width (#364)
1 parent 9d5ffd7 commit aad8f64

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

packages/multiple-select-vanilla/src/MultipleSelectInstance.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,43 +1949,45 @@ export class MultipleSelectInstance {
19491949

19501950
// calculate the "Select All" element width, this text is configurable which is why we recalculate every time
19511951
const selectAllSpanElm = this.dropElm.querySelector<HTMLSpanElement>('.ms-select-all span');
1952-
const dropUlElm = this.dropElm.querySelector('ul') as HTMLUListElement;
1952+
const dropUlElm = this.dropElm.querySelector<HTMLUListElement>('ul');
19531953

1954-
const liPadding = 26; // there are multiple padding involved, let's fix it at 26px
1954+
if (dropUlElm) {
1955+
const liPadding = 26; // there are multiple padding involved, let's fix it at 26px
19551956

1956-
const selectAllElmWidth = selectAllSpanElm?.clientWidth ?? 0 + liPadding;
1957-
const hasScrollbar = dropUlElm.scrollHeight > dropUlElm.clientHeight;
1958-
const scrollbarWidth = hasScrollbar ? this.getScrollbarWidth() : 0;
1959-
let contentWidth = 0;
1957+
const selectAllElmWidth = selectAllSpanElm?.clientWidth ?? 0 + liPadding;
1958+
const hasScrollbar = dropUlElm.scrollHeight > dropUlElm.clientHeight;
1959+
const scrollbarWidth = hasScrollbar ? this.getScrollbarWidth() : 0;
1960+
let contentWidth = 0;
19601961

1961-
this.dropElm.querySelectorAll('li label').forEach(elm => {
1962-
if (elm.scrollWidth > contentWidth) {
1963-
contentWidth = elm.scrollWidth;
1964-
}
1965-
});
1962+
this.dropElm.querySelectorAll('li label').forEach(elm => {
1963+
if (elm.scrollWidth > contentWidth) {
1964+
contentWidth = elm.scrollWidth;
1965+
}
1966+
});
19661967

1967-
// add a padding & include the browser scrollbar width
1968-
contentWidth += liPadding + scrollbarWidth;
1968+
// add a padding & include the browser scrollbar width
1969+
contentWidth += liPadding + scrollbarWidth;
19691970

1970-
// make sure the new calculated width is wide enough to include the "Select All" text (this text is configurable)
1971-
if (contentWidth < selectAllElmWidth) {
1972-
contentWidth = selectAllElmWidth;
1973-
}
1971+
// make sure the new calculated width is wide enough to include the "Select All" text (this text is configurable)
1972+
if (contentWidth < selectAllElmWidth) {
1973+
contentWidth = selectAllElmWidth;
1974+
}
19741975

1975-
// if a maxWidth is defined, make sure our new calculate width is not over the maxWidth
1976-
if (this.options.maxWidth && contentWidth > this.options.maxWidth) {
1977-
contentWidth = this.options.maxWidth;
1978-
}
1976+
// if a maxWidth is defined, make sure our new calculate width is not over the maxWidth
1977+
if (this.options.maxWidth && contentWidth > this.options.maxWidth) {
1978+
contentWidth = this.options.maxWidth;
1979+
}
19791980

1980-
// if a minWidth is defined, make sure our new calculate width is not below the minWidth
1981-
if (this.options.minWidth && contentWidth < this.options.minWidth) {
1982-
contentWidth = this.options.minWidth;
1983-
}
1981+
// if a minWidth is defined, make sure our new calculate width is not below the minWidth
1982+
if (this.options.minWidth && contentWidth < this.options.minWidth) {
1983+
contentWidth = this.options.minWidth;
1984+
}
19841985

1985-
// finally re-adjust the drop to the new calculated width when necessary
1986-
if (currentDefinedWidth === '100%' || +currentDefinedWidth < contentWidth) {
1987-
this.dropElm.style.width = `${contentWidth}px`;
1988-
this.dropElm.style.maxWidth = `${contentWidth}px`;
1986+
// finally re-adjust the drop to the new calculated width when necessary
1987+
if (currentDefinedWidth === '100%' || +currentDefinedWidth < contentWidth) {
1988+
this.dropElm.style.width = `${contentWidth}px`;
1989+
this.dropElm.style.maxWidth = `${contentWidth}px`;
1990+
}
19891991
}
19901992
}
19911993
}

0 commit comments

Comments
 (0)