Skip to content

Commit

Permalink
Take into account implicitly defined types when rendering labels for …
Browse files Browse the repository at this point in the history
…fields (rjsf-team#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 rjsf-team#2501
  • Loading branch information
perrinjerome authored Aug 5, 2021
1 parent 3eae287 commit f8c6850
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/core/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]) {
Expand Down
11 changes: 11 additions & 0 deletions packages/core/test/SchemaField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit f8c6850

Please sign in to comment.