-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add ObjectFieldTemplate #653
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
Changes from 4 commits
a897320
2d95ef9
5b24519
f75760b
de3de0c
1356033
1213dce
ad0a40f
1e193dd
890f970
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 |
|---|---|---|
|
|
@@ -56,6 +56,7 @@ A [live playground](https://mozilla-services.github.io/react-jsonschema-form/) i | |
| - [Advanced customization](#advanced-customization) | ||
| - [Field template](#field-template) | ||
| - [Array Field Template](#array-field-template) | ||
| - [Object Field Template](#object-field-template) | ||
| - [Error List template](#error-list-template) | ||
| - [Custom widgets and fields](#custom-widgets-and-fields) | ||
| - [Custom widget components](#custom-widget-components) | ||
|
|
@@ -870,6 +871,51 @@ The following props are part of each element in `items`: | |
| - `onReorderClick: (index, newIndex) => (event) => void`: Returns a function that swaps the items at `index` with `newIndex`. | ||
| - `readonly`: A boolean value stating if the array item is readonly. | ||
|
|
||
| ### Object Field Template | ||
|
|
||
| Similarly to the `FieldTemplate` you can use an `ObjectFieldTemplate` to customize how your | ||
| objects are rendered. | ||
|
|
||
| ```jsx | ||
| function ObjectFieldTemplate(props) { | ||
| return ( | ||
| <div> | ||
| {props.title} | ||
| {props.description} | ||
| {props.properties.map(element => <div className="property-wrapper">{element.children}</div>)} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| render(( | ||
| <Form schema={schema} | ||
| ObjectFieldTemplate={ObjectFieldTemplate} />, | ||
| ), document.getElementById("app")); | ||
| ``` | ||
|
|
||
| Please see [customObject.js](https://github.com/mozilla-services/react-jsonschema-form/blob/master/playground/samples/customObject.js) for a better example. | ||
|
|
||
| The following props are passed to each `ObjectFieldTemplate`: | ||
|
|
||
| - `DescriptionField`: The generated `DescriptionField` (if you wanted to utilize it) | ||
| - `TitleField`: The generated `TitleField` (if you wanted to utilize it). | ||
| - `title`: A string value containing the title for the object. | ||
| - `description`: A string value containing the description for the object. | ||
| - `properties`: An array of object representing the properties in the array. Each of the properties represent a child with properties described below. | ||
| - `required`: A boolean value stating if the object is required. | ||
| - `schema`: The schema object for this object. | ||
| - `uiSchema`: The uiSchema object for this object field. | ||
| - `idSchema`: An object containing the id for this object & ids for it's properties. | ||
| - `formData`: The form data for the object. | ||
| - `formContext`: The `formContext` object that you passed to Form. | ||
|
|
||
| The following props are part of each element in `items`: | ||
|
Contributor
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. Is |
||
|
|
||
| - `children`: The html for the property's content. | ||
|
Contributor
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. I don't like |
||
| - `name`: A string representing the property name. | ||
| - `disabled`: A boolean value stating if the object property is disabled. | ||
| - `index`: A number stating the index the property occurs in the `properties`. | ||
|
Contributor
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. I don't think we need to expose this, because it should be the same as the index in the
Contributor
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. I still don't really understand why we need to expose this. Knowing that it comes from
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. I missed this one... You're right, I removed it. |
||
| - `readonly`: A boolean value stating if the property is readonly. | ||
|
Contributor
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. Missing blank line. |
||
| ### Error List template | ||
|
|
||
| To take control over how the form errors are displayed, you can define an *error list template* for your form. This list is the form global error list that appears at the top of your forms. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import React from "react"; | ||
|
|
||
| function ObjectFieldTemplate({ TitleField, properties, title, description }) { | ||
| return ( | ||
| <div> | ||
| <TitleField title={title} /> | ||
| <div className="row"> | ||
| {properties.map(prop => ( | ||
| <div className="col-lg-2 col-md-4 col-sm-6 col-xs-12">{prop}</div> | ||
| ))} | ||
| </div> | ||
| {description} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| module.exports = { | ||
| schema: { | ||
| title: "A registration form", | ||
| description: | ||
| "This is the same as the simple form, but it is rendered as a bootstrap grid. Try shrinking the browser window to see it in action.", | ||
| type: "object", | ||
| required: ["firstName", "lastName"], | ||
| properties: { | ||
| firstName: { | ||
| type: "string", | ||
| title: "First name", | ||
| }, | ||
| lastName: { | ||
| type: "string", | ||
| title: "Last name", | ||
| }, | ||
| age: { | ||
| type: "integer", | ||
| title: "Age", | ||
| }, | ||
| bio: { | ||
| type: "string", | ||
| title: "Bio", | ||
| }, | ||
| password: { | ||
| type: "string", | ||
| title: "Password", | ||
| minLength: 3, | ||
| }, | ||
| telephone: { | ||
| type: "string", | ||
| title: "Telephone", | ||
| minLength: 10, | ||
| }, | ||
| }, | ||
| }, | ||
| formData: { | ||
| firstName: "Chuck", | ||
| lastName: "Norris", | ||
| age: 75, | ||
| bio: "Roundhouse kicking asses since 1940", | ||
| password: "noneed", | ||
| }, | ||
| ObjectFieldTemplate, | ||
| }; |
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.
I'm kind of ambivalent about passing these field components to the template. Client code already has the opportunity to override the entire template, using any
DescriptionFieldandTitleFielddefinition they want, and they can re-import DescriptionField and TitleField themselves if they want them. So what's the value?I don't think
generatedis the right word here -- it's the one taken from the registry.How about changing
if you wanted totoin case you wanted to?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.
I agree that passing
DescriptionFieldandTitleFieldis useless. However,ArrayFieldalso passes them toArrayFieldTemplate, so at least we are consistenly useless :)I copy pasted the descriptions from
ArrayFieldTemplatemanual entry. I changed the descriptions in both manual entries now to "TheDescriptionField/TitleFieldfrom the registry (in case you wanted to utilize it)"