Skip to content

Commit

Permalink
Merge branch 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
foxhound87 committed Mar 26, 2023
2 parents 7a33065 + 1a6af13 commit 30a87cc
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 18 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# 5.5.1 (next)

- Introduced `preserveDeletedFieldsValues` form option (disabled by default).
- Fix: after deleting and adding same field, the defined initial values will not be preserverd anymore.

# 5.5.0 (next)

- Updated add()/del()/update() actions to handle `changed` field prop.
(not triggering anymore `onChange` when using add(), use `onAdd`/`onDel` hooks instead).
- Updated `changed` computed prop behavior for nested fields and Event Hooks triggering.
- Events Hooks now are triggered also from actions if not used Event Handlers.

fix: #585 #531
- fix: #585 #531

# 5.4.1 (next)

Expand Down
14 changes: 11 additions & 3 deletions src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,10 @@ export default class Base implements BaseInterface {
const isStrict = this.state.options.get(OptionsEnum.strictUpdate, this);

if (_.isNil(data)) {
this.each((field: any) => field.$value = defaultClearValue({ value: field.$value }));
this.each((field: any) => field.$value = defaultClearValue({
value: field.$value,
type: field.type,
}));
return;
}

Expand Down Expand Up @@ -607,14 +610,19 @@ export default class Base implements BaseInterface {
}

let key;
if (_.has(obj, "key")) key = obj.key;
if (_.has(obj, "name")) key = obj.name;
if (_.has(obj, FieldPropsEnum.key)) key = obj.key;
if (_.has(obj, FieldPropsEnum.name)) key = obj.name;
if (!key) key = maxKey(this.fields);

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));

if(!_.has(obj, FieldPropsEnum.value) && !this.state.options.get(OptionsEnum.preserveDeletedFieldsValues, this)) {
field.$value = defaultClearValue({ value: field.$value, type: field.type });
field.each((field: any) => field.$value = defaultClearValue({ value: field.$value, type: field.type }));
}

this.$changed ++;
this.state.form.$changed ++;
execEvent && this.execHook(FieldPropsEnum.onAdd);
Expand Down
5 changes: 4 additions & 1 deletion src/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,10 @@ export default class Field extends Base implements FieldInterface {
this.$blurred = false;
this.$changed = 0;
this.files = undefined;
this.$value = defaultClearValue({ value: this.$value });
this.$value = defaultClearValue({
value: this.$value,
type: this.type,
});

if (deep) this.each((field: FieldInterface) => field.clear(true));

Expand Down
1 change: 1 addition & 0 deletions src/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default class Options implements OptionsInterface {
retrieveOnlyEnabledFieldsErrors: false,
autoParseNumbers: false,
removeNullishValuesInArrays: false,
preserveDeletedFieldsValues: false,
validationDebounceWait: 250,
validationDebounceOptions: {
leading: false,
Expand Down
1 change: 1 addition & 0 deletions src/models/FieldProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum FieldPropsEnum {
key = "key",
id = "id",
path = "path",
name = "name",
Expand Down
2 changes: 2 additions & 0 deletions src/models/OptionsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export enum OptionsEnum {
retrieveOnlyEnabledFieldsValues = 'retrieveOnlyEnabledFieldsValues',
retrieveOnlyEnabledFieldsErrors = 'retrieveOnlyEnabledFieldsErrors',
removeNullishValuesInArrays = 'removeNullishValuesInArrays',
preserveDeletedFieldsValues = 'preserveDeletedFieldsValues',
autoParseNumbers = 'autoParseNumbers',
validationDebounceWait = 'validationDebounceWait',
validationDebounceOptions = 'validationDebounceOptions',
Expand Down Expand Up @@ -62,6 +63,7 @@ export default interface OptionsModel {
[OptionsEnum.retrieveOnlyEnabledFieldsValues]?: boolean;
[OptionsEnum.retrieveOnlyEnabledFieldsErrors]?: boolean;
[OptionsEnum.removeNullishValuesInArrays]?: boolean;
[OptionsEnum.preserveDeletedFieldsValues]?: boolean;
[OptionsEnum.autoParseNumbers]?: boolean;
[OptionsEnum.validationDebounceWait]?: number;
[OptionsEnum.validationDebounceOptions]?: {
Expand Down
24 changes: 12 additions & 12 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import {
isEmptyArray,
} from "./utils";

const defaultClearValue = ({
value,
}: {
value: any;
}): false | any[] | 0 | "" | null | undefined => {
if (_.isArray(value)) return [];
if (_.isDate(value)) return null;
if (_.isBoolean(value)) return false;
if (_.isNumber(value)) return 0;
if (_.isString(value)) return "";
return undefined;
};
const defaultClearValue =
({ value = undefined, type = undefined }
: { value: any, type?: string })
: false | any[] | 0 | "" | null | undefined => {
if (type === "date") return null;
if (_.isDate(value)) return null;
if (_.isArray(value)) return [];
if (_.isBoolean(value)) return false;
if (_.isNumber(value)) return 0;
if (_.isString(value)) return "";
return undefined;
};

const defaultValue = ({
type,
Expand Down

0 comments on commit 30a87cc

Please sign in to comment.