@@ -168,7 +168,10 @@ function Picker({
168
168
const itemPositionsRef = useRef ( { } ) ;
169
169
const flatListRef = useRef ( ) ;
170
170
const scrollViewRef = useRef ( ) ;
171
- const valueRef = useRef ( null ) ;
171
+ const memoryRef = useRef ( {
172
+ value : null ,
173
+ items : [ ]
174
+ } ) ;
172
175
173
176
const THEME = useMemo ( ( ) => THEMES [ theme ] . default , [ theme ] ) ;
174
177
const ICON = useMemo ( ( ) => THEMES [ theme ] . ICONS , [ theme ] )
@@ -185,6 +188,8 @@ function Picker({
185
188
useEffect ( ( ) => {
186
189
// LogBox.ignoreLogs(['VirtualizedLists should never be nested']);
187
190
191
+ memoryRef . current . value = multiple ? ( Array . isArray ( value ) ? value : [ ] ) : value ;
192
+
188
193
// Get initial seleted items
189
194
let initialSelectedItems = [ ] ;
190
195
const valueNotNull = value !== null && ( Array . isArray ( value ) && value . length !== 0 ) ;
@@ -284,12 +289,19 @@ function Picker({
284
289
} , [ value , items ] ) ;
285
290
286
291
/**
287
- * Keep a ref of the value .
292
+ * Update value in the memory .
288
293
*/
289
294
useEffect ( ( ) => {
290
- valueRef . current = value ;
295
+ memoryRef . current . value = value ;
291
296
} , [ value ] ) ;
292
297
298
+ /**
299
+ * Update items in the memory.
300
+ */
301
+ useEffect ( ( ) => {
302
+ memoryRef . current . items = necessaryItems ;
303
+ } , [ necessaryItems ] ) ;
304
+
293
305
/**
294
306
* Automatically scroll to the first selected item.
295
307
*/
@@ -373,12 +385,12 @@ function Picker({
373
385
const scroll = useCallback ( ( ) => {
374
386
setTimeout ( ( ) => {
375
387
if ( ( scrollViewRef . current || flatListRef . current ) ) {
376
- const isArray = Array . isArray ( valueRef . current ) ;
388
+ const isArray = Array . isArray ( memoryRef . current . value ) ;
377
389
378
- if ( valueRef . current === null || ( isArray && valueRef . current . length === 0 ) )
390
+ if ( memoryRef . current . value === null || ( isArray && memoryRef . current . value . length === 0 ) )
379
391
return ;
380
392
381
- const value = isArray ? valueRef . current [ 0 ] : valueRef . current ;
393
+ const value = isArray ? memoryRef . current . value [ 0 ] : memoryRef . current . value ;
382
394
383
395
if ( scrollViewRef . current && itemPositionsRef . current . hasOwnProperty ( value ) ) {
384
396
scrollViewRef . current ?. scrollTo ?. ( {
@@ -1188,8 +1200,20 @@ function Picker({
1188
1200
}
1189
1201
1190
1202
// 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 {
1192
1215
onSelectItem ( item ) ;
1216
+ }
1193
1217
1194
1218
setValue ( state => {
1195
1219
if ( multiple ) {
0 commit comments