Skip to content

Commit

Permalink
fix: fix: input function applied 2 times when using set()
Browse files Browse the repository at this point in the history
Fix: input function applied 2 times when using `set()`
  • Loading branch information
foxhound87 committed Apr 14, 2023
1 parent 5f31d7e commit da89513
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 6.2.1 (master)
- Refactored set value with input function
- Deprecated input function on `initial` and `default` props.
- Fix: input function applied 2 times when using `set()`

# 6.2.0 (master)
- Feat: #433 (File input append mode)

Expand Down
21 changes: 4 additions & 17 deletions src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,18 +440,12 @@ export default class Base implements BaseInterface {
const fallback = this.state.options.get(OptionsEnum.fallback);
const x = this.state.struct().findIndex(s => s.startsWith($field.path.replace(/\.\d+\./, '[].') + '[]'));
if (!fallback && $field.fields.size === 0 && x < 0) {
$field.value = parseInput($field.$input, {
fallbackValueOption: this.state.options.get(OptionsEnum.fallbackValue, this),
separated: _.get(raw, $path),
});
$field.value = _.get(raw, $path);
return;
}
}
if (_.isNull(field) || _.isNil(field.fields)) {
$field.value = parseInput($field.$input, {
fallbackValueOption: this.state.options.get(OptionsEnum.fallbackValue, this),
separated: field,
});
$field.value = field;
return;
}
}
Expand Down Expand Up @@ -571,15 +565,8 @@ export default class Base implements BaseInterface {
const deep = (_.isObject(data) && prop === FieldPropsEnum.value) || _.isPlainObject(data);
if (deep && this.hasNestedFields) return this.deepSet(prop, data, "", true);

if (([
FieldPropsEnum.value,
FieldPropsEnum.initial,
FieldPropsEnum.default,
] as string[]).includes(prop)) {
(this as any)[prop] = parseInput((this as any).$input, {
fallbackValueOption: this.state.options.get(OptionsEnum.fallbackValue, this),
separated: data,
});
if (prop === FieldPropsEnum.value) {
(this as any).value = data;
} else {
_.set(this, `$${prop}`, data);
}
Expand Down
35 changes: 17 additions & 18 deletions src/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const setupDefaultProp = (
{ isEmptyArray, fallbackValueOption }:
{ isEmptyArray: boolean, fallbackValueOption: any }
) =>
parseInput(instance.$input, {
parseInput((val) => val, {
defaultValue,
nullable: true,
isEmptyArray,
Expand Down Expand Up @@ -262,16 +262,24 @@ export default class Field extends Base implements FieldInterface {
if (
new RegExp("^-?\\d+(,\\d+)*(\\.\\d+([eE]\\d+)?)?$", "g").exec(newVal)
) {
this.$value = _.toNumber(newVal);
this.$value = parseInput(this.$input, {
fallbackValueOption: this.state.options.get(OptionsEnum.fallbackValue, this),
separated: _.toNumber(newVal),
});

this.$changed ++;
if (!this.resetting && !this.clearing) {
if (!this.actionRunning) {
this.state.form.$changed ++;
};
return;
}
}
}
this.$value = newVal;
this.$value = parseInput(this.$input, {
fallbackValueOption: this.state.options.get(OptionsEnum.fallbackValue, this),
separated: newVal,
});

this.$changed ++;
if (!this.actionRunning) {
this.state.form.$changed ++;
Expand All @@ -291,17 +299,11 @@ export default class Field extends Base implements FieldInterface {
}

set initial(val) {
this.$initial = parseInput(this.$input, {
fallbackValueOption: this.state.options.get(OptionsEnum.fallbackValue, this),
separated: val,
});
this.$initial = val;
}

set default(val) {
this.$default = parseInput(this.$input, {
fallbackValueOption: this.state.options.get(OptionsEnum.fallbackValue, this),
separated: val,
});
this.$default = val;
}

get actionRunning() {
Expand Down Expand Up @@ -539,7 +541,7 @@ export default class Field extends Base implements FieldInterface {
fallback: $props.$initial,
});

this.$initial = parseInput(this.$input, {
this.$initial = parseInput((val) => val, {
fallbackValueOption,
nullable: true,
isEmptyArray,
Expand Down Expand Up @@ -572,7 +574,7 @@ export default class Field extends Base implements FieldInterface {
separated: $props.$value,
});

this.$initial = parseInput(this.$input, {
this.$initial = parseInput((val) => val, {
fallbackValueOption,
nullable: true,
isEmptyArray,
Expand Down Expand Up @@ -821,10 +823,7 @@ export default class Field extends Base implements FieldInterface {
const fallback = this.state.options.get(OptionsEnum.fallback, this);
const x = this.state.struct().findIndex(s => s.startsWith(this.path.replace(/\.\d+\./, '[].') + '[]'));
if (!fallback && this.fields.size === 0 && x < 0) {
this.value = parseInput(this.$input, {
fallbackValueOption: this.state.options.get(OptionsEnum.fallbackValue, this),
separated: fields,
});
this.value = fields;
return;
}
super.update(fields);
Expand Down

0 comments on commit da89513

Please sign in to comment.