Skip to content

Commit

Permalink
feat(Form): add submit to component
Browse files Browse the repository at this point in the history
This makes it easier to use submit as if the component was a raw form (via 'standard' )
  • Loading branch information
TheSharpieOne committed May 7, 2018
1 parent e3124af commit 4e10dd9
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions src/Form.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { mapToCssModules } from './utils';
Expand All @@ -15,26 +15,46 @@ const propTypes = {
const defaultProps = {
tag: 'form',
};
class Form extends Component {
constructor(props) {
super(props);
this.getRef = this.getRef.bind(this);
this.submit = this.submit.bind(this);
}

const Form = (props) => {
const {
className,
cssModule,
inline,
tag: Tag,
innerRef,
...attributes
} = props;

const classes = mapToCssModules(classNames(
className,
inline ? 'form-inline' : false
), cssModule);

return (
<Tag {...attributes} ref={innerRef} className={classes} />
);
};
getRef(ref) {
if (this.props.innerRef) {
this.props.innerRef(ref);
}
this.ref = ref;
}

submit() {
if (this.ref) {
this.ref.submit();
}
}

render() {
const {
className,
cssModule,
inline,
tag: Tag,
innerRef,
...attributes
} = this.props;

const classes = mapToCssModules(classNames(
className,
inline ? 'form-inline' : false
), cssModule);

return (
<Tag {...attributes} ref={innerRef} className={classes} />
);
}
}

Form.propTypes = propTypes;
Form.defaultProps = defaultProps;
Expand Down

0 comments on commit 4e10dd9

Please sign in to comment.