From 2055dba3dd14bd3a58b9e84891f0885e6c0cab91 Mon Sep 17 00:00:00 2001 From: Alasdair McLeay Date: Tue, 15 Aug 2017 18:45:08 +0100 Subject: [PATCH 1/3] Failing test for enum without type (#66 #611) --- test/StringField_test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/StringField_test.js b/test/StringField_test.js index 1446f89e8c..ea72a5b863 100644 --- a/test/StringField_test.js +++ b/test/StringField_test.js @@ -190,6 +190,16 @@ describe("StringField", () => { expect(node.querySelectorAll(".field select")).to.have.length.of(1); }); + it("should render a string field for an enum without a type", () => { + const { node } = createFormComponent({ + schema: { + enum: ["foo", "bar"], + }, + }); + + expect(node.querySelectorAll(".field select")).to.have.length.of(1); + }); + it("should render a string field with a label", () => { const { node } = createFormComponent({ schema: { From 5f77cb7cf4a79ce0c17d2ecc6aba0dd435f950c2 Mon Sep 17 00:00:00 2001 From: Alasdair McLeay Date: Tue, 15 Aug 2017 18:45:35 +0100 Subject: [PATCH 2/3] Proposed fix for enum without type (#66 #611) --- src/components/fields/SchemaField.js | 4 ++++ src/utils.js | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/components/fields/SchemaField.js b/src/components/fields/SchemaField.js index 1b20d54ffc..fa2f0aeab6 100644 --- a/src/components/fields/SchemaField.js +++ b/src/components/fields/SchemaField.js @@ -29,6 +29,10 @@ function getFieldComponent(schema, uiSchema, fields) { if (typeof field === "string" && field in fields) { return fields[field]; } + + if (!schema.type && schema.enum) { + return fields[COMPONENT_TYPES["string"]]; + } const componentName = COMPONENT_TYPES[schema.type]; return componentName in fields ? fields[componentName] : UnsupportedField; } diff --git a/src/utils.js b/src/utils.js index 0c24acf71e..ae860c39f8 100644 --- a/src/utils.js +++ b/src/utils.js @@ -62,7 +62,10 @@ export function getDefaultRegistry() { } export function getWidget(schema, widget, registeredWidgets = {}) { - const { type } = schema; + let { type } = schema; + if (!type && schema.enum) { + type = "string"; + } function mergeOptions(Widget) { // cache return value as property of widget for proper react reconciliation From 6577128b0c638f5899d0bc840df9e43fe07e2196 Mon Sep 17 00:00:00 2001 From: Alasdair McLeay Date: Wed, 10 Jan 2018 16:59:42 +0000 Subject: [PATCH 3/3] PR feedback - getSchemaType function https://github.com/mozilla-services/react-jsonschema-form/pull/668/files#r133395455 --- src/components/fields/SchemaField.js | 24 +++++------------------- src/utils.js | 12 +++++++++--- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/src/components/fields/SchemaField.js b/src/components/fields/SchemaField.js index fa2f0aeab6..c0c1993fef 100644 --- a/src/components/fields/SchemaField.js +++ b/src/components/fields/SchemaField.js @@ -8,6 +8,7 @@ import { getUiOptions, isFilesArray, deepEquals, + getSchemaType, } from "../../utils"; import UnsupportedField from "./UnsupportedField"; @@ -30,10 +31,7 @@ function getFieldComponent(schema, uiSchema, fields) { return fields[field]; } - if (!schema.type && schema.enum) { - return fields[COMPONENT_TYPES["string"]]; - } - const componentName = COMPONENT_TYPES[schema.type]; + const componentName = COMPONENT_TYPES[getSchemaType(schema)]; return componentName in fields ? fields[componentName] : UnsupportedField; } @@ -57,17 +55,9 @@ function Help(props) { return
; } if (typeof help === "string") { - return ( -

- {help} -

- ); + return

{help}

; } - return ( -
- {help} -
- ); + return
{help}
; } function ErrorList(props) { @@ -254,11 +244,7 @@ function SchemaFieldRender(props) { uiSchema, }; - return ( - - {field} - - ); + return {field}; } class SchemaField extends React.Component { diff --git a/src/utils.js b/src/utils.js index ae860c39f8..84f28413b8 100644 --- a/src/utils.js +++ b/src/utils.js @@ -61,19 +61,25 @@ export function getDefaultRegistry() { }; } -export function getWidget(schema, widget, registeredWidgets = {}) { +export function getSchemaType(schema) { let { type } = schema; if (!type && schema.enum) { type = "string"; } + return type; +} + +export function getWidget(schema, widget, registeredWidgets = {}) { + const type = getSchemaType(schema); function mergeOptions(Widget) { // cache return value as property of widget for proper react reconciliation if (!Widget.MergedWidget) { const defaultOptions = (Widget.defaultProps && Widget.defaultProps.options) || {}; - Widget.MergedWidget = ({ options = {}, ...props }) => - ; + Widget.MergedWidget = ({ options = {}, ...props }) => ( + + ); } return Widget.MergedWidget; }