Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sumbit method to enable the programmatic submission #1058

Merged
merged 1 commit into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ A [live playground](https://mozilla-services.github.io/react-jsonschema-form/) i
- [Form data changes](#form-data-changes)
- [Form field blur events](#form-field-blur-events)
- [Form field focus events](#form-field-focus-events)
- [Submit form programmatically](#submit-form-programmatically)
- [Form customization](#form-customization)
- [The uiSchema object](#the-uischema-object)
- [Alternative widgets](#alternative-widgets)
Expand Down Expand Up @@ -240,6 +241,22 @@ Sometimes you may want to trigger events or modify external state when a field h

Sometimes you may want to trigger events or modify external state when a field has been focused, so you can pass an `onFocus` handler, which will receive the id of the input that is focused and the field value.

### Submit form programmatically
You can use the reference to get your `Form` component and call the `submit` method to submit the form programmatically without a submit button.
This method will dispatch the `submit` event of the form, and the function, that is passed to `onSubmit` props, will be called.

```js
const onSubmit = ({formData}) => console.log("Data submitted: ", formData);
let yourForm;

render((
<Form schema={schema}
onSubmit={onSubmit} ref={(form) => {yourForm = form;}}/>
), document.getElementById("app"));

yourForm.submit();
```

## Form customization

### The `uiSchema` object
Expand Down
12 changes: 11 additions & 1 deletion src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default class Form extends Component {
) {
this.props.onChange(this.state);
}
this.formElement = null;
}

componentWillReceiveProps(nextProps) {
Expand Down Expand Up @@ -185,6 +186,12 @@ export default class Form extends Component {
};
}

submit() {
if (this.formElement) {
this.formElement.dispatchEvent(new Event("submit"));
}
}

render() {
const {
children,
Expand Down Expand Up @@ -218,7 +225,10 @@ export default class Form extends Component {
encType={enctype}
acceptCharset={acceptcharset}
noValidate={noHtml5Validate}
onSubmit={this.onSubmit}>
onSubmit={this.onSubmit}
ref={form => {
this.formElement = form;
}}>
{this.renderErrors()}
<_SchemaField
schema={schema}
Expand Down