Skip to content

Commit

Permalink
feat: design UI component for picker field
Browse files Browse the repository at this point in the history
  • Loading branch information
gtomitsuka committed Apr 14, 2023
1 parent 4ed3307 commit c65f205
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/CountryPickerField.tsx
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;

0 comments on commit c65f205

Please sign in to comment.