Skip to content

Instantly share code, notes, and snippets.

@NicholasKuchiniski
Created May 24, 2018 14:34
Show Gist options
  • Save NicholasKuchiniski/24e43b939c8a65ffd0f10c37a2c92f47 to your computer and use it in GitHub Desktop.
Save NicholasKuchiniski/24e43b939c8a65ffd0f10c37a2c92f47 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import TextField from '@material-ui/core/TextField';
export class EmailTextField extends Component {
state = {
error: undefined
}
isEmail = (email) => {
var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
if (!filter.test(email)) {
return false
} else {
return true;
}
};
handleChange = (e) => {
const email = e.target.value;
const isEmail = this.isEmail(email);
const { onChange } = this.props;
if(isEmail){
this.setState({
error: undefined
});
onChange(e);
} else {
this.setState({
error: 'Email inválido'
});
onChange(e);
}
}
render() {
const { value, label } = this.props;
const { error } = this.state;
return (
<div>
<TextField
error={Boolean(error)}
helperText={error}
label={label}
value={value}
onChange={this.handleChange}
fullWidth
/>
</div>
)
}
};
EmailTextField.propTypes = {
onChange: PropTypes.func.isRequired,
value: PropTypes.string.isRequired,
label: PropTypes.string.isRequired
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment