-
-
Notifications
You must be signed in to change notification settings - Fork 835
Expand file tree
/
Copy pathFlagButton.tsx
More file actions
171 lines (162 loc) · 4.09 KB
/
FlagButton.tsx
File metadata and controls
171 lines (162 loc) · 4.09 KB
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
import React, { useState, useEffect, ReactNode, memo } from 'react'
import {
TouchableOpacity,
StyleSheet,
View,
StyleProp,
ViewStyle,
TextProps,
} from 'react-native'
import { CountryCode } from './types'
import { Flag } from './Flag'
import { useContext } from './CountryContext'
import { CountryText } from './CountryText'
import { useTheme } from './CountryTheme'
const styles = StyleSheet.create({
container: {
alignItems: 'flex-start',
justifyContent: 'flex-start',
},
containerWithEmoji: {
marginTop: 0,
},
containerWithoutEmoji: {
marginTop: 5,
},
flagWithSomethingContainer: {
flexDirection: 'row',
flexWrap: 'wrap',
alignItems: 'center',
},
something: { fontSize: 16 },
})
type FlagWithSomethingProp = Pick<
FlagButtonProps,
| 'countryCode'
| 'withEmoji'
| 'withCountryNameButton'
| 'withCurrencyButton'
| 'withCallingCodeButton'
| 'withFlagButton'
| 'placeholder'
> & { flagSize: number; allowFontScaling?: boolean }
const FlagText = (props: TextProps & { children: ReactNode }) => (
<CountryText {...props} style={styles.something} />
)
const FlagWithSomething = memo(
({
allowFontScaling,
countryCode,
withEmoji,
withCountryNameButton,
withCurrencyButton,
withCallingCodeButton,
withFlagButton,
flagSize,
placeholder,
}: FlagWithSomethingProp) => {
const { translation, getCountryInfoAsync } = useContext()
const [state, setState] = useState({
countryName: '',
currency: '',
callingCode: '',
})
const { countryName, currency, callingCode } = state
useEffect(() => {
if (countryCode) {
getCountryInfoAsync({ countryCode, translation })
.then(setState)
.catch(console.warn)
}
}, [
countryCode,
withCountryNameButton,
withCurrencyButton,
withCallingCodeButton,
])
return (
<View style={styles.flagWithSomethingContainer}>
{countryCode ? (
<Flag
{...{ withEmoji, countryCode, withFlagButton, flagSize }}
/>
) : (
<FlagText allowFontScaling={allowFontScaling}>{placeholder}</FlagText>
)}
{withCountryNameButton && countryName ? (
<FlagText allowFontScaling={allowFontScaling}>
{countryName + ' '}
</FlagText>
) : null}
{withCurrencyButton && currency ? (
<FlagText
allowFontScaling={allowFontScaling}
>{`(${currency}) `}</FlagText>
) : null}
{withCallingCodeButton && callingCode ? (
<FlagText
allowFontScaling={allowFontScaling}
>{`+${callingCode}`}</FlagText>
) : null}
</View>
)
},
)
export interface FlagButtonProps {
allowFontScaling?: boolean
withEmoji?: boolean
withCountryNameButton?: boolean
withCurrencyButton?: boolean
withCallingCodeButton?: boolean
withFlagButton?: boolean
containerButtonStyle?: StyleProp<ViewStyle>
countryCode?: CountryCode
placeholder: string
onOpen?: () => void
}
export const FlagButton = ({
allowFontScaling,
withEmoji,
withCountryNameButton,
withCallingCodeButton,
withCurrencyButton,
withFlagButton,
countryCode,
containerButtonStyle,
onOpen,
placeholder,
}: FlagButtonProps) => {
const { flagSizeButton: flagSize } = useTheme()
return (
<TouchableOpacity activeOpacity={0.7} onPress={onOpen}>
<View
style={[
styles.container,
withEmoji ? styles.containerWithEmoji : styles.containerWithoutEmoji,
containerButtonStyle,
]}
>
<FlagWithSomething
{...{
allowFontScaling,
countryCode,
withEmoji,
withCountryNameButton,
withCallingCodeButton,
withCurrencyButton,
withFlagButton,
flagSize: flagSize!,
placeholder,
}}
/>
</View>
</TouchableOpacity>
)
}
FlagButton.defaultProps = {
withEmoji: true,
withCountryNameButton: false,
withCallingCodeButton: false,
withCurrencyButton: false,
withFlagButton: true,
}