From 21b82ba0ab946a2676217b034b6442f56e8c959d Mon Sep 17 00:00:00 2001 From: Graham Langford <30706330+grahamlangford@users.noreply.github.com> Date: Tue, 12 Dec 2023 15:51:33 -0600 Subject: [PATCH] #7106: fixes empty label being replaced by field name (#7107) * fixes empty label being replaced by field name * fixes failing tests --- .../__snapshots__/customForm.test.tsx.snap | 12 ++------ .../ephemeralForm/EphemeralForm.test.tsx | 3 +- src/components/formBuilder/FieldTemplate.tsx | 8 +++-- .../formBuilder/preview/FormPreview.test.tsx | 29 +++++++++++++++++++ 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/bricks/renderers/__snapshots__/customForm.test.tsx.snap b/src/bricks/renderers/__snapshots__/customForm.test.tsx.snap index 30ebe8b571..c41e444c54 100644 --- a/src/bricks/renderers/__snapshots__/customForm.test.tsx.snap +++ b/src/bricks/renderers/__snapshots__/customForm.test.tsx.snap @@ -25,9 +25,7 @@ exports[`form data normalization renders normalized data 1`] = ` + /> - age - + /> - rating - + /> { properties: { foo: { type: "string", + title: "Foo", }, }, }, @@ -47,7 +48,7 @@ describe("EphemeralForm", () => { render(); await expect( - screen.findByRole("textbox", { name: "foo" }), + screen.findByRole("textbox", { name: /foo/i }), ).resolves.toBeVisible(); }); diff --git a/src/components/formBuilder/FieldTemplate.tsx b/src/components/formBuilder/FieldTemplate.tsx index 9e4563f9cd..7dc1c90810 100644 --- a/src/components/formBuilder/FieldTemplate.tsx +++ b/src/components/formBuilder/FieldTemplate.tsx @@ -31,8 +31,8 @@ const FieldTemplate = ({ rawHelp, hidden, rawDescription, - label, required, + schema: { title }, }: FieldTemplateProps) => { if (hidden) { return
{children}
; @@ -45,7 +45,11 @@ const FieldTemplate = ({ htmlFor={id} className={rawErrors.length > 0 ? "text-danger" : ""} > - {label} + {/* Cannot use the label prop as RJSF5 defaults to the name if the title is falsy + * See https://github.com/rjsf-team/react-jsonschema-form/blob/e8aa9e8f2078d86a6048ff3d018bd3030d8d2aba/packages/core/src/components/fields/SchemaField.tsx#L196 + * See https://github.com/pixiebrix/pixiebrix-extension/issues/7106 + */} + {title} {required ? "*" : null} )} diff --git a/src/components/formBuilder/preview/FormPreview.test.tsx b/src/components/formBuilder/preview/FormPreview.test.tsx index a08f6afd70..84559c913b 100644 --- a/src/components/formBuilder/preview/FormPreview.test.tsx +++ b/src/components/formBuilder/preview/FormPreview.test.tsx @@ -21,6 +21,8 @@ import testItRenders, { import { type Except } from "type-fest"; import FormPreview, { type FormPreviewProps } from "./FormPreview"; import { type Schema, type UiSchema } from "@/types/schemaTypes"; +import { render, screen } from "@testing-library/react"; +import React from "react"; describe("FormPreview", () => { const defaultProps: Except = { @@ -98,4 +100,31 @@ describe("FormPreview", () => { setActiveField: defaultProps.setActiveField, }, }); + + test("it does not render the name as the label if the title is an empty string", () => { + const schema: Schema = { + title: "Form", + type: "object", + properties: { + notes: { + type: "string", + title: "", + description: "A note", + }, + }, + }; + const uiSchema: UiSchema = {}; + + const props: FormPreviewProps = { + rjsfSchema: { schema, uiSchema }, + activeField: "notes", + setActiveField: defaultProps.setActiveField, + }; + + render(); + + expect(screen.getByRole("textbox")).toHaveAttribute("name", "root_notes"); + expect(screen.getByText("A note")).toBeInTheDocument(); + expect(screen.queryByText("notes")).not.toBeInTheDocument(); + }); });