-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add preliminary support for object "additionalProperties" #1021
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
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| module.exports = { | ||
| schema: { | ||
| title: "A customizable registration form", | ||
| description: "A simple form with additional properties example.", | ||
| type: "object", | ||
| required: ["firstName", "lastName"], | ||
| additionalProperties: { | ||
| type: "string", | ||
| }, | ||
| properties: { | ||
| firstName: { | ||
| type: "string", | ||
| title: "First name", | ||
| }, | ||
| lastName: { | ||
| type: "string", | ||
| title: "Last name", | ||
| }, | ||
| }, | ||
| }, | ||
| uiSchema: { | ||
| firstName: { | ||
| "ui:autofocus": true, | ||
| "ui:emptyValue": "", | ||
| }, | ||
| }, | ||
| formData: { | ||
| firstName: "Chuck", | ||
| lastName: "Norris", | ||
| assKickCount: "infinity", | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import React from "react"; | ||
| import IconButton from "./IconButton"; | ||
|
|
||
| export default function AddButton({ className, onClick, disabled }) { | ||
| return ( | ||
| <div className="row"> | ||
| <p className={`col-xs-3 col-xs-offset-9 text-right ${className}`}> | ||
| <IconButton | ||
| type="info" | ||
| icon="plus" | ||
| className="btn-add col-xs-12" | ||
| tabIndex="0" | ||
| onClick={onClick} | ||
| disabled={disabled} | ||
| /> | ||
| </p> | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import React from "react"; | ||
|
|
||
| export default function IconButton(props) { | ||
| const { type = "default", icon, className, ...otherProps } = props; | ||
| return ( | ||
| <button | ||
| type="button" | ||
| className={`btn btn-${type} ${className}`} | ||
| {...otherProps}> | ||
| <i className={`glyphicon glyphicon-${icon}`} /> | ||
| </button> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,32 @@ | ||
| import AddButton from "../AddButton"; | ||
| import React, { Component } from "react"; | ||
| import PropTypes from "prop-types"; | ||
|
|
||
| import { | ||
| orderProperties, | ||
| retrieveSchema, | ||
| getDefaultRegistry, | ||
| getUiOptions, | ||
| } from "../../utils"; | ||
|
|
||
| function DefaultObjectFieldTemplate(props) { | ||
| const canExpand = function canExpand() { | ||
| const { formData, schema, uiSchema } = props; | ||
| if (!schema.additionalProperties) { | ||
| return false; | ||
| } | ||
| const { expandable } = getUiOptions(uiSchema); | ||
| if (expandable !== false) { | ||
| // if ui:options.expandable was not explicitly set to false, we can add | ||
| // another property if we have not exceeded maxProperties yet | ||
| if (schema.maxProperties !== undefined) { | ||
| return Object.keys(formData).length < schema.maxProperties; | ||
| } | ||
| return true; | ||
| } | ||
| return expandable; | ||
| }; | ||
|
|
||
| const { TitleField, DescriptionField } = props; | ||
| return ( | ||
| <fieldset> | ||
|
|
@@ -27,6 +46,13 @@ function DefaultObjectFieldTemplate(props) { | |
| /> | ||
| )} | ||
| {props.properties.map(prop => prop.content)} | ||
| {canExpand() && ( | ||
| <AddButton | ||
| className="object-property-expand" | ||
| onClick={props.onAddClick(props.schema)} | ||
| disabled={props.disabled || props.readonly} | ||
| /> | ||
| )} | ||
| </fieldset> | ||
| ); | ||
| } | ||
|
|
@@ -42,6 +68,10 @@ class ObjectField extends Component { | |
| readonly: false, | ||
| }; | ||
|
|
||
| state = { | ||
| additionalProperties: {}, | ||
| }; | ||
|
|
||
| isRequired(name) { | ||
| const schema = this.props.schema; | ||
| return ( | ||
|
|
@@ -63,6 +93,62 @@ class ObjectField extends Component { | |
| }; | ||
| }; | ||
|
|
||
| getAvailableKey = (preferredKey, formData) => { | ||
| var index = 0; | ||
| var newKey = preferredKey; | ||
| while (this.props.formData.hasOwnProperty(newKey)) { | ||
| newKey = `${preferredKey}-${++index}`; | ||
| } | ||
| return newKey; | ||
| }; | ||
|
|
||
| onKeyChange = oldValue => { | ||
| return (value, errorSchema) => { | ||
| value = this.getAvailableKey(value, this.props.formData); | ||
| const newFormData = { ...this.props.formData }; | ||
| const property = newFormData[oldValue]; | ||
| delete newFormData[oldValue]; | ||
| newFormData[value] = property; | ||
| this.props.onChange( | ||
| newFormData, | ||
| errorSchema && | ||
| this.props.errorSchema && { | ||
| ...this.props.errorSchema, | ||
| [name]: errorSchema, | ||
|
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. Should
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. Haha, yes, this was copy pasta from onPropertyChange |
||
| } | ||
| ); | ||
| }; | ||
| }; | ||
|
|
||
| getDefaultValue(type) { | ||
| switch (type) { | ||
| case "string": | ||
| return "New Value"; | ||
| case "array": | ||
| return []; | ||
| case "boolean": | ||
| return false; | ||
| case "null": | ||
| return null; | ||
| case "number": | ||
| return 0; | ||
| case "object": | ||
| return {}; | ||
| default: | ||
| // We don't have a datatype for some reason (perhaps additionalProperties was true) | ||
| return "New Value"; | ||
| } | ||
| } | ||
|
|
||
| handleAddClick = schema => () => { | ||
| const type = schema.additionalProperties.type; | ||
| const newFormData = { ...this.props.formData }; | ||
| newFormData[ | ||
| this.getAvailableKey("newKey", newFormData) | ||
| ] = this.getDefaultValue(type); | ||
| this.props.onChange(newFormData); | ||
| }; | ||
|
|
||
| render() { | ||
| const { | ||
| uiSchema, | ||
|
|
@@ -120,6 +206,7 @@ class ObjectField extends Component { | |
| idSchema={idSchema[name]} | ||
| idPrefix={idPrefix} | ||
| formData={formData[name]} | ||
| onKeyChange={this.onKeyChange(name)} | ||
| onChange={this.onPropertyChange(name)} | ||
| onBlur={onBlur} | ||
| onFocus={onFocus} | ||
|
|
@@ -141,7 +228,7 @@ class ObjectField extends Component { | |
| formData, | ||
| formContext, | ||
| }; | ||
| return <Template {...templateProps} />; | ||
| return <Template {...templateProps} onAddClick={this.handleAddClick} />; | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You could refactor this as
This would save a level of indentation. But that's just a personal preference.
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 think this is a widely held preference, and I strongly agree. Some folks call this the "bouncer pattern": http://rikschennink.nl/thoughts/the-bouncer-pattern/