-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
modal-footer.component.ts
156 lines (145 loc) · 4.79 KB
/
modal-footer.component.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnDestroy, Output } from '@angular/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzOutletModule } from 'ng-zorro-antd/core/outlet';
import { isPromise } from 'ng-zorro-antd/core/util';
import { NzI18nService, NzModalI18nInterface } from 'ng-zorro-antd/i18n';
import { NzModalRef } from './modal-ref';
import { ModalButtonOptions, ModalOptions } from './modal-types';
@Component({
selector: 'div[nz-modal-footer]',
exportAs: 'NzModalFooterBuiltin',
template: `
@if (config.nzFooter) {
<ng-container
*nzStringTemplateOutlet="config.nzFooter; context: { $implicit: config.nzData, modalRef: modalRef }"
>
@if (buttonsFooter) {
@for (button of buttons; track button) {
<button
nz-button
(click)="onButtonClick(button)"
[hidden]="!getButtonCallableProp(button, 'show')"
[nzLoading]="getButtonCallableProp(button, 'loading')"
[disabled]="getButtonCallableProp(button, 'disabled')"
[nzType]="button.type!"
[nzDanger]="button.danger"
[nzShape]="button.shape!"
[nzSize]="button.size!"
[nzGhost]="button.ghost!"
>
{{ button.label }}
</button>
}
} @else {
<div [innerHTML]="config.nzFooter"></div>
}
</ng-container>
} @else {
@if (config.nzCancelText !== null) {
<button
[attr.cdkFocusInitial]="config.nzAutofocus === 'cancel' || null"
nz-button
(click)="onCancel()"
[nzLoading]="config.nzCancelLoading"
[disabled]="config.nzCancelDisabled"
>
{{ config.nzCancelText || locale.cancelText }}
</button>
}
@if (config.nzOkText !== null) {
<button
[attr.cdkFocusInitial]="config.nzAutofocus === 'ok' || null"
nz-button
[nzType]="config.nzOkType!"
[nzDanger]="config.nzOkDanger"
(click)="onOk()"
[nzLoading]="config.nzOkLoading"
[disabled]="config.nzOkDisabled"
>
{{ config.nzOkText || locale.okText }}
</button>
}
}
`,
host: {
class: 'ant-modal-footer'
},
changeDetection: ChangeDetectionStrategy.Default,
imports: [NzOutletModule, NzButtonModule],
standalone: true
})
export class NzModalFooterComponent implements OnDestroy {
buttonsFooter = false;
buttons: ModalButtonOptions[] = [];
locale!: NzModalI18nInterface;
@Output() readonly cancelTriggered = new EventEmitter<void>();
@Output() readonly okTriggered = new EventEmitter<void>();
@Input() modalRef!: NzModalRef;
private destroy$ = new Subject<void>();
constructor(
private i18n: NzI18nService,
public config: ModalOptions
) {
if (Array.isArray(config.nzFooter)) {
this.buttonsFooter = true;
this.buttons = (config.nzFooter as ModalButtonOptions[]).map(mergeDefaultOption);
}
this.i18n.localeChange.pipe(takeUntil(this.destroy$)).subscribe(() => {
this.locale = this.i18n.getLocaleData('Modal');
});
}
onCancel(): void {
this.cancelTriggered.emit();
}
onOk(): void {
this.okTriggered.emit();
}
/**
* Returns the value of the specified key.
* If it is a function, run and return the return value of the function.
*/
getButtonCallableProp(options: ModalButtonOptions, prop: keyof ModalButtonOptions): boolean {
const value = options[prop];
const componentInstance = this.modalRef.getContentComponent();
return typeof value === 'function' ? value.apply(options, componentInstance && [componentInstance]) : value;
}
/**
* Run function based on the type and set its `loading` prop if needed.
*/
onButtonClick(options: ModalButtonOptions): void {
const loading = this.getButtonCallableProp(options, 'loading');
if (!loading) {
const result = this.getButtonCallableProp(options, 'onClick');
if (options.autoLoading && isPromise(result)) {
options.loading = true;
result
.then(() => (options.loading = false))
.catch(e => {
options.loading = false;
throw e;
});
}
}
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
}
function mergeDefaultOption(options: ModalButtonOptions): ModalButtonOptions {
return {
type: null,
size: 'default',
autoLoading: true,
show: true,
loading: false,
disabled: false,
...options
};
}