Skip to content

Commit 4d43126

Browse files
committed
Merge pull request #34 from cedricmessiant/master
Allow user to provide custom buttons to the form
2 parents ea8f8c1 + c890062 commit 4d43126

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,22 @@ render((
231231
232232
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.
233233

234+
## Custom buttons
235+
236+
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.
237+
238+
```jsx
239+
render(
240+
<Form schema={schema}>
241+
<div>
242+
<button type="submit">Submit</button>
243+
<button>Cancel</button>
244+
</div>
245+
</Form>);
246+
```
247+
248+
**Warning:** there should be a button or an input with `type="submit"` to trigger the form submission (and then the form validation).
249+
234250
## Development server
235251

236252
```

src/components/Form.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export default class Form extends Component {
7474
}
7575

7676
render() {
77-
const {schema, uiSchema} = this.props;
77+
const {children, schema, uiSchema} = this.props;
7878
const {formData} = this.state;
7979
const _SchemaField = this.props.SchemaField || SchemaField;
8080
return (
@@ -86,7 +86,7 @@ export default class Form extends Component {
8686
formData={formData}
8787
onChange={this.onChange.bind(this)}
8888
SchemaField={_SchemaField}/>
89-
<p><button type="submit">Submit</button></p>
89+
{ children ? children : <p><button type="submit">Submit</button></p> }
9090
</form>
9191
);
9292
}

test/index_test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,36 @@ describe("Form", () => {
4343
expect(node.querySelectorAll("button[type=submit]"))
4444
.to.have.length.of(1);
4545
});
46+
47+
it("should render children buttons", () => {
48+
const props = {schema: {}};
49+
const comp = renderIntoDocument(
50+
<Form {...props}>
51+
<button type="submit">Submit</button>
52+
<button type="submit">Another submit</button>
53+
</Form>
54+
);
55+
const node = findDOMNode(comp);
56+
expect(node.querySelectorAll("button[type=submit]"))
57+
.to.have.length.of(2);
58+
});
59+
});
60+
61+
describe("Custom submit buttons", () => {
62+
it("should submit the form when clicked", () => {
63+
const onSubmit = sandbox.spy();
64+
const comp = renderIntoDocument(
65+
<Form onSubmit={ onSubmit } schema={ {} }>
66+
<button type="submit">Submit</button>
67+
<button type="submit">Another submit</button>
68+
</Form>
69+
);
70+
const node = findDOMNode(comp);
71+
const buttons = node.querySelectorAll("button[type=submit]");
72+
buttons[0].click();
73+
buttons[1].click();
74+
sinon.assert.calledTwice(onSubmit);
75+
});
4676
});
4777

4878
describe("Custom SchemaField", () => {

0 commit comments

Comments
 (0)