Skip to content

Commit 7c35420

Browse files
authored
fix: deletion of last field group item not happening (#297)
1 parent 49cde88 commit 7c35420

File tree

4 files changed

+88
-48
lines changed

4 files changed

+88
-48
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div id="field-group">
2-
<h3 *ngIf="showLabel$" className="label" style="font-weight: bold">
2+
<h3 *ngIf="showLabel$" class="field-group-template-header" style="font-weight: bold">
33
{{ label$ }}
44
</h3>
55
<div *ngIf="readonlyMode; else editable">
@@ -11,23 +11,23 @@ <h3 *ngIf="showLabel$" className="label" style="font-weight: bold">
1111
</div>
1212
<ng-template #editable>
1313
<div *ngIf="children && children.length > 0">
14-
<div *ngFor="let kid of children; let i = index">
14+
<div class="field-group-template-item" *ngFor="let child of children; let i = index">
1515
<div class="header-div">
1616
<div style="width: 80%">
17-
<b>{{ kid.name }}</b>
17+
<b>{{ child.name }}</b>
1818
</div>
19-
<div *ngIf="allowAddEdit !== false" style="width: 20%; text-align: right">
19+
<div *ngIf="allowDelete && child.allowRowDelete" style="width: 20%; text-align: right">
2020
<button id="delete-button" mat-icon-button (click)="deleteFieldGroupItem(i)">
2121
<img class="psdk-utility-card-action-svg-icon" src="{{ menuIconOverride$ }}" />
2222
</button>
2323
</div>
2424
</div>
2525

26-
<div *ngIf="kid.children.getPConnect().getRawMetadata().type.toLowerCase() == 'region'">
27-
<component-mapper name="Region" [props]="{ pConn$: kid.children.getPConnect(), formGroup$ }"></component-mapper>
26+
<div *ngIf="child.children.getPConnect().getRawMetadata().type.toLowerCase() == 'region'">
27+
<component-mapper name="Region" [props]="{ pConn$: child.children.getPConnect(), formGroup$ }"></component-mapper>
2828
</div>
2929
</div>
30-
<button *ngIf="allowAddEdit !== false" mat-button color="primary" style="font-size: 16px" (click)="addFieldGroupItem()">+ Add</button>
3130
</div>
31+
<button *ngIf="allowAdd" mat-button color="primary" style="font-size: 16px" (click)="addFieldGroupItem()">{{ getAddBtnLabel() }}</button>
3232
</ng-template>
3333
</div>

packages/angular-sdk-components/src/lib/_components/template/field-group-template/field-group-template.component.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@
66
display: flex;
77
align-items: center;
88
}
9+
10+
.field-group-template-header {
11+
margin-left: 0;
12+
}
13+
14+
.field-group-template-item {
15+
padding-block: 1rem;
16+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
import { Component, OnInit, Input, forwardRef, OnDestroy, OnChanges } from '@angular/core';
1+
import { Component, OnInit, Input, forwardRef, OnDestroy, OnChanges, AfterViewInit } from '@angular/core';
22
import { CommonModule } from '@angular/common';
33
import { FormGroup } from '@angular/forms';
44
import { MatButtonModule } from '@angular/material/button';
55
import { AngularPConnectData, AngularPConnectService } from '../../../_bridge/angular-pconnect';
66
import { buildView, getReferenceList } from '../../../_helpers/field-group-utils';
77
import { Utils } from '../../../_helpers/utils';
88
import { ComponentMapperComponent } from '../../../_bridge/component-mapper/component-mapper.component';
9+
import { evaluateAllowRowAction } from './utils';
910

1011
interface FieldGroupTemplateProps {
1112
// If any, enter additional props that only exist on this component
1213
label?: string;
13-
showLabel?: boolean;
14+
hideLabel?: boolean;
15+
allowActions?: any;
16+
allowRowDelete?: any;
1417
referenceList?: any[];
1518
contextClass: string;
1619
renderMode?: string;
@@ -19,6 +22,7 @@ interface FieldGroupTemplateProps {
1922
displayMode?: string;
2023
fieldHeader?: string;
2124
allowTableEdit: boolean;
25+
targetClassLabel?: string;
2226
}
2327

2428
@Component({
@@ -28,26 +32,27 @@ interface FieldGroupTemplateProps {
2832
standalone: true,
2933
imports: [CommonModule, MatButtonModule, forwardRef(() => ComponentMapperComponent)]
3034
})
31-
export class FieldGroupTemplateComponent implements OnInit, OnDestroy, OnChanges {
35+
export class FieldGroupTemplateComponent implements OnInit, OnDestroy, OnChanges, AfterViewInit {
3236
@Input() configProps$: FieldGroupTemplateProps;
3337
@Input() pConn$: typeof PConnect;
3438
@Input() formGroup$: FormGroup;
3539

3640
angularPConnectData: AngularPConnectData = {};
37-
inheritedProps$: object;
41+
3842
showLabel$?: boolean = true;
3943
label$?: string;
4044
readonlyMode: boolean;
4145
contextClass: any;
42-
referenceList: any;
43-
pageReference: any;
4446
heading: any;
4547
children: any;
4648
menuIconOverride$: any;
47-
prevRefLength: number;
48-
allowAddEdit: boolean;
49+
referenceListLength: number;
4950
fieldHeader: any;
5051

52+
allowAdd = true;
53+
allowEdit = true;
54+
allowDelete = true;
55+
5156
constructor(
5257
private angularPConnect: AngularPConnectService,
5358
private utils: Utils
@@ -58,9 +63,21 @@ export class FieldGroupTemplateComponent implements OnInit, OnDestroy, OnChanges
5863
this.angularPConnectData = this.angularPConnect.registerAndSubscribeComponent(this, this.onStateChange);
5964
this.updateSelf();
6065

61-
const menuIconOverride$ = 'trash';
62-
if (menuIconOverride$) {
63-
this.menuIconOverride$ = this.utils.getImageSrc(menuIconOverride$, this.utils.getSDKStaticContentUrl());
66+
this.menuIconOverride$ = this.utils.getImageSrc('trash', this.utils.getSDKStaticContentUrl());
67+
68+
const { allowActions, allowTableEdit, referenceList } = this.configProps$;
69+
70+
if (allowActions && Object.keys(allowActions).length > 0) {
71+
this.allowAdd = allowActions.allowAdd ?? allowTableEdit ?? true;
72+
this.allowEdit = allowActions.allowEdit ?? true;
73+
this.allowDelete = allowActions.allowDelete ?? allowTableEdit ?? true;
74+
} else {
75+
this.allowAdd = allowTableEdit ?? true;
76+
this.allowDelete = allowTableEdit ?? true;
77+
}
78+
79+
if (referenceList?.length === 0 && (this.allowAdd || this.allowEdit)) {
80+
this.pConn$.getListActions().insert({ classID: this.contextClass }, referenceList.length);
6481
}
6582
}
6683

@@ -85,56 +102,57 @@ export class FieldGroupTemplateComponent implements OnInit, OnDestroy, OnChanges
85102
const props = changes.configProps$;
86103
if (props.currentValue !== props.previousValue) {
87104
this.configProps$ = props.currentValue;
105+
88106
if (changes?.pConn$?.currentValue) {
89107
this.pConn$ = changes?.pConn$?.currentValue;
90108
}
109+
91110
this.updateSelf();
92111
}
93112
}
94113
}
95114

115+
ngAfterViewInit() {
116+
const resolvedList = getReferenceList(this.pConn$);
117+
// @ts-ignore - Expected 3 arguments, but got 1
118+
this.pConn$.getListActions().initDefaultPageInstructions(resolvedList);
119+
}
120+
96121
updateSelf() {
97-
this.inheritedProps$ = this.pConn$.getInheritedProps();
98-
this.label$ = this.configProps$.label;
99-
this.showLabel$ = this.configProps$.showLabel;
100-
// label & showLabel within inheritedProps takes precedence over configProps
101-
this.label$ = (this.inheritedProps$ as any).label || this.label$;
102-
this.showLabel$ = (this.inheritedProps$ as any).showLabel || this.showLabel$;
122+
const inheritedProps: any = this.pConn$.getInheritedProps();
103123

104-
this.allowAddEdit = this.configProps$.allowTableEdit;
124+
const { label, hideLabel, allowRowDelete, referenceList, fieldHeader, renderMode, displayMode, heading, contextClass, lookForChildInConfig } =
125+
this.configProps$;
126+
127+
// label within inheritedProps takes precedence over configProps
128+
this.label$ = inheritedProps.label || label;
129+
130+
this.showLabel$ = referenceList?.length === 0 || !hideLabel;
105131

106-
const renderMode = this.configProps$.renderMode;
107-
const displayMode = this.configProps$.displayMode;
108132
this.readonlyMode = renderMode === 'ReadOnly' || displayMode === 'DISPLAY_ONLY';
109-
this.contextClass = this.configProps$.contextClass;
110-
const lookForChildInConfig = this.configProps$.lookForChildInConfig;
111-
this.heading = this.configProps$.heading ?? 'Row';
112-
this.fieldHeader = this.configProps$.fieldHeader;
133+
134+
this.contextClass = contextClass;
135+
this.heading = heading ?? 'Row';
136+
this.fieldHeader = fieldHeader;
137+
113138
const resolvedList = getReferenceList(this.pConn$);
114-
this.pageReference = `${this.pConn$.getPageReference()}${resolvedList}`;
115139
this.pConn$.setReferenceList(resolvedList);
140+
116141
if (this.readonlyMode) {
117142
this.pConn$.setInheritedProp('displayMode', 'DISPLAY_ONLY');
118143
}
119-
this.referenceList = this.configProps$.referenceList;
120-
if (this.prevRefLength != this.referenceList?.length) {
121-
// eslint-disable-next-line sonarjs/no-collapsible-if
122-
if (!this.readonlyMode) {
123-
if (this.referenceList?.length === 0 && this.allowAddEdit !== false) {
124-
this.addFieldGroupItem();
125-
}
126-
}
127-
const children: any = [];
128-
this.referenceList?.forEach((item, index) => {
129-
children.push({
144+
145+
if (this.referenceListLength != referenceList?.length) {
146+
this.children = referenceList?.map((item, index) => {
147+
return {
130148
id: index,
131149
name: this.fieldHeader === 'propertyRef' ? this.getDynamicHeader(item, index) : this.getStaticHeader(this.heading, index),
132-
children: buildView(this.pConn$, index, lookForChildInConfig)
133-
});
150+
children: buildView(this.pConn$, index, lookForChildInConfig),
151+
allowRowDelete: evaluateAllowRowAction(allowRowDelete, item)
152+
};
134153
});
135-
this.children = children;
136154
}
137-
this.prevRefLength = this.referenceList.length;
155+
this.referenceListLength = referenceList?.length || 0;
138156
}
139157

140158
getStaticHeader = (heading, index) => {
@@ -149,10 +167,15 @@ export class FieldGroupTemplateComponent implements OnInit, OnDestroy, OnChanges
149167
};
150168

151169
addFieldGroupItem() {
152-
this.pConn$.getListActions().insert({ classID: this.contextClass }, this.referenceList.length);
170+
this.pConn$.getListActions().insert({ classID: this.contextClass }, this.referenceListLength);
153171
}
154172

155173
deleteFieldGroupItem(index) {
156174
this.pConn$.getListActions().deleteEntry(index);
157175
}
176+
177+
getAddBtnLabel() {
178+
const { targetClassLabel } = this.configProps$;
179+
return targetClassLabel ? `+ Add ${targetClassLabel}` : '+ Add';
180+
}
158181
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const evaluateAllowRowAction = (allowRowDelete, rowData) => {
2+
if (allowRowDelete === undefined || allowRowDelete === true) return true;
3+
if (allowRowDelete.startsWith?.('@E ')) {
4+
const expression = allowRowDelete.replace('@E ', '');
5+
// @ts-ignore
6+
return PCore.getExpressionEngine().evaluate(expression, rowData);
7+
}
8+
return false;
9+
};

0 commit comments

Comments
 (0)