-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Handle circular references #557
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 all commits
778fb91
32324cb
6ebb3e4
132bc60
114fb5e
9d0b741
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 |
|---|---|---|
|
|
@@ -45,7 +45,7 @@ class ObjectField extends Component { | |
| onBlur, | ||
| } = this.props; | ||
| const { definitions, fields, formContext } = this.props.registry; | ||
| const { SchemaField, TitleField, DescriptionField } = fields; | ||
| const { ObjectPropertyField, TitleField, DescriptionField } = fields; | ||
| const schema = retrieveSchema(this.props.schema, definitions); | ||
| const title = schema.title === undefined ? name : schema.title; | ||
| let orderedProperties; | ||
|
|
@@ -79,8 +79,12 @@ class ObjectField extends Component { | |
| formContext={formContext} | ||
| />} | ||
| {orderedProperties.map((name, index) => { | ||
| const isDefault = | ||
| !(name in formData) || | ||
| typeof Object.getOwnPropertyDescriptor(formData, name).get === | ||
| "function"; | ||
|
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. Here I'm trying determine whether the value in
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. Yes, I have to say this is super ugly, and passing explicit |
||
| return ( | ||
| <SchemaField | ||
| <ObjectPropertyField | ||
| key={index} | ||
| name={name} | ||
| required={this.isRequired(name)} | ||
|
|
@@ -94,6 +98,7 @@ class ObjectField extends Component { | |
| registry={this.props.registry} | ||
| disabled={disabled} | ||
| readonly={readonly} | ||
| isDefault={isDefault} | ||
| /> | ||
| ); | ||
| })} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import React, { Component, PropTypes } from "react"; | ||
|
|
||
| import { getDefaultRegistry } from "../../utils"; | ||
|
|
||
| class ObjectPropertyField extends Component { | ||
| static defaultProps = { | ||
| uiSchema: {}, | ||
| errorSchema: {}, | ||
| idSchema: {}, | ||
| registry: getDefaultRegistry(), | ||
| disabled: false, | ||
| readonly: false, | ||
| autofocus: false, | ||
| isDefault: true, | ||
| }; | ||
|
|
||
| constructor(props) { | ||
| super(props); | ||
| this.state = { present: props.required || !props.isDefault }; | ||
| } | ||
|
|
||
| show = event => { | ||
| event.preventDefault(); | ||
| this.setState({ present: true }); | ||
| }; | ||
|
|
||
| hide = event => { | ||
| event.preventDefault(); | ||
| this.setState({ present: false }); | ||
| }; | ||
|
|
||
| render() { | ||
| const title = this.props.schema.title === undefined | ||
| ? this.props.name | ||
| : this.props.schema.title; | ||
|
|
||
| if (this.state.present) { | ||
| const SchemaField = this.props.registry.fields.SchemaField; | ||
| const { isDefault, ...props } = this.props; | ||
| return ( | ||
| <div> | ||
| <SchemaField {...props} /> | ||
| {this.props.required || | ||
| <button onClick={this.hide}>REMOVE {title}</button>} | ||
| </div> | ||
| ); | ||
| } else { | ||
| return <button onClick={this.show}>ADD {title}</button>; | ||
|
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. No doubt these
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'm pretty nervous about the UX in general. I know that @n1k0 took a swing at collapsibility before in #268 and wasn't able to find a UX that he found felt right for most use cases, and this feels like a similar situation to me. Can we add this only if we are in a circular-schema situation? (N.B. I haven't actually tried running the code so this may not be as intrusive as I imagine it being.) |
||
| } | ||
| } | ||
| } | ||
|
|
||
| if (process.env.NODE_ENV !== "production") { | ||
| ObjectPropertyField.propTypes = { | ||
| schema: PropTypes.object.isRequired, | ||
| uiSchema: PropTypes.object, | ||
| idSchema: PropTypes.object, | ||
| formData: PropTypes.any, | ||
| errorSchema: PropTypes.object, | ||
| registry: PropTypes.shape({ | ||
| widgets: PropTypes.objectOf( | ||
| PropTypes.oneOfType([PropTypes.func, PropTypes.object]) | ||
| ).isRequired, | ||
| fields: PropTypes.objectOf(PropTypes.func).isRequired, | ||
| definitions: PropTypes.object.isRequired, | ||
| ArrayFieldTemplate: PropTypes.func, | ||
| FieldTemplate: PropTypes.func, | ||
| formContext: PropTypes.object.isRequired, | ||
| }), | ||
| isDefault: PropTypes.bool, | ||
| }; | ||
| } | ||
|
|
||
| export default ObjectPropertyField; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import BooleanField from "./BooleanField"; | |
| import DescriptionField from "./DescriptionField"; | ||
| import NumberField from "./NumberField"; | ||
| import ObjectField from "./ObjectField"; | ||
| import ObjectPropertyField from "./ObjectPropertyField"; | ||
| import SchemaField from "./SchemaField"; | ||
| import StringField from "./StringField"; | ||
| import TitleField from "./TitleField"; | ||
|
|
@@ -14,6 +15,7 @@ export default { | |
| DescriptionField, | ||
| NumberField, | ||
| ObjectField, | ||
| ObjectPropertyField, | ||
|
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'm not sure we really need to expose |
||
| SchemaField, | ||
| StringField, | ||
| TitleField, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
With circular schema, we must avoid rendering everything: object properties should only be rendered when necessary. I define a new component,
ObjectPropertyFieldthat only renders the underlyingSchemaFieldwhen necessary—i.e. if the schema requires the property to be present, if theformDatacontains an explicit value for it, or if the user explicitly requests the property to be shown.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.
Consider putting this comment as JSDoc on the
ObjectPropertyFieldclass.