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

Allow user to provide custom buttons to the form #34

Merged
merged 4 commits into from
Jan 28, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,22 @@ render((

If you're curious how this could ever be useful, have a look at the [Kinto formbuilder](https://github.com/Kinto/formbuilder) repository to see how it's used to provide editing capabilities to any form field.

## Custom buttons

You can provide custom buttons to your form via the `Form` component's `children`. A default submit button will be rendered if you don't provide children to the `Form` component.

```jsx
render(
<Form schema={schema}>
<div>
<button type="submit">Submit</button>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add an important note that there should be either a button or input with type=submit in order to trigger the form submission.

<button>Cancel</button>
</div>
</Form>);
```

**Warning:** there should be a button or an input with `type="submit"` to trigger the form submission (and then the form validation).

## Development server

```
Expand Down
4 changes: 2 additions & 2 deletions src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default class Form extends Component {
}

render() {
const {schema, uiSchema} = this.props;
const {children, schema, uiSchema} = this.props;
const {formData} = this.state;
const _SchemaField = this.props.SchemaField || SchemaField;
return (
Expand All @@ -86,7 +86,7 @@ export default class Form extends Component {
formData={formData}
onChange={this.onChange.bind(this)}
SchemaField={_SchemaField}/>
<p><button type="submit">Submit</button></p>
{ children ? children : <p><button type="submit">Submit</button></p> }
</form>
);
}
Expand Down
30 changes: 30 additions & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,36 @@ describe("Form", () => {
expect(node.querySelectorAll("button[type=submit]"))
.to.have.length.of(1);
});

it("should render children buttons", () => {
const props = {schema: {}};
const comp = renderIntoDocument(
<Form {...props}>
<button type="submit">Submit</button>
<button type="submit">Another submit</button>
</Form>
);
const node = findDOMNode(comp);
expect(node.querySelectorAll("button[type=submit]"))
.to.have.length.of(2);
});
});

describe("Custom submit buttons", () => {
it("should submit the form when clicked", () => {
const onSubmit = sandbox.spy();
const comp = renderIntoDocument(
<Form onSubmit={ onSubmit } schema={ {} }>
<button type="submit">Submit</button>
<button type="submit">Another submit</button>
</Form>
);
const node = findDOMNode(comp);
const buttons = node.querySelectorAll("button[type=submit]");
buttons[0].click();
buttons[1].click();
sinon.assert.calledTwice(onSubmit);
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should now add a test ensuring that clicking on one of these custom buttons actually triggers form submission.


describe("Custom SchemaField", () => {
Expand Down