Skip to content

Commit 07f8f00

Browse files
author
Rani
committed
fix(@rjsf/core): use name for additional properties
1 parent f9e6579 commit 07f8f00

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

packages/core/src/components/fields/SchemaField.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
RJSFSchemaDefinition,
1414
UIOptionsType,
1515
ID_KEY,
16+
ADDITIONAL_PROPERTY_FLAG,
1617
} from "@rjsf/utils";
1718
import isObject from "lodash/isObject";
1819
import omit from "lodash/omit";
@@ -236,10 +237,14 @@ function SchemaFieldRender<T, F>(props: FieldProps<T, F>) {
236237

237238
// If this schema has a title defined, but the user has set a new key/label, retain their input.
238239
let label;
240+
console.log("hello");
241+
console.warn({ schema, name, wasPropertyKeyModified });
239242
if (wasPropertyKeyModified) {
240243
label = name;
241244
} else {
242-
label = uiOptions.title || props.schema.title || schema.title || name;
245+
label = schema.hasOwnProperty(ADDITIONAL_PROPERTY_FLAG)
246+
? name
247+
: uiOptions.title || props.schema.title || schema.title || name;
243248
}
244249

245250
const description =
@@ -305,6 +310,7 @@ function SchemaFieldRender<T, F>(props: FieldProps<T, F>) {
305310
return (
306311
<FieldTemplate {...fieldProps}>
307312
<>
313+
<div>hello</div>
308314
{field}
309315
{/*
310316
If the schema `anyOf` or 'oneOf' can be rendered as a select control, don't

packages/core/test/ObjectField_test.js

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ describe("ObjectField", () => {
446446
expect(node.querySelectorAll(".field-string")).to.have.length.of(1);
447447
});
448448

449-
it("should apply uiSchema to additionalProperties", () => {
449+
it("uiSchema title should not affect additionalProperties", () => {
450450
const { node } = createFormComponent({
451451
schema,
452452
uiSchema: {
@@ -459,8 +459,56 @@ describe("ObjectField", () => {
459459
},
460460
});
461461
const labels = node.querySelectorAll("label.control-label");
462-
expect(labels[0].textContent).eql("CustomName Key");
463-
expect(labels[1].textContent).eql("CustomName");
462+
expect(labels[0].textContent).eql("property1 Key");
463+
expect(labels[1].textContent).eql("property1");
464+
});
465+
466+
it("uiSchema title should update additionalProperties object title", () => {
467+
const objectSchema = {
468+
type: "object",
469+
properties: {
470+
main: {
471+
type: "object",
472+
properties: {},
473+
additionalProperties: {
474+
type: "object",
475+
title: "propTitle",
476+
properties: {
477+
firstName: {
478+
type: "string",
479+
title: "First name",
480+
},
481+
},
482+
},
483+
},
484+
},
485+
};
486+
487+
const { node } = createFormComponent({
488+
schema: objectSchema,
489+
uiSchema: {
490+
main: {
491+
additionalProperties: {
492+
"ui:title": "CustomName",
493+
},
494+
},
495+
},
496+
formData: {
497+
main: {
498+
property1: {
499+
firstName: "hello",
500+
},
501+
},
502+
},
503+
});
504+
const labels = [...node.querySelectorAll("label.control-label")].map(
505+
(n) => n.textContent
506+
);
507+
expect(labels).to.include("property1 Key");
508+
const objectTitle = node.querySelector(
509+
".form-additional > fieldset > legend"
510+
);
511+
expect(objectTitle.textContent).eql("CustomName");
464512
});
465513

466514
it("should not throw validation errors if additionalProperties is undefined", () => {

0 commit comments

Comments
 (0)