Skip to content

Commit 1bae5b2

Browse files
Merge pull request #323 from dd3tech/fix/single-select-options
Fix(SingleSelect): Update state options
2 parents 58fb9d0 + 3a4210c commit 1bae5b2

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/components/Form/SingleSelect.tsx

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

8788
const handleClick = () => {
@@ -124,6 +125,10 @@ function SingleSelect({
124125
onChangeSelect?.(option)
125126
}
126127

128+
useEffect(() => {
129+
setOptions(optionsList)
130+
}, [optionsList])
131+
127132
useEffect(() => {
128133
const handleClickOutside = (e: globalThis.MouseEvent) => {
129134
if (selectRef.current && !selectRef.current.contains(e.target as Node)) {
@@ -138,14 +143,18 @@ function SingleSelect({
138143
}, [])
139144

140145
useEffect(() => {
141-
if (!value) return
146+
if (!newValue) return
142147
setOptions(
143148
optionsList.map((option) =>
144149
option.value === value ? { ...option, selected: true } : option
145150
)
146151
)
147152
}, [])
148153

154+
useEffect(() => {
155+
setNewValue(value)
156+
}, [value])
157+
149158
return (
150159
<div
151160
ref={selectRef}
@@ -171,7 +180,7 @@ function SingleSelect({
171180
)}
172181
<input
173182
placeholder={isFilter ? placeholder : ''}
174-
value={selectedOption?.label}
183+
value={selectedOption?.label || newValue}
175184
{...otherProps}
176185
className={composeClasses(
177186
'outline-none w-full bg-transparent truncate text-sm',

0 commit comments

Comments
 (0)