Skip to content

Commit

Permalink
fix: using autoTrimValue option will not trigger onChange Event Hook
Browse files Browse the repository at this point in the history
using `autoTrimValue` option will not trigger `onChange` Event Hook
  • Loading branch information
foxhound87 committed Apr 3, 2023
1 parent 6ed5db6 commit 6223b4f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 5.9.1 (next)
Fix: using `autoTrimValue` option will not trigger `onChange` Event Hook

# 5.9.0 (next)

- Introduced `inputMode` Field property.
Expand Down
8 changes: 4 additions & 4 deletions src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
parseCheckArray,
parseCheckOutput,
pathToFieldsTree,
defaultClearValue,
defaultValue,
} from "./parser";
import { AllowedFieldPropsTypes, FieldPropsEnum, SeparatedPropsMode } from "./models/FieldProps";
import { OptionsEnum } from "./models/OptionsModel";
Expand Down Expand Up @@ -579,7 +579,7 @@ 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({
this.each((field: any) => field.$value = defaultValue({
value: field.$value,
type: field.type,
}));
Expand Down Expand Up @@ -633,8 +633,8 @@ export default class Base implements BaseInterface {
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 }));
field.$value = defaultValue({ value: field.$value, type: field.type });
field.each((field: any) => field.$value = defaultValue({ value: field.$value, type: field.type }));
}

this.$changed ++;
Expand Down
14 changes: 8 additions & 6 deletions src/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
parseInput,
parseCheckOutput,
defaultValue,
defaultClearValue,
} from "./parser";

import OptionsModel, { OptionsEnum } from "./models/OptionsModel";
Expand Down Expand Up @@ -59,7 +58,10 @@ const setupDefaultProp = (
isEmptyArray,
type: instance.type,
unified: update
? defaultValue({ type: instance.type })
? defaultValue({
type: instance.type,
value: instance.value
})
: data && data.default,
separated: props.$default,
fallback: instance.$initial,
Expand Down Expand Up @@ -255,6 +257,9 @@ export default class Field extends Base implements FieldInterface {
}

set value(newVal) {
if (_.isString(newVal) && this.state.options.get(OptionsEnum.autoTrimValue, this)) {
newVal = newVal.trim();
}
if (this.$value === newVal) return;
// handle numbers
if (this.state.options.get(OptionsEnum.autoParseNumbers, this)) {
Expand All @@ -271,9 +276,6 @@ export default class Field extends Base implements FieldInterface {
}
}
}
if (_.isString(newVal) && this.state.options.get(OptionsEnum.autoTrimValue, this)) {
newVal = newVal.trim();
}
this.$value = newVal;
this.$changed ++;
if (!this.resetting && !this.clearing) {
Expand Down Expand Up @@ -683,7 +685,7 @@ export default class Field extends Base implements FieldInterface {
this.$blurred = false;
this.$changed = 0;
this.files = undefined;
this.$value = defaultClearValue({
this.$value = defaultValue({
value: this.$value,
type: this.type,
});
Expand Down
25 changes: 8 additions & 17 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,20 @@ import {
isEmptyArray,
} from "./utils";

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

const defaultValue = ({
type,
type = undefined,
value = undefined,
nullable = false,
isEmptyArray = false,
}: any): null | false | 0 | [] | "" => {
if (type === "file") return null;
if (type === "nullable") return null;
if (type === "date") return null;
if (type === "datetime-local") return null;
if (type === "checkbox") return false;
if (type === "number") return 0;
if (_.isDate(value) || type === "date") return null;
if (_.isNumber(value) || type === "number") return 0;
if (_.isBoolean(value) || type === "checkbox") return false;
if (_.isArray(value)) return [];
if (_.isString(value)) return "";
if (nullable) return null;
if (isEmptyArray) return [];
return "";
Expand Down Expand Up @@ -320,7 +312,6 @@ const pathToFieldsTree = (

export {
defaultValue,
defaultClearValue,
parseInput,
parsePath,
parseArrayProp,
Expand Down

0 comments on commit 6223b4f

Please sign in to comment.