Skip to content

Add null checks for ArrayField (New PR for #1553) #1992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
161 changes: 92 additions & 69 deletions packages/core/src/components/fields/ArrayField.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ function generateKeyedFormData(formData) {
}

function keyedToPlainFormData(keyedFormData) {
return keyedFormData.map(keyedItem => keyedItem.item);
return keyedFormData == null
? keyedFormData
: keyedFormData.map(keyedItem => keyedItem.item);
}

class ArrayField extends Component {
Expand Down Expand Up @@ -227,7 +229,9 @@ class ArrayField extends Component {
const nextFormData = nextProps.formData;
const previousKeyedFormData = prevState.keyedFormData;
const newKeyedFormData =
nextFormData.length === previousKeyedFormData.length
nextFormData == null || previousKeyedFormData == null
? nextFormData
: nextFormData.length === previousKeyedFormData.length
? previousKeyedFormData.map((previousKeyedFormDatum, index) => {
return {
key: previousKeyedFormDatum.key,
Expand Down Expand Up @@ -290,7 +294,10 @@ class ArrayField extends Component {
key: generateRowId(),
item: this._getNewFormDataRow(),
};
const newKeyedFormData = [...this.state.keyedFormData, newKeyedFormDataRow];
const newKeyedFormData = [
...(this.state.keyedFormData || []),
newKeyedFormDataRow,
];
this.setState(
{
keyedFormData: newKeyedFormData,
Expand All @@ -310,7 +317,7 @@ class ArrayField extends Component {
key: generateRowId(),
item: this._getNewFormDataRow(),
};
let newKeyedFormData = [...this.state.keyedFormData];
let newKeyedFormData = [...(this.state.keyedFormData || [])];
newKeyedFormData.splice(index, 0, newKeyedFormDataRow);

this.setState(
Expand Down Expand Up @@ -344,7 +351,10 @@ class ArrayField extends Component {
}
}
}
const newKeyedFormData = keyedFormData.filter((_, i) => i !== index);
const newKeyedFormData =
keyedFormData == null
? keyedFormData
: keyedFormData.filter((_, i) => i !== index);
this.setState(
{
keyedFormData: newKeyedFormData,
Expand Down Expand Up @@ -378,6 +388,9 @@ class ArrayField extends Component {
}

const { keyedFormData } = this.state;
if (keyedFormData == null) {
return;
}
function reOrderArray() {
// Copy item
let _newKeyedFormData = keyedFormData.slice();
Expand Down Expand Up @@ -478,33 +491,38 @@ class ArrayField extends Component {
const formData = keyedToPlainFormData(this.state.keyedFormData);
const arrayProps = {
canAdd: this.canAddItem(formData),
items: this.state.keyedFormData.map((keyedItem, index) => {
const { key, item } = keyedItem;
const itemSchema = retrieveSchema(schema.items, rootSchema, item);
const itemErrorSchema = errorSchema ? errorSchema[index] : undefined;
const itemIdPrefix = idSchema.$id + "_" + index;
const itemIdSchema = toIdSchema(
itemSchema,
itemIdPrefix,
rootSchema,
item,
idPrefix
);
return this.renderArrayFieldItem({
key,
index,
canMoveUp: index > 0,
canMoveDown: index < formData.length - 1,
itemSchema: itemSchema,
itemIdSchema,
itemErrorSchema,
itemData: item,
itemUiSchema: uiSchema.items,
autofocus: autofocus && index === 0,
onBlur,
onFocus,
});
}),
items:
this.state.keyedFormData == null
? this.state.keyedFormData
: this.state.keyedFormData.map((keyedItem, index) => {
const { key, item } = keyedItem;
const itemSchema = retrieveSchema(schema.items, rootSchema, item);
const itemErrorSchema = errorSchema
? errorSchema[index]
: undefined;
const itemIdPrefix = idSchema.$id + "_" + index;
const itemIdSchema = toIdSchema(
itemSchema,
itemIdPrefix,
rootSchema,
item,
idPrefix
);
return this.renderArrayFieldItem({
key,
index,
canMoveUp: index > 0,
canMoveDown: index < formData.length - 1,
itemSchema: itemSchema,
itemIdSchema,
itemErrorSchema,
itemData: item,
itemUiSchema: uiSchema.items,
autofocus: autofocus && index === 0,
onBlur,
onFocus,
});
}),
className: `field field-array field-array-of-${itemsSchema.type}`,
DescriptionField,
disabled,
Expand Down Expand Up @@ -660,43 +678,48 @@ class ArrayField extends Component {
disabled,
idSchema,
formData,
items: this.state.keyedFormData.map((keyedItem, index) => {
const { key, item } = keyedItem;
const additional = index >= itemSchemas.length;
const itemSchema = additional
? retrieveSchema(schema.additionalItems, rootSchema, item)
: itemSchemas[index];
const itemIdPrefix = idSchema.$id + "_" + index;
const itemIdSchema = toIdSchema(
itemSchema,
itemIdPrefix,
rootSchema,
item,
idPrefix
);
const itemUiSchema = additional
? uiSchema.additionalItems || {}
: Array.isArray(uiSchema.items)
? uiSchema.items[index]
: uiSchema.items || {};
const itemErrorSchema = errorSchema ? errorSchema[index] : undefined;

return this.renderArrayFieldItem({
key,
index,
canRemove: additional,
canMoveUp: index >= itemSchemas.length + 1,
canMoveDown: additional && index < items.length - 1,
itemSchema,
itemData: item,
itemUiSchema,
itemIdSchema,
itemErrorSchema,
autofocus: autofocus && index === 0,
onBlur,
onFocus,
});
}),
items:
this.state.keyedFormData == null
? this.state.keyedFormData
: this.state.keyedFormData.map((keyedItem, index) => {
const { key, item } = keyedItem;
const additional = index >= itemSchemas.length;
const itemSchema = additional
? retrieveSchema(schema.additionalItems, rootSchema, item)
: itemSchemas[index];
const itemIdPrefix = idSchema.$id + "_" + index;
const itemIdSchema = toIdSchema(
itemSchema,
itemIdPrefix,
rootSchema,
item,
idPrefix
);
const itemUiSchema = additional
? uiSchema.additionalItems || {}
: Array.isArray(uiSchema.items)
? uiSchema.items[index]
: uiSchema.items || {};
const itemErrorSchema = errorSchema
? errorSchema[index]
: undefined;

return this.renderArrayFieldItem({
key,
index,
canRemove: additional,
canMoveUp: index >= itemSchemas.length + 1,
canMoveDown: additional && index < items.length - 1,
itemSchema,
itemData: item,
itemUiSchema,
itemIdSchema,
itemErrorSchema,
autofocus: autofocus && index === 0,
onBlur,
onFocus,
});
}),
onAddClick: this.onAddClick,
readonly,
required,
Expand Down
20 changes: 20 additions & 0 deletions packages/material-ui/test/Array.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,24 @@ describe("array fields", () => {
.toJSON();
expect(tree).toMatchSnapshot();
});
test("null array", () => {
const schema: JSONSchema7 = {
"type": "object",
"properties": {
"array": {
"type": "array",
"items": {
"type": "string"
}
}
}
};
const formData = {
"array": null
};
const tree = renderer
.create(<Form schema={schema} formData={formData} />)
.toJSON();
expect(tree).toMatchSnapshot();
});
});