Skip to content

Commit

Permalink
editor: fix can remove field when a hide expression exists
Browse files Browse the repository at this point in the history
* Avoids the possiblity to remove or add a field with an hide expression (tash button on the label component).
* Adds an exemple in the demo editor.
* Does not add a field with an hide expression during the edit initialization. In the previous versions the field was in the editor and in the completion menu.
* Fixes hide then show a field of array type at the root level. An example has been added.

Co-Authored-by: Johnny Mariéthoz <[email protected]>
  • Loading branch information
jma committed Jul 30, 2024
1 parent e3a4402 commit 065d918
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
"markdown": "Hello **world**.\nGreat day.",
"array_with_multicheckbox": ["checkbox1", "checkbox2"],
"input_with_default_value": "slrofar6uh",
"enum": "val1"
"enum": "val1",
"notes": [
{
"type": "staff_note",
"content": "test"
}
]
},
"updated": "2024-03-06T07:47:33.944197+00:00"
}
102 changes: 102 additions & 0 deletions projects/ng-core-tester/src/app/record/editor/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
"required"
],
"propertiesOrder": [
"notes",
"oneOf",
"optional",
"required",
"optional_hide_expression_field",
"essential",
"hidden",
"defaultHidden",
Expand Down Expand Up @@ -47,6 +49,91 @@
"field_radio_button_inline"
],
"properties": {
"notes": {
"title": "Notes",
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"additionalProperties": false,
"title": "Note",
"propertiesOrder": [
"type",
"content"
],
"required": [
"type",
"content"
],
"properties": {
"type": {
"type": "string",
"title": "Type",
"enum": [
"staff_note",
"vendor_note"
],
"default": "staff_note",
"widget": {
"formlyConfig": {
"type": "selectWithSort",
"props": {
"options": [
{
"label": "vendor_note",
"value": "vendor_note"
},
{
"label": "staff_note",
"value": "staff_note"
}
]
}
}
}
},
"content": {
"type": "string",
"title": "Content",
"maxLength": 2000,
"minLength": 1,
"widget": {
"formlyConfig": {
"type": "textarea",
"props": {
"rows": 3
}
}
}
}
}
},
"widget": {
"formlyConfig": {
"wrappers": [
"card"
],
"props": {
"validation": {
"validators": {
"uniqueValueKeysInObject": {
"keys": [
"type"
]
}
},
"messages": {
"uniqueValueKeysInObjectMessage": "Only one note per type is allowed"
}
},
"hide": true,
"navigation": {
"essential": true
}
}
}
}
},
"$schema": {
"title": "Schema",
"type": "string",
Expand Down Expand Up @@ -251,6 +338,21 @@
}
}
},
"optional_hide_expression_field": {
"title": "Optional Field with Hide Expression",
"type": "string",
"minLength": 3,
"widget": {
"formlyConfig": {
"expressions": {
"hide": "!field.parent.model.required"
},
"props": {
"doNotSubmitOnEnter": true
}
}
}
},
"enum": {
"title": "List of values",
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,8 @@ export class EditorComponent extends AbstractCanDeactivateComponent implements O
}
return (
!field.props.required &&
!field.hide
!field.hide &&
!('hide' in field?.expressions)
);
}

Expand Down
2 changes: 2 additions & 0 deletions projects/rero/ng-core/src/lib/record/editor/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ export class NgCoreFormlyExtension {
// ignore array item which as key of the form "0"
// TODO: find a better way to identify this case
|| !isNaN(Number(field.key))
// ignore field that has hide expression
|| ('hide' in field?.expressions)
// do not hide a field containing a 'hide' wrapper
|| this._hasHideWrapper(field)
// do not hide a field that has a parent marked as hidden and a model is empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ export class AddFieldEditorComponent implements OnInit {
* @param match - TypeaheadMath, the selected element
*/
showSelectedField(field: any) {
// show the field in the form
field.hide = false;
// reset the input value
this.value = undefined;
// remove the the element from the list of hidden fields
Expand All @@ -164,6 +162,8 @@ export class AddFieldEditorComponent implements OnInit {
// See: https://blog.angular-university.io/angular-debugging/
// wait that the component is present in the DOM
setTimeout(() => this.editorComponentInstance.setFieldFocus(field, true));
// show the field in the form
field.hide = false;
}

/**
Expand Down

0 comments on commit 065d918

Please sign in to comment.