Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions src/components/Form/SingleSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,28 @@ function SingleSelect({
const handleSelect = (option: ISelectOption) => {
if (option.disabled) return

const originalOption = optionsList.find((opt) => opt.value === option.value)
const originalOption = optionsList?.find(
(opt) => String(opt?.value) === String(option?.value)
)

setSelectedOption(originalOption || option)
setIsOpen(false)

onChangeSelect?.(originalOption || option)
}

const optionsWithSelected = optionsList.map((opt) => ({
const optionsWithSelected = optionsList?.map((opt) => ({
...opt,
selected: opt.value === value
selected:
String(opt?.value) === String(value) ||
String(opt?.label).toLowerCase() === String(value).toLowerCase()
}))

const getInputLabel = () => {
let inputLabel = label
if (!isFilter && !selectedOption) return ''
if (!selectedOption) return inputLabel
if (!isFilter && !selectedOption) return placeholder || label || ''
if (!selectedOption) return placeholder || label || ''

return selectedOption.value === ''
? inputLabel
: (inputLabel =
optionsList.find((opt) => opt.value === selectedOption.value)
?.label || label)
return selectedOption.label || selectedOption.value.toString()
}

useEffect(() => {
Expand All @@ -149,8 +148,19 @@ function SingleSelect({
}, [])

useEffect(() => {
const found = optionsList.find((opt) => opt.value === value)
setSelectedOption(found || null)
const found = optionsList?.find(
(opt) => String(opt?.value) === String(value)
)

const foundByLabel =
!found && value
? optionsList?.find(
(opt) =>
String(opt?.label).toLowerCase() === String(value).toLowerCase()
)
: null

setSelectedOption(found || foundByLabel || null)
}, [value, optionsList])

return (
Expand Down Expand Up @@ -224,9 +234,8 @@ function SingleSelect({
</Text>
</div>
)}
{optionsWithSelected.map((option) => {
{optionsWithSelected?.map((option) => {
const { value, label, disabled } = option

return (
<Flex
className={composeClasses(
Expand Down