-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.tsx
91 lines (81 loc) · 3.29 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import React, {forwardRef, useEffect, useState} from 'react';
import type {ForwardedRef} from 'react';
import {useOnyx} from 'react-native-onyx';
import * as Expensicons from '@components/Icon/Expensicons';
import MenuItem from '@components/MenuItem';
import Modal from '@components/Modal';
import SelectionList from '@components/SelectionList';
import type {BaseSelectionListProps, ListItem, SelectionListHandle} from '@components/SelectionList/types';
import useLocalize from '@hooks/useLocalize';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import {turnOffMobileSelectionMode, turnOnMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
type SelectionListWithModalProps<TItem extends ListItem> = BaseSelectionListProps<TItem> & {
turnOnSelectionModeOnLongPress?: boolean;
onTurnOnSelectionMode?: (item: TItem | null) => void;
};
function SelectionListWithModal<TItem extends ListItem>(
{turnOnSelectionModeOnLongPress, onTurnOnSelectionMode, onLongPressRow, sections, ...rest}: SelectionListWithModalProps<TItem>,
ref: ForwardedRef<SelectionListHandle>,
) {
const [isModalVisible, setIsModalVisible] = useState(false);
const [longPressedItem, setLongPressedItem] = useState<TItem | null>(null);
const {translate} = useLocalize();
const {isSmallScreenWidth} = useResponsiveLayout();
const [selectionMode] = useOnyx(ONYXKEYS.MOBILE_SELECTION_MODE);
useEffect(() => {
// We can access 0 index safely as we are not displaying multiple sections in table view
const selectedItems = sections[0].data.filter((item) => item.isSelected);
if (!isSmallScreenWidth) {
if (selectedItems.length === 0) {
turnOffMobileSelectionMode();
}
return;
}
if (selectedItems.length > 0 && !selectionMode?.isEnabled) {
turnOnMobileSelectionMode();
}
}, [sections, selectionMode, isSmallScreenWidth]);
const handleLongPressRow = (item: TItem) => {
if (!turnOnSelectionModeOnLongPress || !isSmallScreenWidth) {
return;
}
setLongPressedItem(item);
setIsModalVisible(true);
if (onLongPressRow) {
onLongPressRow(item);
}
};
const turnOnSelectionMode = () => {
turnOnMobileSelectionMode();
setIsModalVisible(false);
if (onTurnOnSelectionMode) {
onTurnOnSelectionMode(longPressedItem);
}
};
useEffect(() => turnOffMobileSelectionMode(), []);
return (
<>
<SelectionList
ref={ref}
sections={sections}
onLongPressRow={handleLongPressRow}
// eslint-disable-next-line react/jsx-props-no-spreading
{...rest}
/>
<Modal
isVisible={isModalVisible}
type={CONST.MODAL.MODAL_TYPE.BOTTOM_DOCKED}
onClose={() => setIsModalVisible(false)}
>
<MenuItem
title={translate('common.select')}
icon={Expensicons.Checkmark}
onPress={turnOnSelectionMode}
/>
</Modal>
</>
);
}
export default forwardRef(SelectionListWithModal);