-
Notifications
You must be signed in to change notification settings - Fork 296
/
Copy pathApp.js
133 lines (126 loc) · 2.98 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* eslint react/no-multi-comp:0, no-console:0, react/no-multi-comp:0 */
import React from 'react';
import PropTypes from 'prop-types';
import {
StyleSheet,
Button,
Dimensions,
TextInput,
Text,
View,
Alert,
} from 'react-native';
import { createForm } from 'rc-form';
const { width } = Dimensions.get('window');
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
padding: 50,
justifyContent: 'center',
},
inputView: {
width: width - 100,
paddingLeft: 10,
},
input: {
height: 42,
fontSize: 16,
},
errorinfo: {
marginTop: 10,
},
errorinfoText: {
color: 'red',
},
});
class FromItem extends React.PureComponent {
static propTypes = {
label: PropTypes.string,
onChange: PropTypes.func,
value: PropTypes.string,
error: PropTypes.array,
};
getError = error => {
if (error) {
return error.map(info => {
return (
<Text style={styles.errorinfoText} key={info}>
{info}
</Text>
);
});
}
return null;
};
render() {
const { label, onChange, value, error } = this.props;
return (
<View style={styles.inputView}>
<TextInput
style={styles.input}
value={value || ''}
label={`${label}:`}
duration={150}
onChangeText={onChange}
highlightColor="#40a9ff"
underlineColorAndroid="#40a9ff"
/>
<View style={styles.errorinfo}>{this.getError(error)}</View>
</View>
);
}
}
class App extends React.Component {
static propTypes = {
form: PropTypes.object.isRequired,
};
checkUserNameOne = (value, callback) => {
setTimeout(() => {
if (value === '15188888888') {
callback('手机号已经被注册');
} else {
callback();
}
}, 2000);
};
submit = () => {
this.props.form.validateFields((error) => {
if (error) return;
Alert('通过了所有验证'); // eslint-disable-line new-cap
});
};
render() {
const { getFieldDecorator, getFieldError } = this.props.form;
return (
<View style={styles.container}>
<Text>简单的手机号验证</Text>
{getFieldDecorator('username', {
validateFirst: true,
rules: [
{ required: true, message: '请输入手机号!' },
{
pattern: /^1\d{10}$/,
message: '请输入正确的手机号!',
},
{
validator: (rule, value, callback) => {
this.checkUserNameOne(value, callback);
},
message: '手机号已经被注册!',
},
],
})(
<FromItem
autoFocus
placeholder="手机号"
error={getFieldError('username')}
/>
)}
<Button color="#40a9ff" onPress={this.submit} title="登陆" />
</View>
);
}
}
export default createForm()(App);