cpui-components
Version:
136 lines (113 loc) • 5.15 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.EmailInputGroup = undefined;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var REGEXP_EMAIL = /^(([^<>()[\]\\.,;:\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,}))$/;
function validateEmail(value) {
return REGEXP_EMAIL.test(value);
}
var EmailInputGroup = exports.EmailInputGroup = function (_Component) {
_inherits(EmailInputGroup, _Component);
function EmailInputGroup() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, EmailInputGroup);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = EmailInputGroup.__proto__ || Object.getPrototypeOf(EmailInputGroup)).call.apply(_ref, [this].concat(args))), _this), _this.getInitialState = function () {
return {
isValid: true,
validationIsActive: _this.props.alwaysValidate
};
}, _this.componentDidMount = function () {
if (_this.state.validationIsActive) {
_this.validateInput(_this.props.value);
}
}, _this.componentWillReceiveProps = function (newProps) {
if (_this.state.validationIsActive) {
if (newProps.value !== _this.props.value && newProps.value !== _this._lastChangeValue && !newProps.alwaysValidate) {
// reset validation state if the value was changed outside the component
return _this.setState({
isValid: true,
validationIsActive: false
});
}
_this.validateInput(newProps.value);
}
}, _this.handleChange = function (e) {
_this._lastChangeValue = e.target.value;
if (_this.props.onChange) _this.props.onChange(e);
}, _this.handleBlur = function () {
if (!_this.props.alwaysValidate) {
_this.setState({ validationIsActive: false });
}
_this.validateInput(_this.props.value);
}, _this.validateInput = function (value) {
var newState = {
isValid: true
};
if (value.length && !validateEmail(value)) {
newState.isValid = false;
}
if (!value.length && _this.props.required) {
newState.isValud = false;
}
if (!newState.isValid) {
newState.validationIsActive = true;
}
_this.setState(newState);
}, _this.render = function () {
var validationMessage = null;
if (!_this.state.isValid) {
validationMessage = _react2.default.createElement(
'div',
{ className: 'form-validation is-invalid' },
_this.props.value.length ? _this.props.invalidMessage : _this.props.requiredMessage
);
}
var formGroupClass = (0, _classnames2.default)('FormField', {
'is-invalid': !_this.state.isValid
}, _this.props.className);
var componentLabel = _this.props.label ? _react2.default.createElement(
'label',
{ className: 'FormLabel', htmlFor: 'inputEmail' },
_this.props.label
) : null;
return _react2.default.createElement(
'div',
{ className: formGroupClass },
componentLabel,
_react2.default.createElement('input', { onChange: _this.handleChange, onBlur: _this.handleBlur, value: _this.props.value, type: 'email', className: 'FormInput', placeholder: 'Enter email', id: 'inputEmail' }),
validationMessage
);
}, _temp), _possibleConstructorReturn(_this, _ret);
}
return EmailInputGroup;
}(_react.Component);
EmailInputGroup.propTypes = {
alwaysValidate: _propTypes2.default.bool,
className: _propTypes2.default.string,
invalidMessage: _propTypes2.default.string,
label: _propTypes2.default.string,
onChange: _propTypes2.default.func,
required: _propTypes2.default.bool,
requiredMessage: _propTypes2.default.string,
value: _propTypes2.default.string
};
EmailInputGroup.defaultProps = {
requiredMessage: 'Email address is required',
invalidMessage: 'Please enter a valid email address'
};
exports.default = EmailInputGroup;
;