Skip to content

Commit

Permalink
fix(core): allow control resetOnHide for a specific field (#2619)
Browse files Browse the repository at this point in the history
fix #2570
  • Loading branch information
aitboudad committed Dec 10, 2020
1 parent ddb26a7 commit f51a9df
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/core/json-schema/src/formly-json-schema.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function totalMatchedFields(field: FormlyFieldConfig): number {

interface IOptions extends FormlyJsonschemaOptions {
schema: JSONSchema7;
autoClear?: boolean;
resetOnHide?: boolean;
shareFormControl?: boolean;
ignoreDefault?: boolean;
}
Expand All @@ -65,8 +65,8 @@ export class FormlyJsonschema {
},
};

if (options.autoClear) {
field['autoClear'] = true;
if (options.resetOnHide) {
field['resetOnHide'] = true;
}

if (options.shareFormControl === false) {
Expand Down Expand Up @@ -158,7 +158,7 @@ export class FormlyJsonschema {
oneOfSchema.forEach(oneOfSchemaItem => {
const { [key]: constSchema, ...properties } = oneOfSchemaItem.properties;
field.fieldGroup.push({
...this._toFieldConfig({ ...oneOfSchemaItem, properties }, { ...options, autoClear: true }),
...this._toFieldConfig({ ...oneOfSchemaItem, properties }, { ...options, resetOnHide: true }),
hideExpression: m => !m || getConstValue(constSchema) !== m[key],
});
});
Expand Down Expand Up @@ -336,7 +336,7 @@ export class FormlyJsonschema {
},
{
fieldGroup: schemas.map((s, i) => ({
...this._toFieldConfig(s, { ...options, autoClear: true }),
...this._toFieldConfig(s, { ...options, resetOnHide: true }),
hideExpression: (m, fs, f) => {
const control = f.parent.parent.fieldGroup[0].formControl;
if (control.value === -1) {
Expand Down Expand Up @@ -481,7 +481,7 @@ export class FormlyJsonschema {
private isFieldValid(field: FormlyFieldConfig, schema: JSONSchema7, options: IOptions): boolean {
const { form } = (field.options as any)._buildField({
form: new FormGroup({}),
fieldGroup: [this._toFieldConfig(schema, { ...options, autoClear: true, ignoreDefault: true })],
fieldGroup: [this._toFieldConfig(schema, { ...options, resetOnHide: true, ignoreDefault: true })],
model: field.model ? clone(field.model) : (field.fieldArray ? [] : {}),
});

Expand Down
1 change: 1 addition & 0 deletions src/core/src/lib/components/formly.field.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export interface FormlyFieldConfigCache extends FormlyFieldConfig {
parent?: FormlyFieldConfigCache;
options?: FormlyFormOptionsCache;
_expressionProperties?: { [property: string]: ExpressionPropertyCache };
resetOnHide?: boolean;
_hide?: boolean;
_validators?: ValidatorFn[];
_asyncValidators?: AsyncValidatorFn[];
Expand Down
8 changes: 4 additions & 4 deletions src/core/src/lib/extensions/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ export class CoreExtension implements FormlyExtension {
},
});

if (this.formlyConfig.extras.resetFieldOnHide) {
field['autoClear'] = true;
if (this.formlyConfig.extras.resetFieldOnHide && field.resetOnHide !== false) {
field.resetOnHide = true;
}

if (field.lifecycle) {
Expand Down Expand Up @@ -100,8 +100,8 @@ export class CoreExtension implements FormlyExtension {
let setDefaultValue = !isUndefined(field.key)
&& !isUndefined(field.defaultValue)
&& isUndefined(getFieldValue(field))
&& (!field['autoClear'] || !(field.hide || field.hideExpression));
if (setDefaultValue && field['autoClear']) {
&& (!field.resetOnHide || !(field.hide || field.hideExpression));
if (setDefaultValue && field.resetOnHide) {
let parent = field.parent;
while (parent && !parent.hideExpression && !parent.hide) {
parent = parent.parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export class FieldExpressionExtension implements FormlyExtension {

if (hide === true && c['_fields'].every(f => !!f._hide)) {
unregisterControl(field);
if (resetOnHide && field['autoClear']) {
if (resetOnHide && field.resetOnHide) {
field.formControl.reset({ value: undefined, disabled: field.formControl.disabled });
if (field.fieldGroup) {
assignFieldValue(field, undefined);
Expand All @@ -241,11 +241,11 @@ export class FieldExpressionExtension implements FormlyExtension {
}
}
} else if (hide === false) {
if (field['autoClear'] && field.parent && !isUndefined(field.defaultValue) && isUndefined(getFieldValue(field))) {
if (field.resetOnHide && field.parent && !isUndefined(field.defaultValue) && isUndefined(getFieldValue(field))) {
assignFieldValue(field, field.defaultValue);
}
registerControl(field);
if (field['autoClear'] && field.fieldArray && (field.fieldGroup || []).length !== (field.model || []).length) {
if (field.resetOnHide && field.fieldArray && (field.fieldGroup || []).length !== (field.model || []).length) {
(<any> field.options)._buildForm(true);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function assignFieldValue(field: FormlyFieldConfigCache, value: any) {
paths = [...getKeyPath(root), ...paths];
}

if (value === undefined && field['autoClear']) {
if (value === undefined && field.resetOnHide) {
const k = paths.pop();
const m = paths.reduce((model, path) => model[path] || {}, root.model);
delete m[k];
Expand Down

0 comments on commit f51a9df

Please sign in to comment.