@@ -293,7 +293,7 @@ const FilterBar = forwardRef<HTMLDivElement, FilterBarPropTypes>((props, ref) =>
293293 const debouncedObserverFn = debounce ( ( [ area ] : ResizeObserverEntry [ ] ) => {
294294 const firstChild = area . target ?. children ?. [ 0 ] as HTMLDivElement ;
295295 if ( firstChild ) {
296- setFirstChildWidth ( firstChild . offsetWidth + 16 /*margin*/ ) ;
296+ setFirstChildWidth ( firstChild . getBoundingClientRect ( ) . width + 16 /*margin*/ ) ;
297297 }
298298 } , 100 ) ;
299299 const filterAreaObserver = new ResizeObserver ( debouncedObserverFn ) ;
@@ -338,17 +338,19 @@ const FilterBar = forwardRef<HTMLDivElement, FilterBarPropTypes>((props, ref) =>
338338
339339 // calculates the number of spacers depending on the available width inside the row
340340 const renderSpacers = ( ) => {
341- if ( firstChildWidth && filterAreaWidth && filterBarButtonsWidth ) {
342- const spacers = [ ] ;
343- const filterItemsWidth = calculatedChildren . length * firstChildWidth ;
344- //early return if enough space is available
345- if ( filterAreaWidth - filterBarButtonsWidth > filterItemsWidth ) {
341+ if ( firstChildWidth && filterAreaWidth ) {
342+ const totalItems = calculatedChildren . length + ( search ? 1 : 0 ) ;
343+ const itemsPerRow = Math . round ( filterAreaWidth / firstChildWidth ) ;
344+
345+ if ( totalItems <= itemsPerRow ) {
346346 return null ;
347347 }
348- const usedSpaceLastRow = filterItemsWidth % filterAreaWidth ;
349- const emptySpaceLastRow = filterAreaWidth - usedSpaceLastRow ;
350- // deduct width of buttons container of the empty space in the last row to calculate number of spacers (-1 because of "lastSpacer")
351- const numberOfSpacers = Math . floor ( ( emptySpaceLastRow - filterBarButtonsWidth ) / firstChildWidth ) - 1 ;
348+
349+ const itemsInLastRow = totalItems % itemsPerRow || itemsPerRow ;
350+ // -1 because "lastSpacer" already occupies one flex slot
351+ const numberOfSpacers = Math . max ( 0 , itemsPerRow - itemsInLastRow - 1 ) ;
352+
353+ const spacers = [ ] ;
352354 for ( let i = 0 ; i < numberOfSpacers ; i ++ ) {
353355 spacers . push ( < div key = { `filter-spacer-${ i } ` } className = { classNames . spacer } /> ) ;
354356 }
0 commit comments