Skip to content

Commit

Permalink
fix: fix: input function removed from set value
Browse files Browse the repository at this point in the history
Fix: `input` function removed from `set value`.
  • Loading branch information
foxhound87 committed Apr 15, 2023
1 parent dd6376f commit ed07ffd
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 16 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 6.2.3 (master)
- Fix: `input` function removed from `set value` (applied only on field init)
- Introduced `applyInputConverterOnInit`, `applyInputConverterOnSet`, `applyInputConverterOnUpdate` form options.
- Introduced `converter` function applied on `set value`.
- Fix: `input` function removed from `set value`.

# 6.2.2 (master)
- Fix: mobx cycle detected in computation (get() strict mode)
Expand Down
29 changes: 21 additions & 8 deletions src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,11 @@ export default class Base implements BaseInterface {
// try to get props from separated objects
const _try = (prop: string) => {
const t = _.get(initial[prop], struct);
const isIoProp: boolean = (prop === FieldPropsEnum.input || prop === FieldPropsEnum.output);
if (isIoProp && typeof t !== "function") return undefined;
if (([
FieldPropsEnum.input,
FieldPropsEnum.output,
FieldPropsEnum.converter,
] as string[]).includes(prop) && typeof t !== "function") return undefined;
return t;
};

Expand All @@ -293,6 +296,7 @@ export default class Base implements BaseInterface {
$validators: _try(SeparatedPropsMode.validators),
$observers: _try(SeparatedPropsMode.observers),
$interceptors: _try(SeparatedPropsMode.interceptors),
$converters: _try(SeparatedPropsMode.converters),
$input: _try(SeparatedPropsMode.input),
$output: _try(SeparatedPropsMode.output),
$autoFocus: _try(SeparatedPropsMode.autoFocus),
Expand Down Expand Up @@ -421,9 +425,8 @@ export default class Base implements BaseInterface {
const $key = _.has(field, FieldPropsEnum.name) ? field.name : key;
const $path = _.trimStart(`${path}.${$key}`, ".");
const $field = this.select($path, null, false);
const $container =
this.select(path, null, false) ||
this.state.form.select(this.path, null, false);
const $container = this.select(path, null, false) || this.state.form.select(this.path, null, false);
const applyInputConverterOnUpdate = this.state.options.get(OptionsEnum.applyInputConverterOnUpdate, this);

if (!_.isNil($field) && !_.isUndefined(field)) {
if (_.isArray($field.values())) {
Expand All @@ -440,12 +443,18 @@ 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 = _.get(raw, $path);
$field.value = parseInput(applyInputConverterOnUpdate ? $field.$input : (val) => val, {
fallbackValueOption: this.state.options.get(OptionsEnum.fallbackValue, this),
separated: _.get(raw, $path),
});
return;
}
}
if (_.isNull(field) || _.isNil(field.fields)) {
$field.value = field;
$field.value = parseInput(applyInputConverterOnUpdate ? $field.$input : (val) => val, {
fallbackValueOption: this.state.options.get(OptionsEnum.fallbackValue, this),
separated: field,
});
return;
}
}
Expand Down Expand Up @@ -566,7 +575,11 @@ export default class Base implements BaseInterface {
if (deep && this.hasNestedFields) return this.deepSet(prop, data, "", true);

if (prop === FieldPropsEnum.value) {
(this as any).value = data;
const applyInputConverterOnSet = this.state.options.get(OptionsEnum.applyInputConverterOnSet, this);
(this as any).value = parseInput(applyInputConverterOnSet ? (this as any).$input : (val) => val, {
fallbackValueOption: this.state.options.get(OptionsEnum.fallbackValue, this),
separated: data,
});
} else {
_.set(this, `$${prop}`, data);
}
Expand Down
22 changes: 15 additions & 7 deletions src/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export default class Field extends Base implements FieldInterface {
$observers: any;
$interceptors: any;

$converter = ($: any) => $;
$input = ($: any) => $;
$output = ($: any) => $;

Expand Down Expand Up @@ -262,7 +263,7 @@ 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 = this.$converter(_.toNumber(newVal));
this.$changed ++;
if (!this.actionRunning) {
this.state.form.$changed ++;
Expand All @@ -271,7 +272,7 @@ export default class Field extends Base implements FieldInterface {
}
}
}
this.$value = newVal;
this.$value = this.$converter(newVal);
this.$changed ++;
if (!this.actionRunning) {
this.state.form.$changed ++;
Expand Down Expand Up @@ -506,6 +507,7 @@ export default class Field extends Base implements FieldInterface {
this.path = $path;
this.id = this.state.options.get(OptionsEnum.uniqueId)?.apply(this, [this]);
const fallbackValueOption = this.state.options.get(OptionsEnum.fallbackValue, this);
const applyInputConverterOnInit = this.state.options.get(OptionsEnum.applyInputConverterOnInit, this);
const struct = this.state.struct();
const structPath = pathToStruct(this.path);
const isEmptyArray: boolean = Array.isArray(struct)
Expand All @@ -514,17 +516,18 @@ export default class Field extends Base implements FieldInterface {
.find((s) => s.substring(structPath.length) === "[]")
: !!Array.isArray(_.get(struct, this.path));

const { $type, $input, $output } = $props;
const { $type, $input, $output, $converter } = $props;

if (_.isPlainObject($data)) {
const { type, input, output } = $data;
const { type, input, output, converter } = $data;

this.name = _.toString($data.name || $key);
this.$type = $type || type || "text";
this.$converter = $try($converter, converter, this.$converter);
this.$input = $try($input, input, this.$input);
this.$output = $try($output, output, this.$output);

this.$value = parseInput(this.$input, {
this.$value = parseInput(applyInputConverterOnInit ? this.$input : (val) => val, {
fallbackValueOption,
isEmptyArray,
type: this.type,
Expand Down Expand Up @@ -555,10 +558,11 @@ export default class Field extends Base implements FieldInterface {
/* The field IS the value here */
this.name = _.toString($key);
this.$type = $type || "text";
this.$converter = $try($converter, this.$converter);
this.$input = $try($input, this.$input);
this.$output = $try($output, this.$output);

this.$value = parseInput(this.$input, {
this.$value = parseInput(applyInputConverterOnInit ? this.$input : (val) => val, {
fallbackValueOption,
isEmptyArray,
type: this.type,
Expand Down Expand Up @@ -813,9 +817,13 @@ export default class Field extends Base implements FieldInterface {
throw new Error("The update() method accepts only plain objects.");
}
const fallback = this.state.options.get(OptionsEnum.fallback, this);
const applyInputConverterOnUpdate = this.state.options.get(OptionsEnum.applyInputConverterOnUpdate, this);
const x = this.state.struct().findIndex(s => s.startsWith(this.path.replace(/\.\d+\./, '[].') + '[]'));
if (!fallback && this.fields.size === 0 && x < 0) {
this.value = fields;
this.value = parseInput(applyInputConverterOnUpdate ? this.$input : (val) => val, {
fallbackValueOption: this.state.options.get(OptionsEnum.fallbackValue, this),
separated: fields,
});
return;
}
super.update(fields);
Expand Down
3 changes: 3 additions & 0 deletions src/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export default class Options implements OptionsInterface {
validationPluginsOrder: undefined,
resetValidationBeforeValidate: true,
validateTrimmedValue: false,
applyInputConverterOnInit: true,
applyInputConverterOnSet: true,
applyInputConverterOnUpdate: true,
};

constructor() {
Expand Down
2 changes: 2 additions & 0 deletions src/models/FieldProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export enum FieldPropsEnum {
bindings = "bindings",
hooks = "hooks",
handlers = "handlers",
converter="converter",
input="input",
output="output",
interceptors = "interceptors",
Expand Down Expand Up @@ -98,6 +99,7 @@ export enum SeparatedPropsMode {
validators = 'validators',
observers = 'observers',
interceptors = 'interceptors',
converters = 'converters',
input = 'input',
output = 'output',
autoFocus = 'autoFocus',
Expand Down
6 changes: 6 additions & 0 deletions src/models/OptionsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export enum OptionsEnum {
stopValidationOnError = 'stopValidationOnError',
validationPluginsOrder = 'validationPluginsOrder',
resetValidationBeforeValidate = 'resetValidationBeforeValidate',
applyInputConverterOnInit = 'applyInputConverterOnInit',
applyInputConverterOnSet = 'applyInputConverterOnSet',
applyInputConverterOnUpdate = 'applyInputConverterOnUpdate',
}


Expand Down Expand Up @@ -84,4 +87,7 @@ export default interface OptionsModel {
[OptionsEnum.stopValidationOnError]?: boolean;
[OptionsEnum.validationPluginsOrder]?: undefined | string[];
[OptionsEnum.resetValidationBeforeValidate]?: boolean;
[OptionsEnum.applyInputConverterOnInit]?: boolean,
[OptionsEnum.applyInputConverterOnSet]?: boolean,
[OptionsEnum.applyInputConverterOnUpdate]?: boolean,
}
2 changes: 2 additions & 0 deletions src/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const props: PropsGroupsInterface = {
SeparatedPropsMode.validators,
SeparatedPropsMode.observers,
SeparatedPropsMode.interceptors,
SeparatedPropsMode.converters,
SeparatedPropsMode.input,
SeparatedPropsMode.output,
SeparatedPropsMode.autoFocus,
Expand All @@ -92,6 +93,7 @@ export const props: PropsGroupsInterface = {
functions: [
FieldPropsEnum.observers,
FieldPropsEnum.interceptors,
FieldPropsEnum.converter,
FieldPropsEnum.input,
FieldPropsEnum.output,
],
Expand Down

0 comments on commit ed07ffd

Please sign in to comment.