-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add preliminary support for the anyOf keyword #1118
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
296d876
Add preliminary support for the anyOf keyword
LucianBuzzo 888b25f
Refactor unnecessary .map
LucianBuzzo 0b9057f
Change selected option based on form data
LucianBuzzo 776226f
Update README
LucianBuzzo 97e7317
Update README FAQ
LucianBuzzo abcef8c
Share prop types between fields
LucianBuzzo 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,37 @@ | ||
| module.exports = { | ||
| schema: { | ||
| type: "object", | ||
| properties: { | ||
| age: { | ||
| type: "integer", | ||
| title: "Age", | ||
| }, | ||
| }, | ||
| anyOf: [ | ||
| { | ||
| title: "First method of identification", | ||
| properties: { | ||
| firstName: { | ||
| type: "string", | ||
| title: "First name", | ||
| default: "Chuck", | ||
| }, | ||
| lastName: { | ||
| type: "string", | ||
| title: "Last name", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| title: "Second method of identification", | ||
| properties: { | ||
| idCode: { | ||
| type: "string", | ||
| title: "ID code", | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| formData: {}, | ||
| }; |
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,163 @@ | ||
| import React, { Component } from "react"; | ||
| import PropTypes from "prop-types"; | ||
| import * as types from "../../types"; | ||
| import { guessType } from "../../utils"; | ||
| import { isValid } from "../../validate"; | ||
|
|
||
| class AnyOfField extends Component { | ||
| constructor(props) { | ||
| super(props); | ||
|
|
||
| const { formData, schema } = this.props; | ||
|
|
||
| this.state = { | ||
| selectedOption: this.getMatchingOption(formData, schema.anyOf), | ||
| }; | ||
| } | ||
|
|
||
| componentWillReceiveProps(nextProps) { | ||
| const matchingOption = this.getMatchingOption( | ||
| nextProps.formData, | ||
| nextProps.schema.anyOf | ||
| ); | ||
|
|
||
| if (matchingOption === this.state.selectedOption) { | ||
| return; | ||
| } | ||
|
|
||
| this.setState({ selectedOption: matchingOption }); | ||
| } | ||
|
|
||
| getMatchingOption(formData, options) { | ||
| for (let i = 0; i < options.length; i++) { | ||
| if (isValid(options[i], formData)) { | ||
| return i; | ||
| } | ||
| } | ||
|
|
||
| // If the form data matches none of the options, use the first option | ||
| return 0; | ||
| } | ||
|
|
||
| onOptionChange = event => { | ||
| const selectedOption = parseInt(event.target.value, 10); | ||
| const { formData, onChange, schema } = this.props; | ||
| const options = schema.anyOf; | ||
|
|
||
| if (guessType(formData) === "object") { | ||
| const newFormData = Object.assign({}, formData); | ||
|
|
||
| const optionsToDiscard = options.slice(); | ||
| optionsToDiscard.splice(selectedOption, 1); | ||
|
|
||
| // Discard any data added using other options | ||
| for (const option of optionsToDiscard) { | ||
| if (option.properties) { | ||
| for (const key in option.properties) { | ||
| if (newFormData.hasOwnProperty(key)) { | ||
| delete newFormData[key]; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| onChange(newFormData); | ||
| } else { | ||
| onChange(undefined); | ||
| } | ||
|
|
||
| this.setState({ | ||
| selectedOption: parseInt(event.target.value, 10), | ||
| }); | ||
| }; | ||
|
|
||
| render() { | ||
| const { | ||
| disabled, | ||
| errorSchema, | ||
| formData, | ||
| idPrefix, | ||
| idSchema, | ||
| onBlur, | ||
| onChange, | ||
| onFocus, | ||
| schema, | ||
| registry, | ||
| safeRenderCompletion, | ||
| uiSchema, | ||
| } = this.props; | ||
|
|
||
| const _SchemaField = registry.fields.SchemaField; | ||
| const { selectedOption } = this.state; | ||
|
|
||
| const baseType = schema.type; | ||
| const options = schema.anyOf || []; | ||
| const option = options[selectedOption] || null; | ||
| let optionSchema; | ||
|
|
||
| if (option) { | ||
| // If the subschema doesn't declare a type, infer the type from the | ||
| // parent schema | ||
| optionSchema = option.type | ||
| ? option | ||
| : Object.assign({}, option, { type: baseType }); | ||
| } | ||
|
|
||
| return ( | ||
| <div className="panel panel-default panel-body"> | ||
| <div className="form-group"> | ||
| <select | ||
| className="form-control" | ||
| onChange={this.onOptionChange} | ||
| value={selectedOption} | ||
| id={`${idSchema.$id}_anyof_select`}> | ||
| {options.map((option, index) => { | ||
| return ( | ||
| <option key={index} value={index}> | ||
| {option.title || `Option ${index + 1}`} | ||
| </option> | ||
| ); | ||
| })} | ||
| </select> | ||
| </div> | ||
|
|
||
| {option !== null && ( | ||
| <_SchemaField | ||
| schema={optionSchema} | ||
| uiSchema={uiSchema} | ||
| errorSchema={errorSchema} | ||
| idSchema={idSchema} | ||
| idPrefix={idPrefix} | ||
| formData={formData} | ||
| onChange={onChange} | ||
| onBlur={onBlur} | ||
| onFocus={onFocus} | ||
| registry={registry} | ||
| safeRenderCompletion={safeRenderCompletion} | ||
| disabled={disabled} | ||
| /> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| AnyOfField.defaultProps = { | ||
| disabled: false, | ||
| errorSchema: {}, | ||
| idSchema: {}, | ||
| uiSchema: {}, | ||
| }; | ||
|
|
||
| if (process.env.NODE_ENV !== "production") { | ||
| AnyOfField.propTypes = { | ||
| schema: PropTypes.object.isRequired, | ||
| uiSchema: PropTypes.object, | ||
| idSchema: PropTypes.object, | ||
| formData: PropTypes.any, | ||
| errorSchema: PropTypes.object, | ||
| registry: types.registry.isRequired, | ||
| }; | ||
| } | ||
|
|
||
| export default AnyOfField; |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.