-
Notifications
You must be signed in to change notification settings - Fork 7
/
FormInput.js
58 lines (52 loc) · 1.11 KB
/
FormInput.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
import React, {Component, Fragment} from 'react';
import {StyleSheet, Text, TextInput, View} from 'react-native';
export default class FormInput extends Component {
constructor(props) {
super(props);
this.state = {
touched: false,
};
this.onBlur = this.onBlur.bind(this);
}
renderError() {
const {errorLabel, touched} = this.props;
if (errorLabel && (touched || this.state.touched)) {
return (
<View>
<Text style={styles.error}>{errorLabel}</Text>
</View>
);
}
return null;
}
onBlur() {
this.setState({
touched: true,
});
}
render() {
const {label} = this.props;
return (
<Fragment>
<Text>{label}</Text>
<TextInput style={styles.input} {...this.props} onBlur={this.onBlur} />
{this.renderError()}
</Fragment>
);
}
}
const styles = StyleSheet.create({
input: {
borderWidth: 1,
borderColor: 'black',
padding: 10,
marginBottom: 15,
alignSelf: 'stretch',
},
error: {
position: 'absolute',
bottom: 0,
color: 'red',
fontSize: 12,
},
});