|
1 | | -import React, { Component } from 'react' |
2 | | -import { StyleSheet } from 'react-native' |
| 1 | +import React from 'react' |
3 | 2 | import PropTypes from 'prop-types' |
4 | 3 |
|
5 | | -import Layout from '../../constants/Layout' |
| 4 | +// Components |
| 5 | +import { Button, FormControl, Input, InputLabel } from '@material-ui/core' |
6 | 6 |
|
7 | | -class SignInForm extends Component { |
| 7 | +class SignInForm extends React.Component { |
8 | 8 | constructor(props) { |
9 | 9 | super(props) |
| 10 | + |
10 | 11 | this.state = { |
11 | 12 | email: '', |
12 | 13 | password: '', |
13 | | - inputValidity: true, |
| 14 | + |
| 15 | + validity: { email: false }, |
14 | 16 | } |
15 | 17 |
|
16 | | - this.onChangePassword = this.onChangePassword.bind(this) |
17 | 18 | this.onChangeEmail = this.onChangeEmail.bind(this) |
| 19 | + this.onChangePassword = this.onChangePassword.bind(this) |
| 20 | + |
| 21 | + this.getValidity = this.getValidity.bind(this) |
18 | 22 | } |
19 | 23 |
|
20 | | - onChangeEmail(email) { |
| 24 | + onChangeEmail({ target: { value: email } }) { |
21 | 25 | const validity = email.match( |
22 | 26 | // eslint-disable-next-line no-useless-escape |
23 | 27 | /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ |
24 | 28 | ) |
25 | | - this.setState({ email, inputValidity: validity }) |
| 29 | + |
| 30 | + this.setState(oldState => ({ |
| 31 | + email, |
| 32 | + validity: { ...oldState.validity, email: !!validity }, |
| 33 | + })) |
26 | 34 | } |
27 | 35 |
|
28 | | - onChangePassword(password) { |
| 36 | + onChangePassword({ target: { value: password } }) { |
29 | 37 | this.setState({ password }) |
30 | 38 | } |
31 | 39 |
|
| 40 | + getValidity() { |
| 41 | + return this.state.validity.email |
| 42 | + } |
| 43 | + |
32 | 44 | render() { |
| 45 | + const { onSubmit } = this.props |
| 46 | + const { email, password } = this.state |
| 47 | + |
33 | 48 | return ( |
34 | | - <Form style={styles.form}> |
35 | | - <Item inlineLabel regular style={styles.item}> |
36 | | - <Input |
37 | | - placeholder="Email" |
38 | | - style={styles.input} |
39 | | - onChangeText={this.onChangeEmail} |
40 | | - keyboardType="email-address" |
41 | | - autoCapitalize="none" |
42 | | - value={this.state.email} |
43 | | - /> |
44 | | - </Item> |
45 | | - <Item inlineLabel last regular style={styles.item}> |
| 49 | + <FormControl> |
| 50 | + <FormControl> |
| 51 | + <InputLabel>E-mail</InputLabel> |
| 52 | + <Input type="email" value={email} onChange={this.onChangeEmail} /> |
| 53 | + </FormControl> |
| 54 | + |
| 55 | + <FormControl> |
| 56 | + <InputLabel>Contraseña</InputLabel> |
46 | 57 | <Input |
47 | | - placeholder="Contraseña" |
48 | | - style={styles.input} |
49 | | - onChangeText={this.onChangePassword} |
50 | | - secureTextEntry |
51 | | - value={this.state.password} |
| 58 | + type="password" |
| 59 | + value={password} |
| 60 | + onChange={this.onChangePassword} |
52 | 61 | /> |
53 | | - </Item> |
| 62 | + </FormControl> |
| 63 | + |
54 | 64 | <Button |
55 | | - block |
56 | | - borderRadius={10} |
57 | | - style={styles.button} |
58 | | - disabled={!this.state.inputValidity} |
59 | | - onPress={() => |
60 | | - this.props.onSend(this.state.email, this.state.password) |
61 | | - } |
| 65 | + disabled={!this.getValidity()} |
| 66 | + onClick={() => onSubmit({ email, password })} |
62 | 67 | > |
63 | | - <Text>Entrar</Text> |
| 68 | + Ingresar |
64 | 69 | </Button> |
65 | | - </Form> |
| 70 | + </FormControl> |
66 | 71 | ) |
67 | 72 | } |
68 | 73 | } |
69 | 74 |
|
70 | 75 | SignInForm.propTypes = { |
71 | | - onSend: PropTypes.func.isRequired, |
| 76 | + onSubmit: PropTypes.func.isRequired, |
72 | 77 | } |
73 | 78 |
|
74 | | -const styles = StyleSheet.create({ |
75 | | - button: { |
76 | | - backgroundColor: '#0000FF', |
77 | | - marginTop: 20, |
78 | | - }, |
79 | | - form: { |
80 | | - alignItems: 'center', |
81 | | - height: 250, |
82 | | - margin: 15, |
83 | | - }, |
84 | | - input: { |
85 | | - width: Layout.window.width * 0.85, |
86 | | - }, |
87 | | - item: { |
88 | | - borderRadius: 10, |
89 | | - marginBottom: 20, |
90 | | - paddingHorizontal: 10, |
91 | | - }, |
92 | | -}) |
93 | | - |
94 | 79 | export default SignInForm |
0 commit comments