Skip to content

Commit b481a4f

Browse files
committed
fix(form): fix phone input and button
1 parent 4dd8452 commit b481a4f

8 files changed

Lines changed: 183 additions & 90 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"license": "MIT",
3737
"dependencies": {
3838
"@expo/vector-icons": "10.0.1",
39-
"countries-list": "2.4.2",
4039
"date-fns": "1.30.1",
4140
"deepmerge": "3.2.0",
4241
"exenv": "1.2.2",
@@ -69,6 +68,7 @@
6968
"@types/react-native": "0.57.55",
7069
"babel-core": "7.0.0-bridge.0",
7170
"babel-jest": "24.8.0",
71+
"countries-list": "2.4.2",
7272
"docz": "1.2.0",
7373
"docz-plugin-snapshots": "0.2.0",
7474
"docz-theme-default": "1.2.0",

src/components/Button/Button.styles.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ export interface ButtonStyles {
195195
buttonStyle: ViewStyle;
196196
textStyle: TextStyle;
197197
focusColor: string;
198+
innerButtonWrapperStyle: ViewStyle;
199+
buttonContentWrapperStyle: ViewStyle;
198200
}
199201

200202
export interface ButtonStylesProps {
@@ -204,6 +206,8 @@ export interface ButtonStylesProps {
204206
isDisabled: boolean;
205207
isLoading: boolean;
206208
isInline: boolean;
209+
iconBefore?: any;
210+
iconAfter?: any;
207211
}
208212
export type GetButtonStyles = (
209213
buttonStylesProps: ButtonStylesProps,
@@ -218,6 +222,8 @@ export const getButtonStyles: GetButtonStyles = (buttonStyleProps, theme) => {
218222
isDisabled,
219223
isLoading,
220224
isInline,
225+
iconBefore,
226+
iconAfter,
221227
} = buttonStyleProps;
222228

223229
const { appearances, sizes, disabled, ...baseStyles } = getButtonVariables(
@@ -265,5 +271,17 @@ export const getButtonStyles: GetButtonStyles = (buttonStyleProps, theme) => {
265271
justifyContent: 'center',
266272
textAlign: 'center',
267273
},
274+
275+
innerButtonWrapperStyle: {
276+
alignItems: 'center',
277+
flexDirection: 'row',
278+
height: '100%',
279+
justifyContent: 'center',
280+
},
281+
282+
buttonContentWrapperStyle: {
283+
paddingLeft: iconBefore ? 8 : 0,
284+
paddingRight: iconAfter ? 8 : 0,
285+
},
268286
};
269287
};

src/components/Button/Button.tsx

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { DeepPartial } from 'ts-essentials';
1010

1111
import { useTheme } from '../../theme';
1212
import { mergeStyles, ReplaceReturnType } from '../../utils/mergeStyles';
13-
import { Box } from '../Box';
1413
import { LoadingDots } from '../Loading';
1514
import { Text } from '../Typography';
1615
import {
@@ -130,13 +129,18 @@ export const Button = (props: ButtonProps) => {
130129

131130
const theme = useTheme();
132131

133-
const { buttonStyle, textStyle, focusColor } = mergeStyles(
134-
getButtonStyles,
135-
getStyles,
136-
)(
132+
const {
133+
buttonStyle,
134+
textStyle,
135+
focusColor,
136+
innerButtonWrapperStyle,
137+
buttonContentWrapperStyle,
138+
} = mergeStyles(getButtonStyles, getStyles)(
137139
{
138140
appearance,
139141
color,
142+
iconAfter,
143+
iconBefore,
140144
isDisabled,
141145
isInline,
142146
isLoading,
@@ -156,20 +160,13 @@ export const Button = (props: ButtonProps) => {
156160
testID={testID}
157161
{...accessibilityProps}
158162
>
159-
<View
160-
style={{
161-
alignItems: 'center',
162-
flexDirection: 'row',
163-
height: '100%',
164-
justifyContent: 'center',
165-
}}
166-
>
163+
<View style={innerButtonWrapperStyle}>
167164
{iconBefore}
168-
<Box paddingLeft={iconBefore ? 8 : 0} paddingRight={iconAfter ? 8 : 0}>
165+
<View style={buttonContentWrapperStyle}>
169166
{/*
170167
// @ts-ignore */}
171168
<ButtonContent {...props} textStyle={textStyle} />
172-
</Box>
169+
</View>
173170
{iconAfter}
174171
</View>
175172
</TouchableHighlight>

src/components/Inputs/PhoneNumberInput.mdx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ menu: Components
44
---
55

66
import { Playground, Props } from 'docz';
7+
import { countries } from 'countries-list';
78
import { State } from 'react-powerplug';
89

910
import { Heading } from '../Typography';
@@ -18,7 +19,7 @@ Appends a button to select country code
1819
### Usage
1920

2021
<Playground>
21-
<State initial={{ countryCode: 'US', phoneNumber: '' }}>
22+
<State initial={{ countryCode: '1', phoneNumber: '' }}>
2223
{({ state, setState }) => (
2324
<PhoneNumberInput
2425
header={
@@ -30,7 +31,12 @@ Appends a button to select country code
3031
onChangePhoneNumber={phoneNumber => setState({ phoneNumber })}
3132
phoneNumber={state.phoneNumber}
3233
countryCode={state.countryCode}
34+
countryCodes={Object.keys(countries).map(countryCode => ({
35+
value: countries[countryCode].phone,
36+
label: countries[countryCode].name,
37+
}))}
3338
placeholder="Enter your phone number"
39+
getCountryCodeTitle={countryCode => `+${countryCode}`}
3440
/>
3541
)}
3642
</State>

src/components/Inputs/PhoneNumberInput.tsx

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { countries as countryList } from 'countries-list';
21
import * as React from 'react';
32
import { FlatList, TextInput as RNTextInput, View } from 'react-native';
43
import { DeepPartial, Omit } from 'ts-essentials';
54

5+
import { TextInput, TextInputProps } from '.';
66
import { useTheme } from '../../theme';
77
import { mergeStyles, ReplaceReturnType } from '../../utils/mergeStyles';
88
import { Button } from '../Button';
@@ -13,7 +13,6 @@ import {
1313
getPhoneNumberInputStyles,
1414
PhoneNumberInputStyles,
1515
} from './PhoneNumberInput.styles';
16-
import { TextInput, TextInputProps } from './TextInput';
1716
import { GetTextInputStyles, TextInputStyles } from './TextInput.styles';
1817

1918
export interface PhoneNumberInputProps
@@ -30,26 +29,29 @@ export interface PhoneNumberInputProps
3029
GetTextInputStyles,
3130
DeepPartial<TextInputStyles & PhoneNumberInputStyles>
3231
>;
32+
countryCodes?: CountryCode[];
33+
getCountryCodeTitle?: (countryCode: string) => string;
3334
}
3435

35-
const countries = (() => {
36-
return Object.keys(countryList).map(countryCode => ({
37-
countryCode,
38-
key: countryCode,
39-
...countryList[countryCode],
40-
}));
41-
})();
36+
export interface CountryCode {
37+
/** The value for countryCode */
38+
value: string;
39+
/** Labels used in the list of countries to select the country code from */
40+
label: string;
41+
}
4242

4343
const PhoneNumberInputBase = (props: PhoneNumberInputProps) => {
4444
const {
45-
countryCode = 'US',
45+
countryCode = '1',
46+
countryCodes = [],
4647
phoneNumber,
4748
onChangeCountryCode,
4849
onChangePhoneNumber,
4950
header,
5051
getStyles,
5152
innerRef,
5253
useHistory = false,
54+
getCountryCodeTitle = (code: string) => code,
5355
...textInputProps
5456
} = props;
5557
const [isModalOpen, setIsModalOpen] = React.useState(false);
@@ -80,7 +82,24 @@ const PhoneNumberInputBase = (props: PhoneNumberInputProps) => {
8082
name="chevron-down"
8183
/>
8284
}
83-
title={`+${countryList[countryCode].phone}`}
85+
title={getCountryCodeTitle(countryCode)}
86+
/>
87+
<TextInput
88+
ref={innerRef}
89+
name="phone"
90+
getStyles={() => ({
91+
containerStyle: {
92+
flex: 1,
93+
},
94+
inputStyle: {
95+
borderBottomLeftRadius: 0,
96+
borderTopLeftRadius: 0,
97+
},
98+
})}
99+
keyboardType="phone-pad"
100+
value={phoneNumber}
101+
onChangeText={onChangePhoneNumber}
102+
{...textInputProps}
84103
/>
85104
<CloseableModal
86105
visible={isModalOpen}
@@ -90,22 +109,22 @@ const PhoneNumberInputBase = (props: PhoneNumberInputProps) => {
90109
>
91110
<FlatList
92111
ListHeaderComponent={header}
93-
keyExtractor={item => item.key}
112+
keyExtractor={item => item.label}
94113
getItemLayout={(data, index) => ({
95114
index,
96115
length: theme.controlHeights.medium,
97116
offset: theme.controlHeights.medium * index,
98117
})}
99-
data={countries}
100-
renderItem={({ item: country }) => {
118+
data={countryCodes}
119+
renderItem={({ item }) => {
101120
return (
102121
<ListItem
103-
key={country.countryCode}
104-
label={country.name}
122+
key={item.label}
123+
label={item.label}
105124
onPress={event => {
106125
event.preventDefault();
107126
if (onChangeCountryCode) {
108-
onChangeCountryCode(country.countryCode);
127+
onChangeCountryCode(item.value);
109128
}
110129
setIsModalOpen(false);
111130
}}
@@ -114,24 +133,11 @@ const PhoneNumberInputBase = (props: PhoneNumberInputProps) => {
114133
}}
115134
/>
116135
</CloseableModal>
117-
<TextInput
118-
ref={innerRef}
119-
name="phone"
120-
getStyles={() => ({
121-
inputStyle: {
122-
borderBottomLeftRadius: 0,
123-
borderTopLeftRadius: 0,
124-
},
125-
})}
126-
keyboardType="phone-pad"
127-
value={phoneNumber}
128-
onChangeText={onChangePhoneNumber}
129-
{...textInputProps}
130-
/>
131136
</View>
132137
);
133138
};
134139

135-
export const PhoneNumberInput = React.forwardRef<RNTextInput, TextInputProps>(
136-
(props, ref) => <PhoneNumberInputBase {...props} innerRef={ref} />,
137-
);
140+
export const PhoneNumberInput = React.forwardRef<
141+
RNTextInput,
142+
PhoneNumberInputProps
143+
>((props, ref) => <PhoneNumberInputBase {...props} innerRef={ref} />);

src/components/Modal/ModalContent.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const ModalContent = (props: ModalContentProps) => {
1515
marginLeft: 'auto',
1616
marginRight: 'auto',
1717
maxWidth: 960,
18+
width: '100%',
1819
}}
1920
>
2021
{children}

src/components/Toast/ToastContext.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ const defaultToastContext: ToastContext = {
2222
};
2323

2424
export const ToastContext = React.createContext(defaultToastContext);
25+
26+
export const useToast = () => {
27+
return React.useContext(ToastContext);
28+
};

0 commit comments

Comments
 (0)