@@ -15,6 +15,7 @@ import {
15
15
createDomElement ,
16
16
emptyElement ,
17
17
findParent ,
18
+ getComputedSize ,
18
19
getOffset ,
19
20
getSize ,
20
21
insertAfter ,
@@ -1981,31 +1982,49 @@ export class MultipleSelectInstance {
1981
1982
const parentWidth = this . parentElm . scrollWidth ;
1982
1983
1983
1984
// keep the dropWidth/width as reference, if our new calculated width is below then we will re-adjust (else do nothing)
1984
- let currentDefinedWidth : number | string = parentWidth ;
1985
+ let selectParentWidth : number | string = parentWidth ;
1985
1986
if ( this . options . dropWidth || this . options . width ) {
1986
- currentDefinedWidth = this . options . dropWidth || this . options . width || 0 ;
1987
+ selectParentWidth = this . options . dropWidth || this . options . width || 0 ;
1987
1988
}
1988
1989
1989
1990
// calculate the "Select All" element width, this text is configurable which is why we recalculate every time
1990
1991
const selectAllSpanElm = this . dropElm . querySelector < HTMLSpanElement > ( '.ms-select-all span' ) ;
1991
1992
const dropUlElm = this . dropElm . querySelector < HTMLUListElement > ( 'ul' ) ;
1992
1993
1993
1994
if ( dropUlElm ) {
1994
- const liPadding = 26 ; // there are multiple padding involved, let's fix it at 26px
1995
-
1996
- const selectAllElmWidth = selectAllSpanElm ?. clientWidth ?? 0 + liPadding ;
1995
+ let contentWidth = 0 ;
1996
+ let allPaddingSizes = 0 ; // calculate all possible ul>li>label paddings
1997
+ const selectAllElmWidth = ( selectAllSpanElm ?. clientWidth ?? 0 ) + allPaddingSizes ;
1997
1998
const hasScrollbar = dropUlElm . scrollHeight > dropUlElm . clientHeight ;
1998
1999
const scrollbarWidth = hasScrollbar ? this . getScrollbarWidth ( ) : 0 ;
1999
- let contentWidth = 0 ;
2000
2000
2001
- this . dropElm . querySelectorAll ( 'li label' ) . forEach ( elm => {
2002
- if ( elm . scrollWidth > contentWidth ) {
2003
- contentWidth = elm . scrollWidth ;
2001
+ // 1. find first item that has text value and measure all paddings
2002
+ allPaddingSizes += getComputedSize ( dropUlElm , 'paddingLeft' ) * 2 ;
2003
+
2004
+ let foundFilledItem = false ;
2005
+ for ( const liElm of Array . from ( dropUlElm . querySelectorAll < HTMLLIElement > ( 'li' ) ) ) {
2006
+ const labelElm = liElm . querySelector < HTMLLabelElement > ( 'label' ) ;
2007
+ const iconElm = liElm . querySelector < HTMLLabelElement > ( '.icon-checkbox-container' ) ;
2008
+ const spanElm = liElm . querySelector < HTMLSpanElement > ( 'span' ) ;
2009
+ if ( labelElm && spanElm ?. textContent ) {
2010
+ // find first item that has value and calculate its padding sizes
2011
+ if ( ! foundFilledItem ) {
2012
+ allPaddingSizes += getComputedSize ( liElm , 'paddingLeft' ) * 2 ;
2013
+ allPaddingSizes += getComputedSize ( labelElm , 'paddingLeft' ) * 2 ;
2014
+ allPaddingSizes += getComputedSize ( spanElm , 'paddingLeft' ) ;
2015
+ allPaddingSizes += iconElm ?. offsetWidth ?? 0 ;
2016
+ foundFilledItem = true ;
2017
+ }
2018
+
2019
+ // keep ref of the widest text width
2020
+ if ( spanElm . offsetWidth > contentWidth ) {
2021
+ contentWidth = spanElm . offsetWidth ;
2022
+ }
2004
2023
}
2005
- } ) ;
2024
+ }
2006
2025
2007
2026
// add a padding & include the browser scrollbar width
2008
- contentWidth += liPadding + scrollbarWidth ;
2027
+ contentWidth += allPaddingSizes + scrollbarWidth ;
2009
2028
2010
2029
// make sure the new calculated width is wide enough to include the "Select All" text (this text is configurable)
2011
2030
if ( contentWidth < selectAllElmWidth ) {
@@ -2022,10 +2041,10 @@ export class MultipleSelectInstance {
2022
2041
contentWidth = this . options . minWidth ;
2023
2042
}
2024
2043
2025
- // finally re-adjust the drop to the new calculated width when necessary
2026
- if ( currentDefinedWidth === '100%' || + currentDefinedWidth < contentWidth ) {
2044
+ // finally if text is wider than select container, then re-adjust the drop to the new calculated width
2045
+ if ( selectParentWidth === '100%' || + selectParentWidth < contentWidth ) {
2046
+ this . dropElm . style . minWidth = 'auto' ;
2027
2047
this . dropElm . style . width = `${ contentWidth } px` ;
2028
- this . dropElm . style . maxWidth = `${ contentWidth } px` ;
2029
2048
}
2030
2049
}
2031
2050
}
0 commit comments