Skip to content

Commit 98d8c88

Browse files
authored
fix: select bar on first render (#5493)
1 parent 4a0764e commit 98d8c88

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

.changeset/little-dogs-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ultraviolet/ui": patch
3+
---
4+
5+
`SelectInput`: should display values correctly on first render

packages/ui/src/components/SelectInput/SelectBar.tsx

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ import {
99
PlusIcon,
1010
} from '@ultraviolet/icons'
1111
import type { ReactNode, RefObject } from 'react'
12-
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
12+
import {
13+
useCallback,
14+
useEffect,
15+
useLayoutEffect,
16+
useMemo,
17+
useRef,
18+
useState,
19+
} from 'react'
1320
import { Button } from '../Button'
1421
import { Stack } from '../Stack'
1522
import { Tag } from '../Tag'
@@ -340,11 +347,6 @@ const SelectBar = ({
340347
const measureRef = useRef<HTMLDivElement>(null)
341348
const arrowRef = useRef<HTMLDivElement>(null)
342349
const refPlusTag = useRef<HTMLDivElement>(null)
343-
// width - width of the arrow (in px) - padding between tags (in px)
344-
const [innerWidth, setInnerWidth] = useState(
345-
innerRef.current?.offsetWidth ??
346-
0 - (arrowRef.current?.offsetWidth ?? 0) - SIZES_TAG.paddings,
347-
)
348350
const [overflowAmount, setOverflowAmount] = useState(0)
349351
const [overflow, setOverflow] = useState(false)
350352
const [lastElementMaxWidth, setLastElementMaxWidth] = useState(0)
@@ -387,6 +389,16 @@ const SelectBar = ({
387389
setDisplayShadowCopy(true)
388390
}, [selectedData.selectedValues.length])
389391

392+
const getWidth = useCallback(() => {
393+
if (refTag.current) {
394+
return refTag.current.offsetWidth
395+
}
396+
397+
return (
398+
innerRef.current?.offsetWidth ??
399+
0 - (arrowRef.current?.offsetWidth ?? 0) - SIZES_TAG.paddings
400+
)
401+
}, [innerRef.current?.offsetWidth])
390402
// We then want to measure the tags length before displaying them
391403
// so we can determine if there is an overflow or not
392404
// We use useLayoutEffect to ensure the measurement is done before the browser paints
@@ -399,6 +411,7 @@ const SelectBar = ({
399411
if (measureRef.current && selectedData.selectedValues.length > 0) {
400412
const toMeasureElements: HTMLCollection = measureRef.current.children
401413
const toMeasureElementsArray = [...toMeasureElements]
414+
const innerWidth = getWidth()
402415

403416
const {
404417
measuredVisibleTags,
@@ -506,32 +519,15 @@ const SelectBar = ({
506519
setDisplayShadowCopy(false)
507520
}, [
508521
displayShadowCopy,
509-
innerWidth,
510522
potentiallyNonOverflowedValues,
511523
selectedData.selectedValues.length,
524+
getWidth,
512525
])
513526

514527
useEffect(() => {
515528
setSelectedData({ type: 'update' })
516529
}, [setSelectedData, options])
517530

518-
useEffect(() => {
519-
const getWidth = () => {
520-
if (refTag.current) {
521-
setInnerWidth(refTag.current.offsetWidth)
522-
} else {
523-
setInnerWidth(
524-
innerRef.current?.offsetWidth ??
525-
0 - (arrowRef.current?.offsetWidth ?? 0) - SIZES_TAG.paddings,
526-
)
527-
}
528-
}
529-
getWidth()
530-
window.addEventListener('resize', getWidth)
531-
532-
return () => window.removeEventListener('resize', getWidth)
533-
}, [innerRef, refTag, selectedData.selectedValues])
534-
535531
const shouldDisplayValues = useMemo(() => {
536532
if (multiselect) {
537533
return (

0 commit comments

Comments
 (0)