1
1
import type { ForwardedRef } from 'react' ;
2
- import React , { forwardRef , useEffect , useState } from 'react' ;
2
+ import React , { forwardRef , useEffect , useRef , useState } from 'react' ;
3
3
import * as Expensicons from '@components/Icon/Expensicons' ;
4
4
import MenuItem from '@components/MenuItem' ;
5
5
import Modal from '@components/Modal' ;
@@ -14,10 +14,12 @@ import CONST from '@src/CONST';
14
14
type SelectionListWithModalProps < TItem extends ListItem > = BaseSelectionListProps < TItem > & {
15
15
turnOnSelectionModeOnLongPress ?: boolean ;
16
16
onTurnOnSelectionMode ?: ( item : TItem | null ) => void ;
17
+ shouldAutoTurnOff ?: boolean ;
18
+ isSelected ?: ( item : TItem ) => boolean ;
17
19
} ;
18
20
19
21
function SelectionListWithModal < TItem extends ListItem > (
20
- { turnOnSelectionModeOnLongPress, onTurnOnSelectionMode, onLongPressRow, sections, ...rest } : SelectionListWithModalProps < TItem > ,
22
+ { turnOnSelectionModeOnLongPress, onTurnOnSelectionMode, onLongPressRow, sections, shouldAutoTurnOff , isSelected , ...rest } : SelectionListWithModalProps < TItem > ,
21
23
ref : ForwardedRef < SelectionListHandle > ,
22
24
) {
23
25
const [ isModalVisible , setIsModalVisible ] = useState ( false ) ;
@@ -26,21 +28,47 @@ function SelectionListWithModal<TItem extends ListItem>(
26
28
// We need to use isSmallScreenWidth instead of shouldUseNarrowLayout here because there is a race condition that causes shouldUseNarrowLayout to change indefinitely in this component
27
29
// See https://github.com/Expensify/App/issues/48675 for more details
28
30
const { isSmallScreenWidth} = useResponsiveLayout ( ) ;
29
- const { selectionMode} = useMobileSelectionMode ( true ) ;
31
+ const { selectionMode} = useMobileSelectionMode ( shouldAutoTurnOff ) ;
32
+ // Check if selection should be on when the modal is opened
33
+ const wasSelectionOnRef = useRef ( false ) ;
34
+ // Keep track of the number of selected items to determine if we should turn off selection mode
35
+ const selectionRef = useRef ( 0 ) ;
30
36
31
37
useEffect ( ( ) => {
32
38
// We can access 0 index safely as we are not displaying multiple sections in table view
33
- const selectedItems = sections [ 0 ] . data . filter ( ( item ) => item . isSelected ) ;
39
+ const selectedItems = sections [ 0 ] . data . filter ( ( item ) => {
40
+ if ( isSelected ) {
41
+ return isSelected ( item ) ;
42
+ }
43
+ return ! ! item . isSelected ;
44
+ } ) ;
45
+ selectionRef . current = selectedItems . length ;
46
+
34
47
if ( ! isSmallScreenWidth ) {
35
48
if ( selectedItems . length === 0 ) {
36
49
turnOffMobileSelectionMode ( ) ;
37
50
}
38
51
return ;
39
52
}
53
+ if ( ! wasSelectionOnRef . current && selectedItems . length > 0 ) {
54
+ wasSelectionOnRef . current = true ;
55
+ }
40
56
if ( selectedItems . length > 0 && ! selectionMode ?. isEnabled ) {
41
57
turnOnMobileSelectionMode ( ) ;
58
+ } else if ( selectedItems . length === 0 && selectionMode ?. isEnabled && ! wasSelectionOnRef . current ) {
59
+ turnOffMobileSelectionMode ( ) ;
42
60
}
43
- } , [ sections , selectionMode , isSmallScreenWidth ] ) ;
61
+ } , [ sections , selectionMode , isSmallScreenWidth , isSelected ] ) ;
62
+
63
+ useEffect (
64
+ ( ) => ( ) => {
65
+ if ( selectionRef . current !== 0 ) {
66
+ return ;
67
+ }
68
+ turnOffMobileSelectionMode ( ) ;
69
+ } ,
70
+ [ ] ,
71
+ ) ;
44
72
45
73
const handleLongPressRow = ( item : TItem ) => {
46
74
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
0 commit comments