/
array.widget.ts
133 lines (123 loc) · 4.4 KB
/
array.widget.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { SafeHtml } from '@angular/platform-browser';
import { NzButtonType } from 'ng-zorro-antd/button';
import { SFUpdateValueAndValidity } from '../../interface';
import type { FormProperty } from '../../model/form.property';
import { ArrayLayoutWidget } from '../../widget';
@Component({
selector: 'sf-array',
template: `<nz-form-item [class.ant-form-item-with-help]="showError">
@if (schema.title) {
<div nz-col [nzSpan]="ui.spanLabel!" class="ant-form-item-label">
<label [class.ant-form-item-required]="ui.required">
{{ schema.title }}
<span class="sf__optional">
{{ ui.optional }}
@if (oh) {
<i
nz-tooltip
[nzTooltipTitle]="oh.text"
[nzTooltipPlacement]="oh.placement"
[nzTooltipTrigger]="oh.trigger"
[nzTooltipOverlayClassName]="oh.overlayClassName"
[nzTooltipOverlayStyle]="oh.overlayStyle"
[nzTooltipMouseEnterDelay]="oh.mouseEnterDelay"
[nzTooltipMouseLeaveDelay]="oh.mouseLeaveDelay"
nz-icon
[nzType]="oh.icon!"
></i>
}
</span>
</label>
<div class="sf__array-add">
<button
type="button"
nz-button
[nzType]="addType"
[disabled]="addDisabled"
(click)="addItem()"
[innerHTML]="addTitle"
></button>
</div>
</div>
}
<div nz-col class="ant-form-item-control-wrapper" [nzSpan]="ui.spanControl!" [nzOffset]="ui.offsetControl!">
<div class="ant-form-item-control" [class.has-error]="showError">
<div nz-row class="sf__array-container">
<ng-container *ngFor="let i of $any(formProperty).properties; let $index = index">
@if (i.visible && !i.ui.hidden) {
<div nz-col [nzSpan]="arraySpan" [attr.data-index]="$index" class="sf__array-item">
<nz-card>
<sf-item [formProperty]="i" />
@if (showRemove) {
<span class="sf__array-remove" (click)="removeItem($index)" [attr.title]="removeTitle">
<i nz-icon nzType="delete"></i>
</span>
}
</nz-card>
</div>
}
</ng-container>
</div>
@if (!ui.onlyVisual && showError) {
<div class="ant-form-explain">{{ error }}</div>
}
@if (schema.description) {
<div [innerHTML]="ui._description" class="ant-form-extra"></div>
}
</div>
</div>
</nz-form-item>`,
host: { '[class.sf__array]': 'true' },
preserveWhitespaces: false,
encapsulation: ViewEncapsulation.None
})
export class ArrayWidget extends ArrayLayoutWidget implements OnInit {
addTitle!: SafeHtml;
addType!: NzButtonType;
removeTitle?: string | null;
arraySpan = 8;
get addDisabled(): boolean {
return (
this.disabled ||
(this.schema.maxItems != null && (this.formProperty.properties as FormProperty[]).length >= this.schema.maxItems!)
);
}
get showRemove(): boolean {
if (this.disabled || !this.removeTitle) return false;
if (
this.schema.minItems != null &&
(this.formProperty.properties as FormProperty[]).length <= this.schema.minItems!
)
return false;
return true;
}
ngOnInit(): void {
const { grid, addTitle, addType, removable, removeTitle } = this.ui;
if (grid && grid.arraySpan) {
this.arraySpan = grid.arraySpan;
}
this.addTitle = this.dom.bypassSecurityTrustHtml(addTitle || this.l.addText);
this.addType = addType || 'dashed';
this.removeTitle = removable === false ? null : removeTitle || this.l.removeText;
}
private reValid(options?: SFUpdateValueAndValidity): void {
this.formProperty.updateValueAndValidity({
onlySelf: false,
emitValueEvent: false,
emitValidator: true,
...options
});
}
addItem(): void {
const property = this.formProperty.add({});
this.reValid();
this.ui.add?.(property);
}
removeItem(index: number): void {
const updatePath = (this.formProperty.properties as FormProperty[])[index].path;
this.formProperty.remove(index);
this.reValid({ updatePath, emitValueEvent: true });
this.ui.remove?.(index);
}
}