Skip to content
This repository was archived by the owner on Jan 8, 2021. It is now read-only.

Commit 81a6745

Browse files
author
andela-akhenda
committed
feat(components): create Form common component
1 parent 383942b commit 81a6745

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed

src/components/Form.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
3+
import { ActivityIndicator, Keyboard } from 'react-native';
4+
import { View, Text, Item, Input, Icon, Button } from 'native-base';
5+
6+
import { colors } from 'src/theme';
7+
import styles from './styles/FormStyles';
8+
9+
10+
class Form extends Component {
11+
constructor(props) {
12+
super(props);
13+
14+
this.state = {
15+
showFooter: true,
16+
};
17+
}
18+
19+
componentWillMount() {
20+
this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', () => this.keyboardDidShow());
21+
this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', () => this.keyboardDidHide());
22+
}
23+
24+
componentWillUnmount() {
25+
this.keyboardDidShowListener.remove();
26+
this.keyboardDidHideListener.remove();
27+
}
28+
29+
keyboardDidShow() {
30+
this.setState({ showFooter: false });
31+
}
32+
33+
keyboardDidHide() {
34+
this.setState({ showFooter: true });
35+
}
36+
37+
renderInput(value, placeholder, onChange, icon, keyboard = 'default') {
38+
const { error } = this.props;
39+
40+
return (
41+
<Item rounded style={styles.textInput} error={error}>
42+
<Icon active name={icon ||'ios-contact'} style={styles.textInputIcon} />
43+
<Input
44+
value={value}
45+
autoCorrect={false}
46+
onChangeText={onChange}
47+
keyboardType={keyboard}
48+
placeholder={placeholder}
49+
style={styles.textInputField}
50+
placeholderTextColor={colors.secondary.main}
51+
secureTextEntry={placeholder === 'Password'}
52+
autoCapitalize={placeholder === 'Full Name' ? 'words' : 'none'}
53+
/>
54+
</Item>
55+
);
56+
}
57+
58+
renderFooter() {
59+
const { fields } = this.props;
60+
61+
if (!fields.includes('fullName') && this.state.showFooter) {
62+
return (
63+
<View style={styles.footer}>
64+
<Button transparent light style={styles.forgotButton} onPress={() => {}}>
65+
<Text style={styles.footerText}>Forgot Password?</Text>
66+
</Button>
67+
<Button transparent block light onPress={() => {}}>
68+
<Text style={styles.footerText}>Skip</Text>
69+
</Button>
70+
</View>
71+
);
72+
}
73+
}
74+
75+
render() {
76+
const {
77+
fields,
78+
altText,
79+
loading,
80+
altAction,
81+
buttonText,
82+
emailValue,
83+
passwordValue,
84+
usernameValue,
85+
fullNameValue,
86+
onChangeEmail,
87+
onChangePassword,
88+
onChangeFullName,
89+
onChangeUsername,
90+
onSubmitForm,
91+
} = this.props;
92+
93+
return (
94+
<View style={styles.form}>
95+
{fields.includes('fullName')
96+
? this.renderInput(fullNameValue, 'Full Name', onChangeFullName)
97+
: null}
98+
99+
{fields.includes('username')
100+
? this.renderInput(usernameValue, 'Username', onChangeUsername, 'ios-person-outline')
101+
: null}
102+
103+
{fields.includes('email')
104+
? this.renderInput(emailValue, 'Email Address', onChangeEmail, 'ios-mail', 'email-address')
105+
: null}
106+
107+
{fields.includes('password')
108+
? this.renderInput(passwordValue, 'Password', onChangePassword, 'ios-lock')
109+
: null}
110+
111+
<Button rounded block light style={styles.button} onPress={onSubmitForm}>
112+
{loading
113+
? <ActivityIndicator animating size="large" />
114+
: <Text>{buttonText}</Text>
115+
}
116+
</Button>
117+
<Button transparent block light style={styles.button} onPress={altAction}>
118+
<Text>{altText}</Text>
119+
</Button>
120+
{this.renderFooter()}
121+
</View>
122+
);
123+
}
124+
}
125+
126+
Form.propTypes = {
127+
emailValue: PropTypes.string,
128+
passwordValue: PropTypes.string,
129+
error: PropTypes.bool.isRequired,
130+
fields: PropTypes.array.isRequired,
131+
loading: PropTypes.bool.isRequired,
132+
altText: PropTypes.string.isRequired,
133+
altAction: PropTypes.func.isRequired,
134+
buttonText: PropTypes.string.isRequired,
135+
lastNameValue: PropTypes.string,
136+
usernameValue: PropTypes.string,
137+
fullNameValue: PropTypes.string,
138+
onSubmitForm: PropTypes.func.isRequired,
139+
onChangeEmail: PropTypes.func,
140+
onChangeUsername: PropTypes.func,
141+
onChangeFullName: PropTypes.func,
142+
onChangePassword: PropTypes.func,
143+
};
144+
145+
export default Form;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { StyleSheet } from 'react-native';
2+
3+
import { colors } from 'src/theme';
4+
5+
const styles = StyleSheet.create({
6+
form: {
7+
flex: 1.8,
8+
padding: 20,
9+
},
10+
textInput: {
11+
marginBottom: 15,
12+
},
13+
textInputIcon: {
14+
marginLeft: 10,
15+
fontSize: 30,
16+
color: '#FEFEFE',
17+
},
18+
button: {
19+
marginBottom: 15,
20+
},
21+
textInputField: {
22+
color: 'white',
23+
},
24+
footer: {
25+
position: 'absolute',
26+
left: 0,
27+
right: 0,
28+
bottom: 0,
29+
flexDirection: 'row',
30+
justifyContent: 'space-between',
31+
},
32+
forgotButton: {
33+
alignSelf: 'flex-end',
34+
},
35+
footerText: {
36+
fontSize: 12,
37+
color: colors.primary.lightest,
38+
},
39+
});
40+
41+
export default styles;

0 commit comments

Comments
 (0)