Skip to content

Commit 0ef7dfe

Browse files
quentin-sommern1k0
authored andcommitted
Add an ui:emptyValue uiSchema option (rjsf-team#539)
The `ui:emptyValue` uiSchema directive allows having a field set to a default value when emptied.
1 parent b532e93 commit 0ef7dfe

File tree

6 files changed

+37
-3
lines changed

6 files changed

+37
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1294,10 +1294,12 @@ render((
12941294
12951295
### The case of empty strings
12961296
1297-
When a text input is empty, the field in form data is set to `undefined`. String fields that use `enum` and a `select` widget work similarly and will have an empty option at the top of the options list that when selected will result in the field being `undefined`.
1297+
When a text input is empty, the field in form data is set to `undefined`. String fields that use `enum` and a `select` widget will have an empty option at the top of the options list that when selected will result in the field being `undefined`.
12981298
12991299
One consequence of this is that if you have an empty string in your `enum` array, selecting that option in the `select` input will cause the field to be set to `undefined`, not an empty string.
13001300
1301+
If you want to have the field set to a default value when empty you can provide a `ui:emptyValue` field in the `uiSchema` object.
1302+
13011303
## Styling your forms
13021304
13031305
This library renders form fields and widgets leveraging the [Bootstrap](http://getbootstrap.com/) semantics. That means your forms will be beautiful by default if you're loading its stylesheet in your page.

playground/samples/arrays.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ module.exports = {
117117
},
118118
},
119119
uiSchema: {
120+
listOfStrings: {
121+
items: { "ui:emptyValue": "" },
122+
},
120123
multipleChoicesList: {
121124
"ui:widget": "checkboxes",
122125
},

playground/samples/simple.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = {
3131
uiSchema: {
3232
firstName: {
3333
"ui:autofocus": true,
34+
"ui:emptyValue": "",
3435
},
3536
age: {
3637
"ui:widget": "updown",

src/components/widgets/BaseInput.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function BaseInput(props) {
1616
...inputProps
1717
} = props;
1818
const _onChange = ({ target: { value } }) => {
19-
return props.onChange(value === "" ? undefined : value);
19+
return props.onChange(value === "" ? options.emptyValue : value);
2020
};
2121
return (
2222
<input

src/components/widgets/TextareaWidget.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function TextareaWidget(props) {
1414
onBlur,
1515
} = props;
1616
const _onChange = ({ target: { value } }) => {
17-
return onChange(value === "" ? undefined : value);
17+
return onChange(value === "" ? options.emptyValue : value);
1818
};
1919
return (
2020
<textarea

test/StringField_test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ describe("StringField", () => {
113113
expect(comp.state.formData).eql(undefined);
114114
});
115115

116+
it("should handle an empty string change event with custom ui:defaultValue", () => {
117+
const { comp, node } = createFormComponent({
118+
schema: { type: "string" },
119+
uiSchema: { "ui:emptyValue": "default" },
120+
formData: "x",
121+
});
122+
123+
Simulate.change(node.querySelector("input"), {
124+
target: { value: "" },
125+
});
126+
127+
expect(comp.state.formData).eql("default");
128+
});
129+
116130
it("should fill field with data", () => {
117131
const { node } = createFormComponent({
118132
schema: {
@@ -325,6 +339,20 @@ describe("StringField", () => {
325339
expect(comp.state.formData).eql(undefined);
326340
});
327341

342+
it("should handle an empty string change event with custom ui:defaultValue", () => {
343+
const { comp, node } = createFormComponent({
344+
schema: { type: "string" },
345+
uiSchema: { "ui:widget": "textarea", "ui:emptyValue": "default" },
346+
formData: "x",
347+
});
348+
349+
Simulate.change(node.querySelector("textarea"), {
350+
target: { value: "" },
351+
});
352+
353+
expect(comp.state.formData).eql("default");
354+
});
355+
328356
it("should render a textarea field with rows", () => {
329357
const { node } = createFormComponent({
330358
schema: { type: "string" },

0 commit comments

Comments
 (0)