forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionScreen.tsx
172 lines (144 loc) · 5.94 KB
/
SelectionScreen.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import {isEmpty} from 'lodash';
import React from 'react';
import type {StyleProp, ViewStyle} from 'react-native';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as PolicyUtils from '@libs/PolicyUtils';
import type {AccessVariant} from '@pages/workspace/AccessOrNotFoundWrapper';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import type {TranslationPaths} from '@src/languages/types';
import type * as OnyxCommon from '@src/types/onyx/OnyxCommon';
import type {ConnectionName, PolicyFeatureName} from '@src/types/onyx/Policy';
import type {ReceiptErrors} from '@src/types/onyx/Transaction';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import ErrorMessageRow from './ErrorMessageRow';
import HeaderWithBackButton from './HeaderWithBackButton';
import OfflineWithFeedback from './OfflineWithFeedback';
import ScreenWrapper from './ScreenWrapper';
import SelectionList from './SelectionList';
import type RadioListItem from './SelectionList/RadioListItem';
import type TableListItem from './SelectionList/TableListItem';
import type {ListItem, SectionListDataType} from './SelectionList/types';
import type UserListItem from './SelectionList/UserListItem';
type SelectorType<T = string> = ListItem & {
value: T;
onPress?: () => void;
};
type SelectionScreenProps<T = string> = {
/** Used to set the testID for tests */
displayName: string;
/** Title of the selection component */
title?: TranslationPaths;
/** Custom content to display in the header */
headerContent?: React.ReactNode;
/** Content to display if the list is empty */
listEmptyContent?: React.JSX.Element | null;
/** Custom content to display in the footer of list component. */
listFooterContent?: React.JSX.Element | null;
/** Sections for the section list */
sections: Array<SectionListDataType<SelectorType<T>>>;
/** Default renderer for every item in the list */
listItem: typeof RadioListItem | typeof UserListItem | typeof TableListItem;
/** Item `keyForList` to focus initially */
initiallyFocusedOptionKey?: string | null | undefined;
/** Callback to fire when a row is pressed */
onSelectRow: (selection: SelectorType<T>) => void;
/** Callback to fire when back button is pressed */
onBackButtonPress: () => void;
/** The current policyID */
policyID: string;
/** Defines which types of access should be verified */
accessVariants?: AccessVariant[];
/** The current feature name that the user tries to get access to */
featureName?: PolicyFeatureName;
/** Whether or not to block user from accessing the page */
shouldBeBlocked?: boolean;
/** Name of the current connection */
connectionName: ConnectionName;
/** The type of action that's pending */
pendingAction?: OnyxCommon.PendingAction | null;
/** The errors to display */
errors?: OnyxCommon.Errors | ReceiptErrors | null;
/** Additional style object for the error row */
errorRowStyles?: StyleProp<ViewStyle>;
/** A function to run when the X button next to the error is clicked */
onClose?: () => void;
/** Whether to debounce `onRowSelect` */
shouldDebounceRowSelect?: boolean;
/** Used for dynamic header title translation with parameters */
headerTitleAlreadyTranslated?: string;
};
function SelectionScreen<T = string>({
displayName,
title,
headerContent,
listEmptyContent,
listFooterContent,
sections,
listItem,
initiallyFocusedOptionKey,
onSelectRow,
onBackButtonPress,
policyID,
accessVariants,
featureName,
shouldBeBlocked,
connectionName,
pendingAction,
errors,
errorRowStyles,
onClose,
shouldDebounceRowSelect,
headerTitleAlreadyTranslated,
}: SelectionScreenProps<T>) {
const {translate} = useLocalize();
const styles = useThemeStyles();
const policy = PolicyUtils.getPolicy(policyID);
const isConnectionEmpty = isEmpty(policy?.connections?.[connectionName]);
return (
<AccessOrNotFoundWrapper
policyID={policyID}
accessVariants={accessVariants}
featureName={featureName}
shouldBeBlocked={isConnectionEmpty || shouldBeBlocked}
>
<ScreenWrapper
includeSafeAreaPaddingBottom={!!errors && !isEmptyObject(errors)}
testID={displayName}
>
<HeaderWithBackButton
title={headerTitleAlreadyTranslated ?? (title ? translate(title) : '')}
onBackButtonPress={onBackButtonPress}
/>
{headerContent}
<OfflineWithFeedback
pendingAction={pendingAction}
style={[styles.flex1]}
contentContainerStyle={[styles.flex1]}
>
<SelectionList
onSelectRow={onSelectRow}
sections={sections}
ListItem={listItem}
showScrollIndicator
shouldShowTooltips={false}
initiallyFocusedOptionKey={initiallyFocusedOptionKey}
listEmptyContent={listEmptyContent}
listFooterContent={listFooterContent}
sectionListStyle={!!sections.length && [styles.flexGrow0]}
shouldDebounceRowSelect={shouldDebounceRowSelect}
>
<ErrorMessageRow
errors={errors}
errorRowStyles={errorRowStyles}
onClose={onClose}
/>
</SelectionList>
</OfflineWithFeedback>
</ScreenWrapper>
</AccessOrNotFoundWrapper>
);
}
export type {SelectorType};
SelectionScreen.displayName = 'SelectionScreen';
export default SelectionScreen;