Skip to content

Commit 95aa03c

Browse files
committed
fix: visibility issues in Default form template
1 parent f66a4d3 commit 95aa03c

File tree

5 files changed

+60
-17
lines changed

5 files changed

+60
-17
lines changed

packages/angular-sdk-components/src/lib/_components/infra/reference/reference.component.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,18 @@ export class ReferenceComponent {
3333
static createFullReferencedViewFromRef(inPConn: any) {
3434
// BAIL and ERROR if inPConn is NOT a reference!
3535
if (inPConn.getComponentName() !== 'reference') {
36-
// debugger;
37-
3836
console.error(`Reference component: createFullReferencedViewFromRef inPConn is NOT a reference! ${inPConn.getComponentName()}`);
3937
}
4038

41-
const theResolvedConfigProps = inPConn.resolveConfigProps(inPConn.getConfigProps());
42-
43-
const referenceConfig = { ...inPConn.getComponentConfig() } || {};
39+
const referenceConfig = { ...inPConn.getComponentConfig() };
4440

4541
// Since SDK-A implements Reference as static methods and we don't rely on
4642
// the Reference component's handling of the visibility prop, we leave it in
4743
// (and also leaving the others in for now) so the referenced View can act on
4844
// the visibility prop. (The following 3 lines were carried over from React SDK)
4945
delete referenceConfig?.name;
50-
// delete referenceConfig?.type;
51-
// delete referenceConfig?.visibility;
46+
delete referenceConfig?.type;
47+
delete referenceConfig?.visibility;
5248

5349
const viewMetadata = inPConn.getReferencedView();
5450

@@ -68,21 +64,24 @@ export class ReferenceComponent {
6864
}
6965
};
7066

67+
const theResolvedConfigProps = inPConn.resolveConfigProps(inPConn.getConfigProps());
68+
const { visibility = true, context, readOnly = false, displayMode = '' } = theResolvedConfigProps;
69+
7170
if (ReferenceComponent.bLogging) {
72-
console.log(`Reference: about to call createComponent with pageReference: context: ${theResolvedConfigProps.context}`);
71+
console.log(`Reference: about to call createComponent with pageReference: context: ${inPConn.getContextName()}`);
7372
}
7473

7574
const viewComponent = inPConn.createComponent(viewObject, null, null, {
76-
pageReference: theResolvedConfigProps.context
75+
pageReference: context && context.startsWith('@CLASS') ? '' : context
7776
});
7877

7978
// updating the referencedComponent should trigger a render
8079
const newCompPConnect = viewComponent.getPConnect();
8180

8281
newCompPConnect.setInheritedConfig({
8382
...referenceConfig,
84-
readOnly: theResolvedConfigProps.readOnly ? theResolvedConfigProps.readOnly : false,
85-
displayMode: theResolvedConfigProps.displayMode ? theResolvedConfigProps.displayMode : null
83+
readOnly,
84+
displayMode
8685
});
8786

8887
if (ReferenceComponent.bLogging) {
@@ -93,7 +92,11 @@ export class ReferenceComponent {
9392
);
9493
}
9594

96-
return newCompPConnect;
95+
if (visibility !== false) {
96+
return newCompPConnect;
97+
}
98+
99+
return null;
97100
}
98101

