Skip to content

Commit e1fd2aa

Browse files
Allow user to provide custom buttons to the form
1 parent ea8f8c1 commit e1fd2aa

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,20 @@ 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+
234248
## Development server
235249

236250
```

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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ 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+
56+
const node = findDOMNode(comp);
57+
expect(node.querySelectorAll("button[type=submit]"))
58+
.to.have.length.of(2);
59+
});
4660
});
4761

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

0 commit comments

Comments
 (0)