Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1294,10 +1294,12 @@ render((

### The case of empty strings

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`.
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`.

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.

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.

## Styling your forms

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.
Expand Down
3 changes: 3 additions & 0 deletions playground/samples/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ module.exports = {
},
},
uiSchema: {
listOfStrings: {
items: { "ui:emptyValue": "" },
},
multipleChoicesList: {
"ui:widget": "checkboxes",
},
Expand Down
1 change: 1 addition & 0 deletions playground/samples/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = {
uiSchema: {
firstName: {
"ui:autofocus": true,
"ui:emptyValue": "",
},
age: {
"ui:widget": "updown",
Expand Down
2 changes: 1 addition & 1 deletion src/components/widgets/BaseInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function BaseInput(props) {
...inputProps
} = props;
const _onChange = ({ target: { value } }) => {
return props.onChange(value === "" ? undefined : value);
return props.onChange(value === "" ? options.emptyValue : value);
};
return (
<input
Expand Down
2 changes: 1 addition & 1 deletion src/components/widgets/TextareaWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function TextareaWidget(props) {
onBlur,
} = props;
const _onChange = ({ target: { value } }) => {
return onChange(value === "" ? undefined : value);
return onChange(value === "" ? options.emptyValue : value);
};
return (
<textarea
Expand Down
28 changes: 28 additions & 0 deletions test/StringField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ describe("StringField", () => {
expect(comp.state.formData).eql(undefined);
});

it("should handle an empty string change event with custom ui:defaultValue", () => {
const { comp, node } = createFormComponent({
schema: { type: "string" },
uiSchema: { "ui:emptyValue": "default" },
formData: "x",
});

Simulate.change(node.querySelector("input"), {
target: { value: "" },
});

expect(comp.state.formData).eql("default");
});

it("should fill field with data", () => {
const { node } = createFormComponent({
schema: {
Expand Down Expand Up @@ -325,6 +339,20 @@ describe("StringField", () => {
expect(comp.state.formData).eql(undefined);
});

it("should handle an empty string change event with custom ui:defaultValue", () => {
const { comp, node } = createFormComponent({
schema: { type: "string" },
uiSchema: { "ui:widget": "textarea", "ui:emptyValue": "default" },
formData: "x",
});

Simulate.change(node.querySelector("textarea"), {
target: { value: "" },
});

expect(comp.state.formData).eql("default");
});

it("should render a textarea field with rows", () => {
const { node } = createFormComponent({
schema: { type: "string" },
Expand Down