2
0

🐛 Fix select input displaying id instead of label on refresh

Closes #1495
This commit is contained in:
Baptiste Arnaud
2024-05-16 15:02:16 +02:00
parent 8351e20fd3
commit f211a3e29f

View File

@ -15,7 +15,7 @@ import {
IconButton,
HStack,
} from '@chakra-ui/react'
import { useState, useRef, ChangeEvent } from 'react'
import { useState, useRef, ChangeEvent, useEffect } from 'react'
import { isDefined } from '@typebot.io/lib'
import { useOutsideClick } from '@/hooks/useOutsideClick'
import { useParentModal } from '@/features/graph/providers/ParentModalProvider'
@ -23,14 +23,14 @@ import { ChevronDownIcon, CloseIcon } from '../icons'
const dropdownCloseAnimationDuration = 300
type Item =
| string
| {
type RichItem = {
icon?: JSX.Element
label: string
value: string
extras?: Record<string, unknown>
}
}
type Item = string | RichItem
type Props<T extends Item> = {
selectedItem?: string
@ -59,6 +59,18 @@ export const Select = <T extends Item>({
)
)
useEffect(() => {
if (!items || typeof items[0] === 'string' || !selectedItem || isTouched)
return
setInputValue(
getItemLabel(
(items as readonly RichItem[]).find(
(item) => selectedItem === item.value
) ?? selectedItem
)
)
}, [isTouched, items, selectedItem])
const [keyboardFocusIndex, setKeyboardFocusIndex] = useState<
number | undefined
>()