Skip to content

Commit 740aa23

Browse files
authored
Picker - multi select - fix behavior when the value is changed by user (#2010)
* Picker - multi select - fix behavior when the value is changed by user * Apply only to multi
1 parent dd03625 commit 740aa23

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/components/picker/helpers/usePickerSelection.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-
import {RefObject, useCallback, useState} from 'react';
1+
import {RefObject, useCallback, useState, useEffect} from 'react';
22
import _ from 'lodash';
3-
import {PickerProps, PickerValue, PickerSingleValue, PickerMultiValue} from '../types';
3+
import {PickerProps, PickerValue, PickerSingleValue, PickerMultiValue, PickerModes} from '../types';
44

55
interface UsePickerSelectionProps
6-
extends Pick<PickerProps, 'migrate' | 'value' | 'onChange' | 'getItemValue' | 'topBarProps'> {
6+
extends Pick<PickerProps, 'migrate' | 'value' | 'onChange' | 'getItemValue' | 'topBarProps' | 'mode'> {
77
pickerExpandableRef: RefObject<any>;
88
setSearchValue: (searchValue: string) => void;
99
}
1010

1111
const usePickerSelection = (props: UsePickerSelectionProps) => {
12-
const {migrate, value, onChange, topBarProps, pickerExpandableRef, getItemValue, setSearchValue} = props;
12+
const {migrate, value, onChange, topBarProps, pickerExpandableRef, getItemValue, setSearchValue, mode} = props;
1313
const [multiDraftValue, setMultiDraftValue] = useState(value as PickerMultiValue);
1414
const [multiFinalValue, setMultiFinalValue] = useState(value as PickerMultiValue);
1515

16+
useEffect(() => {
17+
if (mode === PickerModes.MULTI && multiFinalValue !== value) {
18+
setMultiDraftValue(value as PickerMultiValue);
19+
setMultiFinalValue(value as PickerMultiValue);
20+
}
21+
}, [value]);
22+
1623
const onDoneSelecting = useCallback((item: PickerValue) => {
1724
setSearchValue('');
1825
setMultiFinalValue(item as PickerMultiValue);

src/components/picker/new.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ const Picker = (props: PropsWithChildren<PickerProps> & ForwardRefInjectedProps
8686
pickerExpandableRef: pickerExpandable,
8787
getItemValue,
8888
topBarProps,
89-
setSearchValue
89+
setSearchValue,
90+
mode
9091
});
9192

9293
const {label, accessibilityInfo} = usePickerLabel({

0 commit comments

Comments
 (0)