@@ -969,10 +969,42 @@ class MultiSelectComboBox extends ResizeMixin(InputControlMixin(ThemableMixin(El
969
969
970
970
const chipMinWidth = parseInt ( getComputedStyle ( this ) . getPropertyValue ( '--_chip-min-width' ) ) ;
971
971
972
- // Detect if the field can expand
973
972
if ( this . autoExpandHorizontally ) {
974
- const extraWidth = this . __getExtraWidth ( ) ;
975
- remainingWidth += Math . max ( 0 , extraWidth ) ;
973
+ const chips = [ ] ;
974
+
975
+ // First, add all chips to make the field fully expand
976
+ for ( let i = items . length - 1 , refNode = null ; i >= 0 ; i -- ) {
977
+ const chip = this . __createChip ( items [ i ] ) ;
978
+ this . insertBefore ( chip , refNode ) ;
979
+ refNode = chip ;
980
+ chips . unshift ( chip ) ;
981
+ }
982
+
983
+ const overflowItems = [ ] ;
984
+ const availableWidth = this . _inputField . $ . wrapper . clientWidth - this . $ . chips . clientWidth ;
985
+
986
+ // When auto expanding vertically, no need to measure width
987
+ if ( ! this . autoExpandVertically && availableWidth < inputWidth ) {
988
+ // Always show at least last item as a chip
989
+ while ( chips . length > 1 ) {
990
+ const lastChip = chips . pop ( ) ;
991
+ lastChip . remove ( ) ;
992
+ overflowItems . unshift ( items . pop ( ) ) ;
993
+
994
+ // Remove chips until there is enough width for the input element to fit
995
+ const neededWidth = overflowItems . length > 0 ? inputWidth + this . __getOverflowWidth ( ) : inputWidth ;
996
+ if ( this . _inputField . $ . wrapper . clientWidth - this . $ . chips . clientWidth >= neededWidth ) {
997
+ break ;
998
+ }
999
+ }
1000
+
1001
+ if ( chips . length === 1 ) {
1002
+ chips [ 0 ] . style . maxWidth = `${ Math . max ( chipMinWidth , remainingWidth ) } px` ;
1003
+ }
1004
+ }
1005
+
1006
+ this . _overflowItems = overflowItems ;
1007
+ return ;
976
1008
}
977
1009
978
1010
// Add chips until remaining width is exceeded
@@ -998,27 +1030,6 @@ class MultiSelectComboBox extends ResizeMixin(InputControlMixin(ThemableMixin(El
998
1030
this . _overflowItems = items ;
999
1031
}
1000
1032
1001
- /** @private */
1002
- __getExtraWidth ( ) {
1003
- // Check if max-width is set on the host
1004
- const maxWidth = parseInt ( getComputedStyle ( this ) . maxWidth ) ;
1005
- if ( ! isNaN ( maxWidth ) ) {
1006
- return maxWidth - this . clientWidth ;
1007
- }
1008
-
1009
- // Check if width is set using style attribute
1010
- const width = parseInt ( this . style . width ) ;
1011
- if ( ! isNaN ( width ) ) {
1012
- return width - this . clientWidth ;
1013
- }
1014
-
1015
- // Check if parent element allows to expand
1016
- const parent = this . parentNode ;
1017
- const host = parent instanceof ShadowRoot ? parent . host : parent ;
1018
-
1019
- return host . clientWidth - this . clientWidth ;
1020
- }
1021
-
1022
1033
/** @private */
1023
1034
__updateOverflowChip ( overflow , items , disabled , readonly ) {
1024
1035
if ( overflow ) {
0 commit comments