Skip to content

Commit

Permalink
[WIP] Live omit (#1332)
Browse files Browse the repository at this point in the history
* feat: add liveOmit prop

* fix: fix live omit

* fix: only live omit when liveOmit is true

* doc: update doc
  • Loading branch information
epicfaace authored Jun 29, 2019
1 parent fe62b49 commit e98a016
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ render((
), document.getElementById("app"));
```

> Note: If there are fields in the `formData` that are not represented in the schema, they will be retained by default. If you would like to remove those extra values on form submission, then set the `omitExtraData` prop to `true`.
> Note: If there are fields in the `formData` that are not represented in the schema, they will be retained by default. If you would like to remove those extra values on form submission, then set the `omitExtraData` prop to `true`. Set the `liveOmit` prop to true in order to remove extra data upon form data change.
#### Form error event handler

Expand All @@ -137,7 +137,7 @@ render((

If you plan on being notified every time the form data are updated, you can pass an `onChange` handler, which will receive the same args as `onSubmit` any time a value is updated in the form.

> Note: If `omitExtraData` and `liveValidate` are both set to true, then extra form data values that are not in any form field will be removed whenever `onChange` is called.
> Note: If `omitExtraData` and `liveOmit` are both set to true, then extra form data values that are not in any form field will be removed whenever `onChange` is called.
#### Form field blur events

Expand Down
5 changes: 4 additions & 1 deletion src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export default class Form extends Component {
let state = { formData };
let newFormData = formData;

if (this.props.omitExtraData === true && this.props.liveValidate) {
if (this.props.omitExtraData === true && this.props.liveOmit === true) {
const newState = this.getStateFromProps(this.props, formData);

const fieldNames = this.getFieldNames(
Expand All @@ -184,6 +184,9 @@ export default class Form extends Component {
);

newFormData = this.getUsedFormData(formData, fieldNames);
state = {
formData: newFormData,
};
}

if (mustValidate) {
Expand Down
6 changes: 3 additions & 3 deletions test/Form_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ describe("Form", () => {
});
});

it("should call getUsedFormData when the omitExtraData prop is true and liveValidate is true", () => {
it("should call getUsedFormData when the omitExtraData prop is true and liveOmit is true", () => {
const schema = {
type: "object",
properties: {
Expand All @@ -1124,13 +1124,13 @@ describe("Form", () => {
};
const onChange = sandbox.spy();
const omitExtraData = true;
const liveValidate = true;
const liveOmit = true;
const { node, comp } = createFormComponent({
schema,
formData,
onChange,
omitExtraData,
liveValidate,
liveOmit,
});

sandbox.stub(comp, "getUsedFormData").returns({
Expand Down

0 comments on commit e98a016

Please sign in to comment.