|
16 | 16 | * specific language governing permissions and limitations |
17 | 17 | * under the License. |
18 | 18 | */ |
| 19 | +import { NgFor } from "@angular/common"; |
19 | 20 | import { Component, OnInit } from "@angular/core"; |
20 | | -import { NgFor, NgIf } from "@angular/common"; |
21 | 21 | import { FieldArrayType, FormlyFieldConfig, FormlyModule } from "@ngx-formly/core"; |
22 | 22 |
|
| 23 | +const COLUMN_KEYS = ["value", "attributeName", "attributeType"] as const; |
| 24 | + |
23 | 25 | @Component({ |
24 | 26 | selector: "texera-ui-udf-parameters", |
25 | | - templateUrl: "./ui-udf-parameters.component.html", |
26 | | - styleUrls: ["./ui-udf-parameters.component.scss"], |
27 | | - imports: [NgIf, NgFor, FormlyModule], |
| 27 | + template: |
| 28 | + '<div class="ui-udf-param-row" *ngFor="let rowField of field.fieldGroup"><formly-field *ngFor="let childField of displayFields(rowField)" [field]="childField"></formly-field></div>', |
| 29 | + styles: [ |
| 30 | + ".ui-udf-param-row{display:grid;grid-template-columns:minmax(160px,250px) minmax(160px,250px) minmax(120px,1fr);gap:12px}:host ::ng-deep .ant-form-item{margin-bottom:0}:host ::ng-deep .ant-form-item-label{display:none}", |
| 31 | + ], |
| 32 | + imports: [NgFor, FormlyModule], |
28 | 33 | }) |
29 | 34 | export class UiUdfParametersComponent extends FieldArrayType implements OnInit { |
30 | 35 | ngOnInit(): void { |
31 | | - this.field.fieldGroup?.forEach(rowField => { |
32 | | - this.configureDisabledState(this.getAttributeChild(rowField, "attributeName"), true); |
33 | | - this.configureDisabledState(this.getAttributeChild(rowField, "attributeType"), true); |
34 | | - this.configureDisabledState(this.getField(rowField, "value"), false); |
35 | | - }); |
| 36 | + this.field.fieldGroup?.forEach(rowField => |
| 37 | + COLUMN_KEYS.forEach(key => this.setDisabled(this.child(rowField, key), key !== "value")) |
| 38 | + ); |
36 | 39 | } |
37 | 40 |
|
38 | | - private getField(rowField: FormlyFieldConfig, key: string): FormlyFieldConfig | undefined { |
39 | | - return rowField.fieldGroup?.find(f => f.key === key); |
| 41 | + displayFields(rowField: FormlyFieldConfig): FormlyFieldConfig[] { |
| 42 | + return COLUMN_KEYS.map(key => this.child(rowField, key)).filter((field): field is FormlyFieldConfig => !!field); |
40 | 43 | } |
41 | 44 |
|
42 | | - private getAttributeChild(rowField: FormlyFieldConfig, childKey: string): FormlyFieldConfig | undefined { |
43 | | - const attributeGroup = this.getField(rowField, "attribute"); |
44 | | - return attributeGroup?.fieldGroup?.find(f => f.key === childKey); |
| 45 | + private child(rowField: FormlyFieldConfig, key: string): FormlyFieldConfig | undefined { |
| 46 | + if (key === "value") return rowField.fieldGroup?.find(field => field.key === key); |
| 47 | + return rowField.fieldGroup?.find(field => field.key === "attribute")?.fieldGroup?.find(field => field.key === key); |
45 | 48 | } |
46 | 49 |
|
47 | | - private configureDisabledState(field: FormlyFieldConfig | undefined, disabled: boolean): void { |
| 50 | + private setDisabled(field: FormlyFieldConfig | undefined, disabled: boolean): void { |
48 | 51 | if (!field) return; |
49 | | - |
50 | 52 | field.props = { ...(field.props ?? {}), disabled }; |
51 | | - |
52 | | - // (`as any` so we don't get nagged by the @deprecated JSDoc) |
53 | | - (field as any).templateOptions = { ...((field as any).templateOptions ?? {}), disabled }; |
54 | | - |
55 | | - const prevOnInit = field.hooks?.onInit; |
56 | | - field.hooks = { |
57 | | - ...(field.hooks ?? {}), |
58 | | - onInit: f => { |
59 | | - prevOnInit?.(f); |
60 | | - this.applyDisabledState(f, disabled); |
61 | | - }, |
62 | | - }; |
63 | | - |
64 | | - this.applyDisabledState(field, disabled); |
65 | | - } |
66 | | - |
67 | | - private applyDisabledState(field: FormlyFieldConfig, disabled: boolean): void { |
68 | | - if (field.formControl) { |
69 | | - if (disabled) { |
70 | | - field.formControl.disable({ emitEvent: false }); |
71 | | - } else { |
72 | | - field.formControl.enable({ emitEvent: false }); |
73 | | - } |
74 | | - } |
| 53 | + field.formControl?.[disabled ? "disable" : "enable"]({ emitEvent: false }); |
75 | 54 | } |
76 | | - |
77 | | - // Disable Name |
78 | | - getNameField(rowField: FormlyFieldConfig): FormlyFieldConfig | undefined { |
79 | | - return this.getAttributeChild(rowField, "attributeName"); |
80 | | - } |
81 | | - |
82 | | - // Disable Type |
83 | | - getTypeField(rowField: FormlyFieldConfig): FormlyFieldConfig | undefined { |
84 | | - return this.getAttributeChild(rowField, "attributeType"); |
85 | | - } |
86 | | - |
87 | | - // Value editable |
88 | | - getValueField(rowField: FormlyFieldConfig): FormlyFieldConfig | undefined { |
89 | | - return this.getField(rowField, "value"); |
90 | | - } |
91 | | - |
92 | | - trackByParamName = (index: number, param: any): string | number => { |
93 | | - return param?.attribute?.attributeName ?? index; |
94 | | - }; |
95 | 55 | } |
0 commit comments