-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
ArrayFieldTemplate #437
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
ArrayFieldTemplate #437
Changes from 20 commits
3196e4f
ad691de
1f5a139
00c1f29
832b675
d02e94d
02b5ed5
0204e38
7a2ada7
7d3448b
1947a79
0fea21b
5cbc3a1
71782f5
47533df
1e464e2
3f380cc
1a865b8
20e1828
7bed3d4
b45a81b
e54dcfb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ A [live playground](https://mozilla-services.github.io/react-jsonschema-form/) i | |
| - [Form attributes](#form-attributes) | ||
| - [Advanced customization](#advanced-customization) | ||
| - [Field template](#field-template) | ||
| - [Array template](#array-template) | ||
| - [Custom widgets and fields](#custom-widgets-and-fields) | ||
| - [Custom widget components](#custom-widget-components) | ||
| - [Custom component registration](#custom-component-registration) | ||
|
|
@@ -700,6 +701,58 @@ The following props are passed to a custom field template component: | |
|
|
||
| > Note: you can only define a single field template for a form. If you need many, it's probably time to look at [custom fields](#custom-field-components) instead. | ||
|
|
||
| ### Array Template | ||
|
|
||
| Similarly to the `FieldTemplate` you can use an `ArrayFieldTemplate` to customize how your | ||
| arrays are rendered. This allows you to customize your array, and each element in the array. | ||
|
|
||
| ```jsx | ||
| function ArrayFieldTemplate(props) { | ||
| return ( | ||
| <div> | ||
| {props.items.map(element => element.children)} | ||
| {props.canAdd && <button onClick={props.onAddClick}></button>} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| render(( | ||
| <Form schema={schema} | ||
| ArrayFieldTemplate={ArrayFieldTemplate} />, | ||
| ), document.getElementById("app")); | ||
| ``` | ||
|
|
||
| Please see [customArray.js](https://github.com/mozilla-services/react-jsonschema-form/blob/master/playground/samples/customArray.js) for a better example. | ||
|
|
||
| The following props are passed to each `ArrayFieldTemplate`: | ||
|
|
||
| - `DescriptionField`: The generated `DescriptionField` (if you wanted to utilize it) | ||
| - `TitleField`: The generated `TitleField` (if you wanted to utilize it) | ||
| - `canAdd`: boolean whether new elements can be added to array | ||
| - `className`: string | ||
| - `disabled`: boolean | ||
| - `idSchema`: Object | ||
| - `items`: Object[] - Each of the items represent a child with properties described below. | ||
| - `onAddClick`: (event) => void | ||
| - `readonly`: boolean | ||
| - `required`: boolean | ||
| - `schema`: Object | ||
| - `title`: string | ||
|
|
||
| The following props are part of each element in `items`: | ||
|
|
||
| - `children`: JSX.Element | ||
| - `className`: string | ||
| - `disabled`: boolean | ||
| - `hasMoveDown`: boolean whether the element can be moved down | ||
| - `hasMoveUp`: boolean whether the element can be moved up | ||
| - `hasRemove`: boolean whether the element can be removed | ||
| - `hasToolbar`: boolean | ||
| - `index`: number | ||
| - `onDropIndexClick`: (index) => void | ||
| - `onReorderClick`: (index, newIndex) => void | ||
| - `readonly`: boolean | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. General remark for these two lists: it would be nice to have English sentences describing the purpose of each property, for consistency with the rest of the docs :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea! I'll improve the docs I wrote a bit! |
||
|
|
||
| ### Custom widgets and fields | ||
|
|
||
| The API allows to specify your own custom *widget* and *field* components: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import React, { Component } from "react"; | ||
|
|
||
| const ArrayFieldTemplate = props => ( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use a regular
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good 👍 |
||
| <div className={props.className}> | ||
|
|
||
| {props.items && props.items.map(element => ( | ||
| <div key={element.index}> | ||
| <div>{element.children}</div> | ||
| {element.hasMoveDown && | ||
| <button onClick={element.onReorderClick(element.index, element.index + 1)}>Down</button>} | ||
| {element.hasMoveUp && | ||
| <button onClick={element.onReorderClick(element.index, element.index - 1)}>Up</button>} | ||
| <button onClick={element.onDropIndexClick(element.index)}>Delete</button> | ||
| <hr /> | ||
| </div> | ||
| ))} | ||
|
|
||
| {props.canAdd && | ||
| <div className="row"> | ||
| <p className="col-xs-3 col-xs-offset-9 array-item-add text-right"> | ||
| <button onClick={props.onAddClick} type="button">Custom +</button> | ||
| </p> | ||
| </div>} | ||
|
|
||
| </div> | ||
| ); | ||
|
|
||
| module.exports = { | ||
| schema: { | ||
| title: "Custom array of strings", | ||
| type: "array", | ||
| items: { | ||
| type: "string" | ||
| } | ||
| }, | ||
| formData: ["react", "jsonschema", "form"], | ||
| ArrayFieldTemplate | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import date from "./date"; | |
| import validation from "./validation"; | ||
| import files from "./files"; | ||
| import single from "./single"; | ||
| import customArray from "./customArray"; | ||
|
|
||
| export const samples = { | ||
| Simple: simple, | ||
|
|
@@ -27,5 +28,6 @@ export const samples = { | |
| "Date & time": date, | ||
| Validation: validation, | ||
| Files: files, | ||
| Single: single | ||
| }; | ||
| Single: single, | ||
| "Custom Array": customArray | ||
| }; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: all files need an empty line at EOF.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Array field template maybe