Skip to content

Commit 4e10dd9

Browse files
committed
feat(Form): add submit to component
This makes it easier to use submit as if the component was a raw form (via 'standard' )
1 parent e3124af commit 4e10dd9

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

src/Form.js

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import classNames from 'classnames';
44
import { mapToCssModules } from './utils';
@@ -15,26 +15,46 @@ const propTypes = {
1515
const defaultProps = {
1616
tag: 'form',
1717
};
18+
class Form extends Component {
19+
constructor(props) {
20+
super(props);
21+
this.getRef = this.getRef.bind(this);
22+
this.submit = this.submit.bind(this);
23+
}
1824

19-
const Form = (props) => {
20-
const {
21-
className,
22-
cssModule,
23-
inline,
24-
tag: Tag,
25-
innerRef,
26-
...attributes
27-
} = props;
28-
29-
const classes = mapToCssModules(classNames(
30-
className,
31-
inline ? 'form-inline' : false
32-
), cssModule);
33-
34-
return (
35-
<Tag {...attributes} ref={innerRef} className={classes} />
36-
);
37-
};
25+
getRef(ref) {
26+
if (this.props.innerRef) {
27+
this.props.innerRef(ref);
28+
}
29+
this.ref = ref;
30+
}
31+
32+
submit() {
33+
if (this.ref) {
34+
this.ref.submit();
35+
}
36+
}
37+
38+
render() {
39+
const {
40+
className,
41+
cssModule,
42+
inline,
43+
tag: Tag,
44+
innerRef,
45+
...attributes
46+
} = this.props;
47+
48+
const classes = mapToCssModules(classNames(
49+
className,
50+
inline ? 'form-inline' : false
51+
), cssModule);
52+
53+
return (
54+
<Tag {...attributes} ref={innerRef} className={classes} />
55+
);
56+
}
57+
}
3858

3959
Form.propTypes = propTypes;
4060
Form.defaultProps = defaultProps;

0 commit comments

Comments
 (0)