Skip to content

Commit e0ec5ea

Browse files
Fix crashing when arrayfield has a formdata set to null (rjsf-team#2154)
* Fix runtime error in ArrayField component * Update ArrayField_test.js * Update ArrayField_test.js * fixed Co-authored-by: Ashwin Ramaswami <aramaswamis@gmail.com>
1 parent 53ac2c2 commit e0ec5ea

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

packages/core/src/components/fields/ArrayField.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ class ArrayField extends Component {
224224
updatedKeyedFormData: false,
225225
};
226226
}
227-
const nextFormData = nextProps.formData;
228-
const previousKeyedFormData = prevState.keyedFormData;
227+
const nextFormData = nextProps.formData || [];
228+
const previousKeyedFormData = prevState.keyedFormData || [];
229229
const newKeyedFormData =
230230
nextFormData.length === previousKeyedFormData.length
231231
? previousKeyedFormData.map((previousKeyedFormDatum, index) => {

packages/core/test/ArrayField_test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,53 @@ describe("ArrayField", () => {
123123
});
124124
});
125125

126+
describe("Malformed nested array formData", () => {
127+
const schema = {
128+
type: "object",
129+
properties: {
130+
foo: {
131+
type: "array",
132+
items: { type: "string" },
133+
},
134+
},
135+
};
136+
137+
it("should contain no field in the list when nested array formData is explicitly null", () => {
138+
const { node } = createFormComponent({
139+
schema,
140+
formData: { foo: null },
141+
});
142+
expect(node.querySelectorAll(".field-string")).to.have.length.of(0);
143+
});
144+
});
145+
146+
describe("Nullable array formData", () => {
147+
const schema = {
148+
type: "object",
149+
properties: {
150+
foo: {
151+
type: ["array", "null"],
152+
items: { type: "string" },
153+
},
154+
},
155+
};
156+
157+
it("should contain no field in the list when nested array formData is explicitly null", () => {
158+
const { node } = createFormComponent({
159+
schema,
160+
formData: { foo: null },
161+
});
162+
expect(node.querySelectorAll(".field-string")).to.have.length.of(0);
163+
});
164+
it("should contain a field in the list when nested array formData is a single item", () => {
165+
const { node } = createFormComponent({
166+
schema,
167+
formData: { foo: ["test"] },
168+
});
169+
expect(node.querySelectorAll(".field-string")).to.have.length.of(1);
170+
});
171+
});
172+
126173
describe("List of inputs", () => {
127174
const schema = {
128175
type: "array",

0 commit comments

Comments
 (0)