Skip to content

Commit 4222711

Browse files
committed
Add onSelectItem for multiple items.
1 parent 5cbaa4f commit 4222711

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ declare module 'react-native-dropdown-picker' {
269269
interface DropDownPickerMultipleProps {
270270
multiple: true;
271271
onChangeValue?: (value: ValueType[] | null) => void;
272+
onSelectItem?: (items: ItemType[]) => void;
272273
setValue: Dispatch<SetStateAction<ValueType[] | null>>;
273274
value: ValueType[] | null;
274275
}

src/components/Picker.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,10 @@ function Picker({
168168
const itemPositionsRef = useRef({});
169169
const flatListRef = useRef();
170170
const scrollViewRef = useRef();
171-
const valueRef = useRef(null);
171+
const memoryRef = useRef({
172+
value: null,
173+
items: []
174+
});
172175

173176
const THEME = useMemo(() => THEMES[theme].default, [theme]);
174177
const ICON = useMemo(() => THEMES[theme].ICONS, [theme])
@@ -185,6 +188,8 @@ function Picker({
185188
useEffect(() => {
186189
// LogBox.ignoreLogs(['VirtualizedLists should never be nested']);
187190

191+
memoryRef.current.value = multiple ? (Array.isArray(value) ? value : []) : value;
192+
188193
// Get initial seleted items
189194
let initialSelectedItems = [];
190195
const valueNotNull = value !== null && (Array.isArray(value) && value.length !== 0);
@@ -284,12 +289,19 @@ function Picker({
284289
}, [value, items]);
285290

286291
/**
287-
* Keep a ref of the value.
292+
* Update value in the memory.
288293
*/
289294
useEffect(() => {
290-
valueRef.current = value;
295+
memoryRef.current.value = value;
291296
}, [value]);
292297

298+
/**
299+
* Update items in the memory.
300+
*/
301+
useEffect(() => {
302+
memoryRef.current.items = necessaryItems;
303+
}, [necessaryItems]);
304+
293305
/**
294306
* Automatically scroll to the first selected item.
295307
*/
@@ -373,12 +385,12 @@ function Picker({
373385
const scroll = useCallback(() => {
374386
setTimeout(() => {
375387
if ((scrollViewRef.current || flatListRef.current)) {
376-
const isArray = Array.isArray(valueRef.current);
388+
const isArray = Array.isArray(memoryRef.current.value);
377389

378-
if (valueRef.current === null || (isArray && valueRef.current.length === 0))
390+
if (memoryRef.current.value === null || (isArray && memoryRef.current.value.length === 0))
379391
return;
380392

381-
const value = isArray ? valueRef.current[0] : valueRef.current;
393+
const value = isArray ? memoryRef.current.value[0] : memoryRef.current.value;
382394

383395
if (scrollViewRef.current && itemPositionsRef.current.hasOwnProperty(value)) {
384396
scrollViewRef.current?.scrollTo?.({
@@ -1188,8 +1200,20 @@ function Picker({
11881200
}
11891201

11901202
// Not a reliable method for external value changes.
1191-
if (! multiple)
1203+
if (multiple) {
1204+
if (memoryRef.current.value.includes(item[_schema.value])) {
1205+
const index = memoryRef.current.items.findIndex(x => x[_schema.value] === item[_schema.value]);
1206+
1207+
if (index > -1) {
1208+
memoryRef.current.items.splice(index, 1);
1209+
onSelectItem(memoryRef.current.items.slice());
1210+
}
1211+
} else {
1212+
onSelectItem([...memoryRef.current.items, item]);
1213+
}
1214+
} else {
11921215
onSelectItem(item);
1216+
}
11931217

11941218
setValue(state => {
11951219
if (multiple) {

0 commit comments

Comments
 (0)