diff --git a/src/components/fields/MultiSchemaField.js b/src/components/fields/MultiSchemaField.js index 325a1b9851..6ecaa82761 100644 --- a/src/components/fields/MultiSchemaField.js +++ b/src/components/fields/MultiSchemaField.js @@ -1,7 +1,7 @@ import React, { Component } from "react"; import PropTypes from "prop-types"; import * as types from "../../types"; -import { guessType } from "../../utils"; +import { getUiOptions, getWidget, guessType } from "../../utils"; import { isValid } from "../../validate"; class AnyOfField extends Component { @@ -85,8 +85,8 @@ class AnyOfField extends Component { return 0; } - onOptionChange = event => { - const selectedOption = parseInt(event.target.value, 10); + onOptionChange = option => { + const selectedOption = parseInt(option, 10); const { formData, onChange, options } = this.props; const newOption = options[selectedOption]; @@ -119,7 +119,7 @@ class AnyOfField extends Component { } this.setState({ - selectedOption: parseInt(event.target.value, 10), + selectedOption: parseInt(option, 10), }); }; @@ -141,7 +141,10 @@ class AnyOfField extends Component { } = this.props; const _SchemaField = registry.fields.SchemaField; + const { widgets } = registry; const { selectedOption } = this.state; + const { widget = "select", ...uiOptions } = getUiOptions(uiSchema); + const Widget = getWidget({ type: "number" }, widget, widgets); const option = options[selectedOption] || null; let optionSchema; @@ -154,22 +157,24 @@ class AnyOfField extends Component { : Object.assign({}, option, { type: baseType }); } + const enumOptions = options.map((option, index) => ({ + label: option.title || `Option ${index + 1}`, + value: index, + })); + return (
- + options={{ enumOptions }} + {...uiOptions} + />
{option !== null && ( diff --git a/test/anyOf_test.js b/test/anyOf_test.js index a2bd14b200..9f7cbf2b06 100644 --- a/test/anyOf_test.js +++ b/test/anyOf_test.js @@ -54,6 +54,36 @@ describe("anyOf", () => { expect(node.querySelectorAll("select")).to.have.length.of(1); }); + it("should render a custom widget", () => { + const schema = { + type: "object", + anyOf: [ + { + properties: { + foo: { type: "string" }, + }, + }, + { + properties: { + bar: { type: "string" }, + }, + }, + ], + }; + const widgets = { + SelectWidget: () => { + return
Custom Widget
; + }, + }; + + const { node } = createFormComponent({ + schema, + widgets, + }); + + expect(node.querySelector("#CustomSelect")).to.exist; + }); + it("should change the rendered form when the select value is changed", () => { const schema = { type: "object", diff --git a/test/oneOf_test.js b/test/oneOf_test.js index a7589dae68..98ba91ed6d 100644 --- a/test/oneOf_test.js +++ b/test/oneOf_test.js @@ -54,6 +54,36 @@ describe("oneOf", () => { expect(node.querySelectorAll("select")).to.have.length.of(1); }); + it("should render a custom widget", () => { + const schema = { + type: "object", + oneOf: [ + { + properties: { + foo: { type: "string" }, + }, + }, + { + properties: { + bar: { type: "string" }, + }, + }, + ], + }; + const widgets = { + SelectWidget: () => { + return
Custom Widget
; + }, + }; + + const { node } = createFormComponent({ + schema, + widgets, + }); + + expect(node.querySelector("#CustomSelect")).to.exist; + }); + it("should change the rendered form when the select value is changed", () => { const schema = { type: "object",