Skip to content

Commit

Permalink
fix: schema of type string shows defaults when cleared (rjsf-team#1505)
Browse files Browse the repository at this point in the history
* fix: schema of type string shows defaults when cleared

* feat: add unit test

* Update test/StringField_test.js

Co-Authored-By: Ashwin Ramaswami <[email protected]>
  • Loading branch information
anothertempore and epicfaace committed Dec 7, 2019
1 parent 4f02482 commit e673a90
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export function getDefaultFormState(
if (isObject(formData) || Array.isArray(formData)) {
return mergeDefaultsWithFormData(defaults, formData);
}
if (formData === 0 || formData === false) {
if (formData === 0 || formData === false || formData === "") {
return formData;
}
return formData || defaults;
Expand Down
15 changes: 15 additions & 0 deletions packages/core/test/StringField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,21 @@ describe("StringField", () => {
expect(comp.state.formData).eql("default");
});

it("should handle an empty string change event with defaults set", () => {
const { comp, node } = createFormComponent({
schema: {
type: "string",
default: "a",
},
});

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

expect(comp.state.formData).eql(undefined);
});

it("should fill field with data", () => {
const { node } = createFormComponent({
schema: {
Expand Down

0 comments on commit e673a90

Please sign in to comment.