|
| 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; |
0 commit comments