Skip to content

Commit d6fa4f4

Browse files
authored
Fix duplicate descriptions. (rjsf-team#265)
1 parent 700f223 commit d6fa4f4

File tree

6 files changed

+26
-8
lines changed

6 files changed

+26
-8
lines changed

playground/samples/simple.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = {
22
schema: {
33
title: "A registration form",
4+
description: "A simple form example.",
45
type: "object",
56
required: ["firstName", "lastName"],
67
properties: {

src/components/fields/DescriptionField.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import React, {PropTypes} from "react";
22

33
function DescriptionField(props) {
44
const {id, description} = props;
5-
return <div id={id} className="field-description">{description}</div>;
5+
if (typeof description === "string") {
6+
return <p id={id} className="field-description">{description}</p>;
7+
} else {
8+
return <div id={id} className="field-description">{description}</div>;
9+
}
610
}
711

812
if (process.env.NODE_ENV !== "production") {

src/components/fields/SchemaField.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ function Wrapper({
9797
return (
9898
<div className={classList}>
9999
{displayLabel && label ? getLabel(label, required, id) : null}
100-
{description ? <DescriptionField id={`${id}__description`} description={description} /> : null}
100+
{displayLabel && description ?
101+
<DescriptionField id={`${id}__description`} description={description} /> : null}
101102
{children}
102103
{isError ? <ErrorList errors={errors} /> : <div/>}
103104
{renderHelp(help)}

test/ArrayField_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe("ArrayField", () => {
4444
it("should render a description", () => {
4545
const {node} = createFormComponent({schema});
4646

47-
const description = node.querySelector("fieldset > div.field-description");
47+
const description = node.querySelector("fieldset > .field-description");
4848

4949
expect(description.textContent).eql("my description");
5050
expect(description.id).eql("root__description");

test/DescriptionField_test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,24 @@ describe("DescriptionField", () => {
2626
}
2727
}
2828

29-
it("should return a div", () => {
29+
it("should return a div for a custom component", () => {
3030
const props = {
31-
description: "Field description",
31+
description: <em>description</em>,
3232
};
3333
const {node} = createComponent(DescriptionFieldWrapper, props);
3434

3535
expect(node.tagName).to.equal("DIV");
3636
});
3737

38+
it("should return a p for a description text", () => {
39+
const props = {
40+
description: "description",
41+
};
42+
const {node} = createComponent(DescriptionFieldWrapper, props);
43+
44+
expect(node.tagName).to.equal("P");
45+
});
46+
3847
it("should have the expected id", () => {
3948
const props = {
4049
description: "Field description",

test/SchemaField_test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ describe("SchemaField", () => {
149149

150150
it("should render description if available from the schema", () => {
151151
const {node} = createFormComponent({schema});
152-
expect(node.querySelectorAll("div#root_foo__description"))
152+
153+
expect(node.querySelectorAll("#root_foo__description"))
153154
.to.have.length.of(1);
154155
});
155156

@@ -169,14 +170,16 @@ describe("SchemaField", () => {
169170
}
170171
};
171172
const {node} = createFormComponent({schema: schemaWithReference});
172-
const matches = node.querySelectorAll("div#root_foo__description");
173+
174+
const matches = node.querySelectorAll("#root_foo__description");
173175
expect(matches).to.have.length.of(1);
174176
expect(matches[0].textContent).to.equal("A Foo field");
175177
});
176178

177179
it("should not render description if not available from schema", () => {
178180
const {node} = createFormComponent({schema});
179-
expect(node.querySelectorAll("div#root_bar__description"))
181+
182+
expect(node.querySelectorAll("#root_bar__description"))
180183
.to.have.length.of(0);
181184
});
182185
});

0 commit comments

Comments
 (0)