Skip to content

Commit 9f07297

Browse files
committed
feat: add input value retention in select picker for multi select
1 parent 8de5bd0 commit 9f07297

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

src/Shared/Components/SelectPicker/FilterSelectPicker.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const FilterSelectPicker = ({
2222
...props
2323
}: FilterSelectPickerProps) => {
2424
const [isMenuOpen, setIsMenuOpen] = useState(false)
25-
const [inputValue, setInputValue] = useState('')
2625

2726
const [selectedOptions, setSelectedOptions] = useState<SelectPickerOptionType[]>(
2827
structuredClone(appliedFilterOptions ?? []),
@@ -50,7 +49,7 @@ const FilterSelectPicker = ({
5049
}
5150

5251
const handleSelectOnChange: SelectPickerProps<number | string, true>['onChange'] = (selectedOptionsToUpdate) => {
53-
setInputValue(inputValue)
52+
// setInputValue(inputValue)
5453
setSelectedOptions(structuredClone(selectedOptionsToUpdate) as SelectPickerOptionType[])
5554
}
5655

@@ -111,8 +110,6 @@ const FilterSelectPicker = ({
111110
isClearable={false}
112111
customSelectedOptionsCount={appliedFiltersCount}
113112
icon={filterIcon}
114-
inputValue={inputValue}
115-
onInputChange={setInputValue}
116113
/>
117114
</div>
118115
)

src/Shared/Components/SelectPicker/SelectPicker.component.tsx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { ReactComponent as ICInfoFilledOverride } from '@Icons/ic-info-filled-ov
3030
import { ComponentSizeType } from '@Shared/constants'
3131
import { ConditionalWrap } from '@Common/Helper'
3232
import Tippy from '@tippyjs/react'
33+
import { isNullOrUndefined } from '@Shared/Helpers'
3334
import { getCommonSelectStyle, getSelectPickerOptionByValue } from './utils'
3435
import {
3536
SelectPickerMultiValueLabel,
@@ -208,8 +209,6 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
208209
fullWidth = false,
209210
customSelectedOptionsCount = null,
210211
renderMenuListFooter,
211-
inputValue,
212-
onInputChange,
213212
isCreatable = false,
214213
onCreateOption,
215214
closeMenuOnSelect = false,
@@ -218,6 +217,8 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
218217
...props
219218
}: SelectPickerProps<OptionValue, IsMulti>) => {
220219
const [isFocussed, setIsFocussed] = useState(false)
220+
const [inputValue, setInputValue] = useState('')
221+
221222
const {
222223
inputId,
223224
required,
@@ -331,6 +332,15 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
331332
}
332333
}
333334

335+
const handleInputChange: ReactSelectProps['onInputChange'] = (updatedInputValue, actionMeta) => {
336+
// If onInputChange is provided, then the input state should be controlled externally
337+
if (props.onInputChange) {
338+
props.onInputChange(updatedInputValue, actionMeta)
339+
return
340+
}
341+
setInputValue(updatedInputValue)
342+
}
343+
334344
const handleKeyDown: ReactSelectProps['onKeyDown'] = (e) => {
335345
// Prevent the option from being selected if meta or control key is pressed
336346
if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') {
@@ -349,6 +359,15 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
349359
props.onBlur?.(e)
350360
}
351361

362+
const handleChange: ReactSelectProps<SelectPickerOptionType<OptionValue>, IsMulti>['onChange'] = (...params) => {
363+
// Retain the input value on selection change
364+
if (isMulti && isNullOrUndefined(props.inputValue)) {
365+
setInputValue(inputValue)
366+
}
367+
368+
props.onChange?.(...params)
369+
}
370+
352371
const commonProps = useMemo(
353372
() => ({
354373
name: name || inputId,
@@ -435,14 +454,15 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
435454
createOptionPosition="first"
436455
onCreateOption={handleCreateOption}
437456
renderMenuListFooter={!optionListError && renderMenuListFooter}
438-
inputValue={inputValue}
439-
onInputChange={onInputChange}
457+
inputValue={props.inputValue ?? inputValue}
458+
onInputChange={handleInputChange}
440459
icon={icon}
441460
showSelectedOptionIcon={shouldShowSelectedOptionIcon}
442461
onKeyDown={handleKeyDown}
443462
customDisplayText={customDisplayText}
444463
onFocus={handleFocus}
445464
onBlur={handleBlur}
465+
onChange={handleChange}
446466
controlShouldRenderValue={controlShouldRenderValue}
447467
/>
448468
</div>

src/Shared/Components/SelectPicker/common.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,11 @@ export const SelectPickerMultiValueLabel = <OptionValue, IsMulti extends boolean
286286
}
287287

288288
export const SelectPickerMultiValueRemove = (props: MultiValueRemoveProps) => (
289-
<components.MultiValueLabel {...props}>
289+
<components.MultiValueRemove {...props}>
290290
<span className="flex dc__no-shrink">
291291
<ICClose className="icon-dim-12 icon-use-fill-n6" />
292292
</span>
293-
</components.MultiValueLabel>
293+
</components.MultiValueRemove>
294294
)
295295

296296
export const SelectPickerGroupHeading = <OptionValue,>({

0 commit comments

Comments
 (0)