UNPKG

4.21 kBJavaScriptView Raw
1/*
2 * Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5 * the License. A copy of the License is located at
6 *
7 * http://aws.amazon.com/apache2.0/
8 *
9 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11 * and limitations under the License.
12 */
13
14import React, { Component } from 'react';
15import {
16 View,
17 Text,
18 TextInput,
19 TouchableHighlight,
20 TouchableOpacity,
21 Picker,
22} from 'react-native';
23import { I18n } from 'aws-amplify';
24import AmplifyTheme, {
25 linkUnderlayColor,
26 errorIconColor,
27} from './AmplifyTheme';
28import { Icon } from 'react-native-elements';
29import countryDialCodes from './CountryDialCodes';
30
31export 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
48export 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
106export 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
120export 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
134export 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
143export 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
154export 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};