-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: design UI component for picker field
- Loading branch information
1 parent
4ed3307
commit c65f205
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { | ||
StyleSheet, | ||
Text, | ||
TextStyle, | ||
TouchableOpacity, | ||
ViewStyle, | ||
} from 'react-native'; | ||
import React from 'react'; | ||
import type { InputManager } from './usePhoneNumberInput'; | ||
import { countries } from './countries'; | ||
|
||
interface CountryPickerFieldProps { | ||
manager: InputManager; | ||
fieldStyles?: ViewStyle; | ||
textStyles?: TextStyle; | ||
} | ||
|
||
const CountryPickerField = ({ | ||
manager, | ||
fieldStyles, | ||
textStyles, | ||
}: CountryPickerFieldProps) => { | ||
const { state, dispatch } = manager; | ||
|
||
let country; | ||
if (state.customCountries) { | ||
country = state.customCountries.find((c) => c.code === state.countryCode); | ||
} else { | ||
country = countries.find((c) => c.code === state.countryCode); | ||
} | ||
|
||
if (country == null) { | ||
throw new Error('Invalid country'); | ||
} | ||
|
||
return ( | ||
<TouchableOpacity | ||
style={{ ...fieldStyles }} | ||
onPress={() => | ||
dispatch({ type: 'setHidden', payload: !state.pickerHidden }) | ||
} | ||
> | ||
{/* textStyles applies to all texts */} | ||
<Text style={{ ...styles.text, ...textStyles }}> | ||
{country.emoji} {state.countryTel} | ||
</Text> | ||
<Text style={{ ...styles.chevron, ...textStyles }}>▼</Text> | ||
</TouchableOpacity> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
text: { | ||
fontSize: 20, | ||
}, | ||
chevron: { | ||
fontSize: 14, | ||
}, | ||
}); | ||
|
||
export default CountryPickerField; |