From f8c6850a3680eeba16bd19fe3474187992f8d483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Perrin?= Date: Fri, 6 Aug 2021 06:33:45 +0900 Subject: [PATCH] Take into account implicitly defined types when rendering labels for fields (#2502) `getSchemaType` is clever enough to figure out that a schema with `properties` or `additionalProperties` has `type` "object", even if the type is not explicitly defined, but `getDisplayLabel` does not have the same logic, which results in displaying the label twice in cases where the schema was guessed to be object. Using `getSchemaType` as well in `getDisplayLabel` bring consistent behavior and the label is rendered only once in that case. Fixes #2501 --- packages/core/src/utils.js | 8 +++++--- packages/core/test/SchemaField_test.js | 11 +++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/core/src/utils.js b/packages/core/src/utils.js index 83c20cdec8..d759ea9b70 100644 --- a/packages/core/src/utils.js +++ b/packages/core/src/utils.js @@ -389,15 +389,17 @@ export function getUiOptions(uiSchema) { export function getDisplayLabel(schema, uiSchema, rootSchema) { const uiOptions = getUiOptions(uiSchema); let { label: displayLabel = true } = uiOptions; - if (schema.type === "array") { + const schemaType = getSchemaType(schema); + + if (schemaType === "array") { displayLabel = isMultiSelect(schema, rootSchema) || isFilesArray(schema, uiSchema, rootSchema); } - if (schema.type === "object") { + if (schemaType === "object") { displayLabel = false; } - if (schema.type === "boolean" && !uiSchema["ui:widget"]) { + if (schemaType === "boolean" && !uiSchema["ui:widget"]) { displayLabel = false; } if (uiSchema["ui:field"]) { diff --git a/packages/core/test/SchemaField_test.js b/packages/core/test/SchemaField_test.js index 83eed62ef8..406dccda88 100644 --- a/packages/core/test/SchemaField_test.js +++ b/packages/core/test/SchemaField_test.js @@ -279,6 +279,17 @@ describe("SchemaField", () => { const { node } = createFormComponent({ schema, uiSchema }); expect(node.querySelectorAll("label")).to.have.length.of(0); }); + + it("should render label even when type object is missing", () => { + const schema = { + title: "test", + properties: { + foo: { type: "string" }, + }, + }; + const { node } = createFormComponent({ schema }); + expect(node.querySelectorAll("label")).to.have.length.of(1); + }); }); describe("description support", () => {