1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | import React, { Component } from 'react';
|
15 | import {
|
16 | View,
|
17 | Text,
|
18 | TextInput,
|
19 | TouchableHighlight,
|
20 | TouchableOpacity,
|
21 | Picker,
|
22 | } from 'react-native';
|
23 | import { I18n } from 'aws-amplify';
|
24 | import AmplifyTheme, {
|
25 | linkUnderlayColor,
|
26 | errorIconColor,
|
27 | } from './AmplifyTheme';
|
28 | import { Icon } from 'react-native-elements';
|
29 | import countryDialCodes from './CountryDialCodes';
|
30 |
|
31 | export const FormField = props => {
|
32 | const theme = props.theme || AmplifyTheme;
|
33 | return (
|
34 | <View style={theme.formField}>
|
35 | <Text style={theme.inputLabel}>
|
36 | {props.label} {props.required ? '*' : ''}
|
37 | </Text>
|
38 | <TextInput
|
39 | style={theme.input}
|
40 | autoCapitalize="none"
|
41 | autoCorrect={false}
|
42 | {...props}
|
43 | />
|
44 | </View>
|
45 | );
|
46 | };
|
47 |
|
48 | export class PhoneField extends Component {
|
49 | constructor(props) {
|
50 | super(props);
|
51 |
|
52 | this.state = {
|
53 | dialCode: this.props.defaultDialCode || '+1',
|
54 | phone: '',
|
55 | };
|
56 | }
|
57 |
|
58 | onChangeText() {
|
59 | const { dialCode, phone } = this.state;
|
60 | const cleanedPhone = phone.replace(/[^0-9.]/g, '') || '';
|
61 | const phoneNumber = cleanedPhone === '' ? '' : `${dialCode}${cleanedPhone}`;
|
62 | this.props.onChangeText(phoneNumber);
|
63 | }
|
64 |
|
65 | render() {
|
66 | const { label, required } = this.props;
|
67 | const theme = this.props.theme || AmplifyTheme;
|
68 |
|
69 | return (
|
70 | <View style={theme.formField}>
|
71 | <Text style={theme.inputLabel}>
|
72 | {label} {required ? '*' : ''}
|
73 | </Text>
|
74 | <View style={theme.phoneContainer}>
|
75 | <Picker
|
76 | style={theme.picker}
|
77 | selectedValue={this.state.dialCode}
|
78 | itemStyle={theme.pickerItem}
|
79 | onValueChange={dialCode => {
|
80 | this.setState({ dialCode }, () => {
|
81 | this.onChangeText();
|
82 | });
|
83 | }}
|
84 | >
|
85 | {countryDialCodes.map(dialCode => (
|
86 | <Picker.Item key={dialCode} value={dialCode} label={dialCode} />
|
87 | ))}
|
88 | </Picker>
|
89 | <TextInput
|
90 | style={theme.phoneInput}
|
91 | autoCapitalize="none"
|
92 | autoCorrect={false}
|
93 | {...this.props}
|
94 | onChangeText={phone => {
|
95 | this.setState({ phone }, () => {
|
96 | this.onChangeText();
|
97 | });
|
98 | }}
|
99 | />
|
100 | </View>
|
101 | </View>
|
102 | );
|
103 | }
|
104 | }
|
105 |
|
106 | export const SectionFooter = props => {
|
107 | const theme = props.theme || AmplifyTheme;
|
108 | return (
|
109 | <View style={theme.sectionFooter}>
|
110 | <LinkCell theme={theme} onPress={() => onStateChange('confirmSignUp')}>
|
111 | {I18n.get('Confirm a Code')}
|
112 | </LinkCell>
|
113 | <LinkCell theme={theme} onPress={() => onStateChange('signIn')}>
|
114 | {I18n.get('Sign In')}
|
115 | </LinkCell>
|
116 | </View>
|
117 | );
|
118 | };
|
119 |
|
120 | export const LinkCell = props => {
|
121 | const theme = props.theme || AmplifyTheme;
|
122 | return (
|
123 | <View style={theme.cell}>
|
124 | <TouchableHighlight
|
125 | onPress={props.onPress}
|
126 | underlayColor={linkUnderlayColor}
|
127 | >
|
128 | <Text style={theme.sectionFooterLink}>{props.children}</Text>
|
129 | </TouchableHighlight>
|
130 | </View>
|
131 | );
|
132 | };
|
133 |
|
134 | export const Header = props => {
|
135 | const theme = props.theme || AmplifyTheme;
|
136 | return (
|
137 | <View style={theme.sectionHeader}>
|
138 | <Text style={theme.sectionHeaderText}>{props.children}</Text>
|
139 | </View>
|
140 | );
|
141 | };
|
142 |
|
143 | export const ErrorRow = props => {
|
144 | const theme = props.theme || AmplifyTheme;
|
145 | if (!props.children) return null;
|
146 | return (
|
147 | <View style={theme.errorRow}>
|
148 | <Icon name="warning" color={errorIconColor} />
|
149 | <Text style={theme.errorRowText}>{props.children}</Text>
|
150 | </View>
|
151 | );
|
152 | };
|
153 |
|
154 | export const AmplifyButton = props => {
|
155 | const theme = props.theme || AmplifyTheme;
|
156 | let style = theme.button;
|
157 | if (props.disabled) {
|
158 | style = theme.buttonDisabled;
|
159 | }
|
160 |
|
161 | if (props.style) {
|
162 | style = [style, props.style];
|
163 | }
|
164 |
|
165 | return (
|
166 | <TouchableOpacity {...props} style={style}>
|
167 | <Text style={theme.buttonText}>{props.text}</Text>
|
168 | </TouchableOpacity>
|
169 | );
|
170 | };
|