Skip to content

Commit

Permalink
fix: preserveDeletedFieldsValues on add() action to handle `field…
Browse files Browse the repository at this point in the history
…s` prop

`preserveDeletedFieldsValues` on `add()` action to handle `fields` prop.
  • Loading branch information
foxhound87 committed Jun 17, 2023
1 parent c2d9244 commit 8d18b2f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 6.3.2 (master)
Fix: `preserveDeletedFieldsValues` on `add()` action to handle `fields` prop.

# 6.3.1 (master)
- Introduced field `computed` prop. To handle nested array fields computed values.

Expand Down
3 changes: 2 additions & 1 deletion src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,9 @@ export default class Base implements BaseInterface {
const $path = ($key: string) =>_.trimStart([this.path, $key].join("."), ".");
const tree = pathToFieldsTree(this.state.struct(), this.path, 0, true);
const field = this.initField(key, $path(key), _.merge(tree[0], obj));
const hasValues = _.has(obj, FieldPropsEnum.value) || _.has(obj, FieldPropsEnum.fields);

if(!_.has(obj, FieldPropsEnum.value) && !this.state.options.get(OptionsEnum.preserveDeletedFieldsValues, this)) {
if(!hasValues && !this.state.options.get(OptionsEnum.preserveDeletedFieldsValues, this)) {
const fallbackValueOption = this.state.options.get(OptionsEnum.fallbackValue, this);
field.$value = defaultValue({ fallbackValueOption, value: field.$value, type: field.type });
field.each((field: any) => field.$value = defaultValue({ fallbackValueOption, value: field.$value, type: field.type }));
Expand Down
3 changes: 1 addition & 2 deletions src/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ const setupDefaultProp = (
{ isEmptyArray: boolean, fallbackValueOption: any }
) =>
parseInput((val) => val, {
defaultValue,
isEmptyArray,
type: instance.type,
unified: update
Expand Down Expand Up @@ -413,7 +412,7 @@ export default class Field extends Base implements FieldInterface {
}

get validatedValue() {
return parseCheckOutput(this, this.validatedWith as string);
return parseCheckOutput(this, this.validatedWith);
}

get error() {
Expand Down
3 changes: 3 additions & 0 deletions tests/data/_.fixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import $R from "./forms/fixes/form.r";
import $S from "./forms/fixes/form.s";
import $T from "./forms/fixes/form.t";
import $U from "./forms/fixes/form.u";
import $V from "./forms/fixes/form.v";

import $425 from "./forms/fixes/form.425";
import $472 from "./forms/fixes/form.472";
Expand Down Expand Up @@ -67,6 +68,8 @@ export default {
$S,
$T,
$U,
$V,

$425,
$472,
$480,
Expand Down
34 changes: 34 additions & 0 deletions tests/data/forms/fixes/form.v.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

import { Form } from "../../../../src";
import FormInterface from "../../../../src/models/FormInterface";

const generateNewPiece = () => ({
length: Math.floor(Math.random() * 10),
width: Math.floor(Math.random() * 10),
height: Math.floor(Math.random() * 10)
});

export default new Form(
{
fields: [
"pieces[]",
"pieces[].length",
"pieces[].width",
"pieces[].height"
],
values: { pieces: [{ length: 1, width: 2, height: 3 }] }
},
{
options: {
// preserveDeletedFieldsValues: true
},
hooks: {
onInit(form: FormInterface) {
form.$("pieces").add({
fields: generateNewPiece(),
// values: generateNewPiece(), // EQUIVALENT
});
}
}
}
);
13 changes: 12 additions & 1 deletion tests/fixes.values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,4 +803,15 @@ describe('#615, not to add nested array item', () => {
expect($form.select('a.0.b.c', null, false)).not.to.be.undefined
expect($form.select('a.0.b.c.0', null, false)).to.be.undefined
})
})

});

describe('$V Field values checks', () => {
it('$V pieces[0].length values to be not equal zero', () => expect($.$V.$('pieces[0].length').value).to.be.not.equal(0));
it('$V pieces[0].width values to be not equal zero', () => expect($.$V.$('pieces[0].width').value).to.be.not.equal(0));
it('$V pieces[0].height values to be not equal zero', () => expect($.$V.$('pieces[0].height').value).to.be.not.equal(0));

it('$V pieces[1].length values to be not equal zero', () => expect($.$V.$('pieces[1].length').value).to.be.not.equal(0));
it('$V pieces[1].width values to be not equal zero', () => expect($.$V.$('pieces[1].width').value).to.be.not.equal(0));
it('$V pieces[1].height values to be not equal zero', () => expect($.$V.$('pieces[1].height').value).to.be.not.equal(0));
});

0 comments on commit 8d18b2f

Please sign in to comment.