-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathCurrencyPicker.tsx
86 lines (77 loc) · 3.15 KB
/
CurrencyPicker.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
import type {ReactNode} from 'react';
import React, {useState} from 'react';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import Navigation from '@libs/Navigation/Navigation';
import CONST from '@src/CONST';
import CurrencySelectionList from './CurrencySelectionList';
import type {CurrencyListItem} from './CurrencySelectionList/types';
import HeaderWithBackButton from './HeaderWithBackButton';
import MenuItemWithTopDescription from './MenuItemWithTopDescription';
import Modal from './Modal';
import ScreenWrapper from './ScreenWrapper';
type CurrencyPickerProps = {
/** Current value of the selected item */
value?: string;
/** Custom content to display in the header */
headerContent?: ReactNode;
/** Callback when the list item is selected */
onInputChange?: (value: string, key?: string) => void;
/** Form Error description */
errorText?: string;
};
function CurrencyPicker({value, errorText, headerContent, onInputChange = () => {}}: CurrencyPickerProps) {
const {translate} = useLocalize();
const [isPickerVisible, setIsPickerVisible] = useState(false);
const styles = useThemeStyles();
const hidePickerModal = () => {
setIsPickerVisible(false);
};
const updateInput = (item: CurrencyListItem) => {
onInputChange?.(item.currencyCode);
hidePickerModal();
};
return (
<>
<MenuItemWithTopDescription
shouldShowRightIcon
title={value ? `${value} - ${CurrencyUtils.getCurrencySymbol(value)}` : undefined}
description={translate('common.currency')}
onPress={() => setIsPickerVisible(true)}
brickRoadIndicator={errorText ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined}
errorText={errorText}
/>
<Modal
type={CONST.MODAL.MODAL_TYPE.RIGHT_DOCKED}
isVisible={isPickerVisible}
onClose={hidePickerModal}
onModalHide={hidePickerModal}
hideModalContentWhileAnimating
useNativeDriver
onBackdropPress={Navigation.dismissModal}
>
<ScreenWrapper
style={[styles.pb0]}
includePaddingTop={false}
includeSafeAreaPaddingBottom
testID={CurrencyPicker.displayName}
>
<HeaderWithBackButton
title={translate('common.currency')}
shouldShowBackButton
onBackButtonPress={hidePickerModal}
/>
{!!headerContent && headerContent}
<CurrencySelectionList
initiallySelectedCurrencyCode={value}
onSelect={updateInput}
searchInputLabel={translate('common.search')}
/>
</ScreenWrapper>
</Modal>
</>
);
}
CurrencyPicker.displayName = 'CurrencyPicker';
export default CurrencyPicker;