-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form.js
44 lines (35 loc) · 1.05 KB
/
Form.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import React, { Component } from "react";
import PropTypes from "prop-types";
/**
* The Form class should be used instead of any form
* element on the page which has to be validated.
* Use the property "as" to define the component it should
* actually render.
*/
class Form extends Component {
constructor(props) {
super(props);
this.submitHandler = this.submitHandler.bind(this);
}
submitHandler(e) {
if (!this.context)
throw new Error(
"The Field component was used inside" +
"of a Component which is not wrapped into a validation HOC!"
);
e.preventDefault();
this.context.submit(e);
}
render() {
const { as: ActualElement = "form" } = this.props;
const propsToPass = Object.assign({}, this.props, {
onSubmit: this.submitHandler
});
delete propsToPass.as;
return React.createElement(ActualElement, propsToPass);
}
}
Form.contextTypes = {
submit: PropTypes.func
};
export { Form };