99102
// STATIC method that other components can call to normalize
@@ -130,7 +133,7 @@ export class ReferenceComponent {
130133
// debugger;
131134
let theRefViewPConn = this.createFullReferencedViewFromRef(inPConn.getPConnect());
132135
// now return its PConnect
133-
theRefViewPConn = theRefViewPConn.getComponent();
136+
theRefViewPConn = theRefViewPConn?.getComponent();
134137

135138
// const theFullReference = theRefViewPConn.getPConnect().getFullReference();
136139
// console.log(`theFullReference: ${theFullReference}`);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { Directive, OnDestroy } from '@angular/core';
2+
import { AngularPConnectData } from '../../../_bridge/angular-pconnect';
23

34
@Directive()
45
export class FormTemplateBase implements OnDestroy {
56
pConn$: any;
7+
angularPConnectData: AngularPConnectData;
68

79
ngOnDestroy(): void {
810
PCore.getContextTreeManager().removeContextTreeNode(this.pConn$.getContextName());
11+
12+
if (this.angularPConnectData?.unsubscribeFn) {
13+
this.angularPConnectData.unsubscribeFn();
14+
}
915
}
1016
}

packages/angular-sdk-components/src/lib/_components/template/default-form/default-form.component.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
import { Component, OnInit, Input, forwardRef } from '@angular/core';
22
import { CommonModule } from '@angular/common';
33
import { FormGroup } from '@angular/forms';
4+
import { AngularPConnectData, AngularPConnectService } from '../../../_bridge/angular-pconnect';
45
import { ReferenceComponent } from '../../infra/reference/reference.component';
56
import { ComponentMapperComponent } from '../../../_bridge/component-mapper/component-mapper.component';
67
import { TemplateUtils } from '../../../_helpers/template-utils';
78
import { FormTemplateBase } from '../base/form-template-base';
89

10+
function areViewsChanged(oldViews: any[], newViews: any[]): boolean {
11+
if (oldViews?.length !== newViews?.length) {
12+
return true;
13+
}
14+
15+
return !oldViews?.every((oldView, index) => {
16+
const newView = newViews[index];
17+
return oldView.getPConnect().viewName === newView.getPConnect().viewName;
18+
});
19+
}
20+
921
interface DefaultFormProps {
1022
// If any, enter additional props that only exist on this component
1123
NumCols: string;
@@ -23,15 +35,32 @@ export class DefaultFormComponent extends FormTemplateBase implements OnInit {
2335
@Input() override pConn$: typeof PConnect;
2436
@Input() formGroup$: FormGroup;
2537

38+
// Used with AngularPConnect
39+
override angularPConnectData: AngularPConnectData = {};
40+
2641
arChildren$: any[];
2742
divClass$: string;
2843
instructions: string;
2944

30-
constructor(private templateUtils: TemplateUtils) {
45+
constructor(
46+
private angularPConnect: AngularPConnectService,
47+
private templateUtils: TemplateUtils
48+
) {
3149
super();
3250
}
3351

3452
ngOnInit(): void {
53+
// First thing in initialization is registering and subscribing to the AngularPConnect service
54+
this.angularPConnectData = this.angularPConnect.registerAndSubscribeComponent(this, this.onStateChange);
55+
56+
this.updateSelf();
57+
}
58+
59+
onStateChange() {
60+
this.updateSelf();
61+
}
62+
63+
updateSelf() {
3564
const configProps = this.pConn$.getConfigProps() as DefaultFormProps;
3665
const kids = this.pConn$.getChildren();
3766
this.instructions = this.templateUtils.getInstructions(this.pConn$, configProps?.instructions);
@@ -55,6 +84,12 @@ export class DefaultFormComponent extends FormTemplateBase implements OnInit {
5584
// repoint children before getting templateArray
5685
// Children may contain 'reference' component, so we need to
5786
// normalize them
58-
this.arChildren$ = ReferenceComponent.normalizePConnArray(kids[0].getPConnect().getChildren());
87+
const children = ReferenceComponent.normalizePConnArray(kids[0].getPConnect().getChildren());
88+
89+
const visibleChildren = children.filter(child => child !== undefined);
90+
91+
if (areViewsChanged(this.arChildren$, visibleChildren)) {
92+
this.arChildren$ = visibleChildren;
93+
}
5994
}
6095
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class FieldGroupTemplateComponent implements OnInit, OnDestroy, OnChanges
117117
this.pConn$.setInheritedProp('displayMode', 'DISPLAY_ONLY');
118118
}
119119
this.referenceList = this.configProps$.referenceList;
120-
if (this.prevRefLength != this.referenceList.length) {
120+
if (this.prevRefLength != this.referenceList?.length) {
121121
// eslint-disable-next-line sonarjs/no-collapsible-if
122122
if (!this.readonlyMode) {
123123
if (this.referenceList?.length === 0 && this.allowAddEdit !== false) {

packages/angular-sdk-components/src/lib/_components/template/simple-table-manual/simple-table-manual.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable max-classes-per-file */
21
import { Component, OnInit, Input, ViewChild, forwardRef, OnDestroy } from '@angular/core';
32
import { CommonModule } from '@angular/common';
43
import { FormGroup } from '@angular/forms';

0 commit comments

Comments
 (0)