Skip to content

Commit be21b8e

Browse files
stepekn1k0
authored andcommitted
Added possibility to change title and description by ui:schema. (rjsf-team#587)
1 parent 32cca38 commit be21b8e

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ A [live playground](https://mozilla-services.github.io/react-jsonschema-form/) i
4545
- [Autogenerated widget ids](#autogenerated-widget-ids)
4646
- [Form action buttons](#form-action-buttons)
4747
- [Help texts](#help-texts)
48+
- [Title texts](#title-texts)
49+
- [Description texts](#description-texts)
4850
- [Auto focus](#auto-focus)
4951
- [Textarea rows option](#textarea-rows-option)
5052
- [Placeholders](#placeholders)
@@ -640,6 +642,30 @@ const uiSchema = {
640642

641643
Help texts work for any kind of field at any level, and will always be rendered immediately below the field component widget(s), but after contextualized errors, if any.
642644

645+
### Title texts
646+
647+
Sometimes it's convenient to change title a field; this is the purpose of the `ui:title` uiSchema directive:
648+
649+
```js
650+
const schema = {type: "string"};
651+
const uiSchema = {
652+
"ui:widget": "password",
653+
"ui:title": "Your password"
654+
};
655+
```
656+
657+
### Description texts
658+
659+
Sometimes it's convenient to change description a field; this is the purpose of the `ui:description` uiSchema directive:
660+
661+
```js
662+
const schema = {type: "string"};
663+
const uiSchema = {
664+
"ui:widget": "password",
665+
"ui:description": "The best password"
666+
};
667+
```
668+
643669
### Auto focus
644670

645671
If you want to focus on a text input or textarea input/on a widget automatically, just set `ui:autofocus` uiSchema directive to `true`.

playground/samples/simple.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ module.exports = {
4040
},
4141
age: {
4242
"ui:widget": "updown",
43+
"ui:title": "Age of person",
44+
"ui:description": "(earthian year)",
4345
},
4446
bio: {
4547
"ui:widget": "textarea",

src/components/fields/SchemaField.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,12 @@ function SchemaFieldRender(props) {
191191

192192
const { type } = schema;
193193
const id = idSchema.$id;
194-
const label = props.schema.title || schema.title || name;
195-
const description = props.schema.description || schema.description;
194+
const label =
195+
uiSchema["ui:title"] || props.schema.title || schema.title || name;
196+
const description =
197+
uiSchema["ui:description"] ||
198+
props.schema.description ||
199+
schema.description;
196200
const errors = __errors;
197201
const help = uiSchema["ui:help"];
198202
const hidden = uiSchema["ui:widget"] === "hidden";

test/uiSchema_test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,28 @@ describe("uiSchema", () => {
355355
});
356356
});
357357

358+
describe("ui:title", () => {
359+
it("should render the provided title text", () => {
360+
const schema = { type: "string" };
361+
const uiSchema = { "ui:title": "plop" };
362+
363+
const { node } = createFormComponent({ schema, uiSchema });
364+
365+
expect(node.querySelector("label.control-label").textContent).eql("plop");
366+
});
367+
});
368+
369+
describe("ui:description", () => {
370+
it("should render the provided description text", () => {
371+
const schema = { type: "string" };
372+
const uiSchema = { "ui:description": "plop" };
373+
374+
const { node } = createFormComponent({ schema, uiSchema });
375+
376+
expect(node.querySelector("p.field-description").textContent).eql("plop");
377+
});
378+
});
379+
358380
it("should accept a react element as help", () => {
359381
const schema = { type: "string" };
360382
const uiSchema = { "ui:help": <b>plop</b> };

0 commit comments

Comments
 (0)