Skip to content
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

Live omit fixes #1418

Closed
wants to merge 13 commits into from
26 changes: 18 additions & 8 deletions src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
deepEquals,
toPathSchema,
isObject,
getLastPart,
} from "../utils";
import validateFormData, { toErrorList } from "../validate";

Expand Down Expand Up @@ -154,11 +155,7 @@ export default class Form extends Component {
if (!paths.length) {
getAllPaths(_obj[key], acc, [key]);
} else {
let newPaths = [];
paths.forEach(path => {
newPaths.push(path);
});
newPaths = newPaths.map(path => `${path}.${key}`);
let newPaths = paths.map(path => `${path}.${key}`);
getAllPaths(_obj[key], acc, newPaths);
}
} else if (key === "$name") {
Expand All @@ -167,6 +164,16 @@ export default class Form extends Component {
if (typeof formValue !== "object") {
acc.push(path);
}
if (
isObject(formValue) &&
Object.keys(formValue).length === 0 &&
Array.isArray(getLastPart(formData, path))
) {
// If formValue is {} and it is stored above, then user it.
// Example: path = "participants.0", formData = {participants: [{}]}, formValue = {}, so it should actually add this path to lodash.
// This is because doing _pick(formData, "participants.0.name") gives us {} (i.e. removes the participant) whereas _pick(formData, "participants.0") gives us {}, which is what we want.
acc.push(path);
}
});
}
});
Expand Down Expand Up @@ -194,6 +201,7 @@ export default class Form extends Component {
);

newFormData = this.getUsedFormData(formData, fieldNames);

state = {
formData: newFormData,
};
Expand Down Expand Up @@ -237,10 +245,12 @@ export default class Form extends Component {
event.persist();
let newFormData = this.state.formData;

const { pathSchema } = this.state;

if (this.props.omitExtraData === true) {
const fieldNames = this.getFieldNames(pathSchema, this.state.formData);
const newState = this.getStateFromProps(this.props, this.state.formData);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: is this line of code needed?

const fieldNames = this.getFieldNames(
newState.pathSchema,
newState.formData
);
newFormData = this.getUsedFormData(this.state.formData, fieldNames);
}

Expand Down
17 changes: 17 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import * as ReactIs from "react-is";
import _get from "lodash/get";
import fill from "core-js/library/fn/array/fill";
import validateFormData, { isValid } from "./validate";

Expand Down Expand Up @@ -1074,3 +1075,19 @@ export function getMatchingOption(formData, options, definitions) {
}
return 0;
}

function removeLastPart(input) {
let parts = input.split(".");
if (parts.length < 2) {
return input;
}
parts.pop();
return parts.join(".");
}

export function getLastPart(data, path) {
if (path.indexOf(".") === -1) {
return data;
}
return _get(data, removeLastPart(path));
}
Loading