accounts-frontend/src/components/ui/form/Form.jsx

104 lines
2.7 KiB
React
Raw Normal View History

2016-05-02 12:45:42 +05:30
import React, { Component, PropTypes } from 'react';
import classNames from 'classnames';
import FormModel from './FormModel';
2016-05-02 12:45:42 +05:30
import styles from './form.scss';
export default class Form extends Component {
static displayName = 'Form';
static propTypes = {
id: PropTypes.string, // and id, that uniquely identifies form contents
isLoading: PropTypes.bool,
form: PropTypes.instanceOf(FormModel),
2016-05-02 12:45:42 +05:30
onSubmit: PropTypes.func,
onInvalid: PropTypes.func,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node
])
};
static defaultProps = {
id: 'default',
isLoading: false,
onSubmit() {},
onInvalid() {}
};
state = {
isTouched: false
};
componentWillReceiveProps(nextProps) {
if (nextProps.id !== this.props.id) {
this.setState({
isTouched: false
});
}
}
render() {
const {isLoading} = this.props;
return (
<form
className={classNames(
styles.form,
{
[styles.isFormLoading]: isLoading,
[styles.formTouched]: this.state.isTouched
}
)}
onSubmit={this.onFormSubmit}
noValidate
>
{this.props.children}
</form>
);
}
onFormSubmit = (event) => {
event.preventDefault();
if (!this.state.isTouched) {
this.setState({
isTouched: true
});
}
const form = event.currentTarget;
if (form.checkValidity()) {
this.props.onSubmit();
} else {
const invalidEls = form.querySelectorAll(':invalid');
const errors = {};
invalidEls[0].focus(); // focus on first error
Array.from(invalidEls).reduce((errors, el) => {
if (!el.name) {
console.warn('Found an element without name', el);
return errors;
}
let errorMessage = el.validationMessage;
if (el.validity.valueMissing) {
errorMessage = `error.${el.name}_required`;
} else if (el.validity.typeMismatch) {
errorMessage = `error.${el.name}_invalid`;
}
errors[el.name] = errorMessage;
2016-05-02 12:45:42 +05:30
return errors;
}, errors);
2016-05-02 12:45:42 +05:30
this.props.form && this.props.form.setErrors(errors);
this.props.onInvalid(errors);
2016-05-02 12:45:42 +05:30
}
};
}