Skip to content

Commit ca86409

Browse files
Merge pull request #327 from dd3tech/fix/single-select
Fix(SingleSelect): Different validations were added
2 parents 186bb5a + 5dfefec commit ca86409

1 file changed

Lines changed: 32 additions & 29 deletions

File tree

src/components/Form/SingleSelect.tsx

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ function SingleSelect({
8282
const [selectedOption, setSelectedOption] = useState<ISelectOption | null>(
8383
null
8484
)
85-
const [newValue, setNewValue] = useState(value)
86-
const [options, setOptions] = useState<ISelectOption[]>(optionsList)
8785

8886
const handleClick = () => {
8987
if (isDisabled) return
@@ -112,22 +110,30 @@ function SingleSelect({
112110
const handleSelect = (option: ISelectOption) => {
113111
if (option.disabled) return
114112

115-
const updatedOptions = options.map((opt) =>
116-
opt.value === option.value
117-
? { ...opt, selected: true }
118-
: { ...opt, selected: false }
119-
)
113+
const originalOption = optionsList.find((opt) => opt.value === option.value)
120114

121-
setOptions(updatedOptions)
122-
setSelectedOption(option)
115+
setSelectedOption(originalOption || option)
123116
setIsOpen(false)
124117

125-
onChangeSelect?.(option)
118+
onChangeSelect?.(originalOption || option)
126119
}
127120

128-
useEffect(() => {
129-
setOptions(optionsList)
130-
}, [optionsList])
121+
const optionsWithSelected = optionsList.map((opt) => ({
122+
...opt,
123+
selected: opt.value === value
124+
}))
125+
126+
const getInputLabel = () => {
127+
let inputLabel = label
128+
if (!isFilter && !selectedOption) return ''
129+
if (!selectedOption) return inputLabel
130+
131+
return selectedOption.value === ''
132+
? inputLabel
133+
: (inputLabel =
134+
optionsList.find((opt) => opt.value === selectedOption.value)
135+
?.label || label)
136+
}
131137

132138
useEffect(() => {
133139
const handleClickOutside = (e: globalThis.MouseEvent) => {
@@ -143,17 +149,9 @@ function SingleSelect({
143149
}, [])
144150

145151
useEffect(() => {
146-
if (!newValue) return
147-
setOptions(
148-
optionsList.map((option) =>
149-
option.value === value ? { ...option, selected: true } : option
150-
)
151-
)
152-
}, [])
153-
154-
useEffect(() => {
155-
setNewValue(value)
156-
}, [value])
152+
const found = optionsList.find((opt) => opt.value === value)
153+
setSelectedOption(found || null)
154+
}, [value, optionsList])
157155

158156
return (
159157
<div
@@ -166,7 +164,10 @@ function SingleSelect({
166164
role="select-container"
167165
className={composeClasses(
168166
styles.container,
169-
isFilter && !!selectedOption && 'bg-blue-100 border border-blue-600'
167+
isFilter &&
168+
!!selectedOption &&
169+
selectedOption.value !== '' &&
170+
'bg-blue-100 border border-blue-600'
170171
)}
171172
style={{ zIndex: 2, ...style }}
172173
>
@@ -180,7 +181,7 @@ function SingleSelect({
180181
)}
181182
<input
182183
placeholder={isFilter ? placeholder : ''}
183-
value={selectedOption?.label || newValue}
184+
value={getInputLabel()}
184185
{...otherProps}
185186
className={composeClasses(
186187
'outline-none w-full bg-transparent truncate text-sm',
@@ -223,14 +224,14 @@ function SingleSelect({
223224
</Text>
224225
</div>
225226
)}
226-
{options.map((option) => {
227+
{optionsWithSelected.map((option) => {
227228
const { value, label, disabled } = option
228229

229230
return (
230231
<Flex
231232
className={composeClasses(
232233
'w-full px-2 py-2 hover:bg-blue-50 hover:text-blue-800',
233-
option.selected && 'text-blue-700'
234+
option.selected && isFilter && 'text-blue-700'
234235
)}
235236
justifyContent="between"
236237
alignItems="center"
@@ -250,7 +251,9 @@ function SingleSelect({
250251
{label ?? value}
251252
</button>
252253

253-
{option.selected && <CheckCircleIcon width={20} />}
254+
{option.selected && isFilter && (
255+
<CheckCircleIcon width={20} />
256+
)}
254257
</Flex>
255258
)
256259
})}

0 commit comments

Comments
 (0)