Skip to content

Commit

Permalink
feat(module:modal): add nzModalContent directive
Browse files Browse the repository at this point in the history
close #6065

DEPRECATED: Usage `<ng-content></ng-content>` is deprecated, use `<ng-template nzModalContent></ng-template>` to declare the contents of the modal.
  • Loading branch information
hsuanxyz committed Nov 30, 2020
1 parent 3204927 commit a78812c
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 46 deletions.
2 changes: 1 addition & 1 deletion components/modal/demo/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Component } from '@angular/core';
(nzOnOk)="handleOk()"
[nzOkLoading]="isOkLoading"
>
<p>Modal Content</p>
<p *nzModalContent>Modal Content</p>
</nz-modal>
`
})
Expand Down
9 changes: 5 additions & 4 deletions components/modal/demo/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { Component } from '@angular/core';
template: `
<button nz-button [nzType]="'primary'" (click)="showModal()"><span>Show Modal</span></button>
<nz-modal [(nzVisible)]="isVisible" nzTitle="The first Modal" (nzOnCancel)="handleCancel()" (nzOnOk)="handleOk()">
<p>Content one</p>
<p>Content two</p>
<p>Content three</p>
<p>Content three</p>
<ng-container *nzModalContent>
<p>Content one</p>
<p>Content two</p>
<p>Content three</p>
</ng-container>
</nz-modal>
`
})
Expand Down
4 changes: 2 additions & 2 deletions components/modal/demo/footer.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
order: 2
title:
zh-CN: 自定义页脚
en-US: Customized Footer
zh-CN: 自定义组成部分
en-US: Customized Parts
---

## zh-CN
Expand Down
8 changes: 3 additions & 5 deletions components/modal/demo/footer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import { Component } from '@angular/core';
[nzFooter]="modalFooter"
(nzOnCancel)="handleCancel()"
>
<ng-template #modalTitle>
Custom Modal Title
</ng-template>
<ng-template #modalTitle>Custom Modal Title</ng-template>
<ng-template #modalContent>
<p>Modal Content</p>
Expand All @@ -26,7 +24,7 @@ import { Component } from '@angular/core';
</ng-template>
<ng-template #modalFooter>
<span>Modal Footer: </span>
<span>Modal Footer:</span>
<button nz-button nzType="default" (click)="handleCancel()">Custom Callback</button>
<button nz-button nzType="primary" (click)="handleOk()" [nzLoading]="isConfirmLoading">Custom Submit</button>
</ng-template>
Expand All @@ -48,7 +46,7 @@ export class NzDemoModalFooterComponent {
setTimeout(() => {
this.isVisible = false;
this.isConfirmLoading = false;
}, 3000);
}, 1000);
}

handleCancel(): void {
Expand Down
4 changes: 2 additions & 2 deletions components/modal/demo/footer2.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
order: 3
title:
zh-CN: 自定义页脚(2)
en-US: Customized Footer(2)
zh-CN: 自定义页脚
en-US: Customized Footer
---

## zh-CN
Expand Down
4 changes: 2 additions & 2 deletions components/modal/demo/footer2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
<span>In Component</span>
</button>
<nz-modal [(nzVisible)]="isVisible" nzTitle="Custom Modal Title" (nzOnCancel)="handleCancel()">
<div>
<div *nzModalContent>
<p>Modal Content</p>
<p>Modal Content</p>
<p>Modal Content</p>
<p>Modal Content</p>
<p>Modal Content</p>
</div>
<div *nzModalFooter>
<span>Modal Footer: </span>
<span>Modal Footer:</span>
<button nz-button nzType="default" (click)="handleCancel()">Custom Callback</button>
<button nz-button nzType="primary" (click)="handleOk()" [nzLoading]="isConfirmLoading">Custom Submit</button>
</div>
Expand Down
8 changes: 5 additions & 3 deletions components/modal/demo/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import { NzModalService } from 'ng-zorro-antd/modal';
(nzOnOk)="handleOk()"
(nzOnCancel)="handleCancel()"
>
<p>Bla bla ...</p>
<p>Bla bla ...</p>
<p>Bla bla ...</p>
<ng-container *nzModalContent>
<p>Bla bla ...</p>
<p>Bla bla ...</p>
<p>Bla bla ...</p>
</ng-container>
</nz-modal>
</div>
<br />
Expand Down
19 changes: 12 additions & 7 deletions components/modal/demo/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import { Component } from '@angular/core';
(nzOnCancel)="handleCancelTop()"
(nzOnOk)="handleOkTop()"
>
<p>some contents...</p>
<p>some contents...</p>
<p>some contents...</p>
<ng-container *nzModalContent>
<p>some contents...</p>
<p>some contents...</p>
<p>some contents...</p>
</ng-container>
</nz-modal>
<br /><br />
<br />
<br />
<button nz-button nzType="primary" (click)="showModalMiddle()">Vertically centered modal dialog</button>
<nz-modal
Expand All @@ -26,9 +29,11 @@ import { Component } from '@angular/core';
(nzOnCancel)="handleCancelMiddle()"
(nzOnOk)="handleOkMiddle()"
>
<p>some contents...</p>
<p>some contents...</p>
<p>some contents...</p>
<ng-container *nzModalContent>
<p>some contents...</p>
<p>some contents...</p>
<p>some contents...</p>
</ng-container>
</nz-modal>
`,
styles: [
Expand Down
6 changes: 0 additions & 6 deletions components/modal/demo/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
<br />
<button nz-button nzType="primary" (click)="openAndCloseAll()">Open more modals then close all after 2s</button>
<nz-modal [(nzVisible)]="htmlModalVisible" nzMask="false" [nzZIndex]="1001" nzTitle="Non-service html modal">
This is a non-service html modal
</nz-modal>
`,
styles: [
`
Expand All @@ -53,7 +50,6 @@ import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
})
export class NzDemoModalServiceComponent {
tplModalButtonLoading = false;
htmlModalVisible = false;
disabled = false;

constructor(private modal: NzModalService, private viewContainerRef: ViewContainerRef) {}
Expand Down Expand Up @@ -170,8 +166,6 @@ export class NzDemoModalServiceComponent {
})
);

this.htmlModalVisible = true;

this.modal.afterAllClose.subscribe(() => console.log('afterAllClose emitted!'));

setTimeout(() => this.modal.closeAll(), 2000);
Expand Down
2 changes: 1 addition & 1 deletion components/modal/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ The above configuration items can also be changed in real-time to trigger the bu
### [nzModalFooter]
Another way to customize the footer button.
Customize the footer.
```html
<div *nzModalFooter>
Expand Down
2 changes: 1 addition & 1 deletion components/modal/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ nzFooter: [{
### [nzModalFooter]
另一种自定义页脚按钮的方式
自定义页脚
```html
<div *nzModalFooter>
Expand Down
14 changes: 14 additions & 0 deletions components/modal/modal-content.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* 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 { Directive, TemplateRef } from '@angular/core';

@Directive({
selector: '[nzModalContent]',
exportAs: 'nzModalContent'
})
export class NzModalContentDirective {
constructor(public templateRef: TemplateRef<{}>) {}
}
13 changes: 7 additions & 6 deletions components/modal/modal-footer.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Component, TemplateRef, ViewChild } from '@angular/core';
import { ComponentFixture, fakeAsync, flush, inject, TestBed, waitForAsync } from '@angular/core/testing';

import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { NzSafeAny } from 'ng-zorro-antd/core/types';

import { NzModalFooterDirective } from './modal-footer.directive';
import { NzModalRef } from './modal-ref';
Expand Down Expand Up @@ -48,7 +49,7 @@ describe('modal footer directive', () => {
fixture.detectChanges();
expect(testComponent.isVisible).toBe(true);
const modalRef = testComponent.nzModalComponent.getModalRef();
expect(modalRef!.getConfig().nzFooter).toBe(testComponent.nzModalFooterDirective.templateRef);
expect(modalRef!.getConfig().nzFooter).toEqual(testComponent.nzModalFooterDirective);

testComponent.handleCancel();
fixture.detectChanges();
Expand All @@ -63,7 +64,7 @@ describe('modal footer directive', () => {
initOpenedComponentFixture.detectChanges();
const modalRef = initOpenedComponent.nzModalComponent.getModalRef();

expect(modalRef!.getConfig().nzFooter).toBe(initOpenedComponent.nzModalFooterDirective.templateRef);
expect(modalRef!.getConfig().nzFooter).toEqual(initOpenedComponent.nzModalFooterDirective);

initOpenedComponentFixture.detectChanges();
}));
Expand All @@ -73,7 +74,7 @@ describe('modal footer directive', () => {
fixture.detectChanges();

expect(modalRef.componentInstance!.nzModalRef).toBe(modalRef);
expect(modalRef.componentInstance!.nzModalFooterDirective.templateRef).toBe(modalRef.getConfig().nzFooter as TemplateRef<{}>);
expect(modalRef.componentInstance!.nzModalFooterDirective).toEqual(modalRef.getConfig().nzFooter as TemplateRef<{}>);
});
});

Expand All @@ -92,7 +93,7 @@ describe('modal footer directive', () => {
class TestDirectiveFooterComponent {
isVisible = false;
@ViewChild(NzModalComponent) nzModalComponent!: NzModalComponent;
@ViewChild(NzModalFooterDirective) nzModalFooterDirective!: NzModalFooterDirective;
@ViewChild(NzModalFooterDirective, { static: true, read: TemplateRef }) nzModalFooterDirective!: TemplateRef<NzSafeAny>;

constructor() {}

Expand Down Expand Up @@ -120,7 +121,7 @@ class TestDirectiveFooterComponent {
class TestDirectiveFooterWithInitOpenedComponent {
isVisible = true;
@ViewChild(NzModalComponent) nzModalComponent!: NzModalComponent;
@ViewChild(NzModalFooterDirective) nzModalFooterDirective!: NzModalFooterDirective;
@ViewChild(NzModalFooterDirective, { static: true, read: TemplateRef }) nzModalFooterDirective!: TemplateRef<NzSafeAny>;

constructor() {}
}
Expand All @@ -134,7 +135,7 @@ class TestDirectiveFooterWithInitOpenedComponent {
`
})
class TestDirectiveFooterInServiceComponent {
@ViewChild(NzModalFooterDirective) nzModalFooterDirective!: NzModalFooterDirective;
@ViewChild(NzModalFooterDirective, { static: true, read: TemplateRef }) nzModalFooterDirective!: TemplateRef<NzSafeAny>;

constructor(public nzModalRef: NzModalRef) {}

Expand Down
19 changes: 14 additions & 5 deletions components/modal/modal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import {
} from '@angular/core';

import { NzButtonType } from 'ng-zorro-antd/button';
import { warnDeprecation } from 'ng-zorro-antd/core/logger';
import { BooleanInput, NzSafeAny } from 'ng-zorro-antd/core/types';
import { InputBoolean } from 'ng-zorro-antd/core/util';
import { Observable } from 'rxjs';

import { NzModalContentDirective } from './modal-content.directive';
import { NzModalFooterDirective } from './modal-footer.directive';
import { NzModalLegacyAPI } from './modal-legacy-api';
import { NzModalRef } from './modal-ref';
Expand Down Expand Up @@ -104,12 +106,14 @@ export class NzModalComponent<T = NzSafeAny, R = NzSafeAny> implements OnChanges
@Output() readonly nzVisibleChange = new EventEmitter<boolean>();

@ViewChild(TemplateRef, { static: true }) contentTemplateRef!: TemplateRef<{}>;
@ContentChild(NzModalFooterDirective)
set modalFooter(value: NzModalFooterDirective) {
if (value && value.templateRef) {
this.setFooterWithTemplate(value.templateRef);
@ContentChild(NzModalContentDirective, { static: true, read: TemplateRef }) contentFromContentChild!: TemplateRef<NzSafeAny>;
@ContentChild(NzModalFooterDirective, { static: true, read: TemplateRef })
set modalFooter(value: TemplateRef<NzSafeAny>) {
if (value) {
this.setFooterWithTemplate(value);
}
}

private modalRef: NzModalRef | null = null;

get afterOpen(): Observable<void> {
Expand Down Expand Up @@ -189,8 +193,13 @@ export class NzModalComponent<T = NzSafeAny, R = NzSafeAny> implements OnChanges
private getConfig(): ModalOptions {
const componentConfig = getConfigFromComponent(this);
componentConfig.nzViewContainerRef = this.viewContainerRef;
if (!this.nzContent) {
if (!this.nzContent && !this.contentFromContentChild) {
componentConfig.nzContent = this.contentTemplateRef;
warnDeprecation(
'Usage `<ng-content></ng-content>` is deprecated, which will be removed in 12.0.0. Please instead use `<ng-template nzModalContent></ng-template>` to declare the content of the modal.'
);
} else {
componentConfig.nzContent = this.nzContent || this.contentFromContentChild;
}
return componentConfig;
}
Expand Down
4 changes: 3 additions & 1 deletion components/modal/modal.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { NzPipesModule } from 'ng-zorro-antd/pipes';
import { NzModalCloseComponent } from './modal-close.component';
import { NzModalConfirmContainerComponent } from './modal-confirm-container.component';
import { NzModalContainerComponent } from './modal-container.component';
import { NzModalContentDirective } from './modal-content.directive';
import { NzModalFooterComponent } from './modal-footer.component';
import { NzModalFooterDirective } from './modal-footer.directive';
import { NzModalTitleComponent } from './modal-title.component';
Expand All @@ -37,12 +38,13 @@ import { NzModalService } from './modal.service';
NzNoAnimationModule,
NzPipesModule
],
exports: [NzModalComponent, NzModalFooterDirective],
exports: [NzModalComponent, NzModalFooterDirective, NzModalContentDirective],
providers: [NzModalService],
entryComponents: [NzModalContainerComponent, NzModalConfirmContainerComponent],
declarations: [
NzModalComponent,
NzModalFooterDirective,
NzModalContentDirective,
NzModalCloseComponent,
NzModalFooterComponent,
NzModalTitleComponent,
Expand Down
1 change: 1 addition & 0 deletions components/modal/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './modal-ref';
export * from './modal-config';
export * from './modal.component';
export * from './modal-footer.directive';
export * from './modal-content.directive';
export * from './modal.module';
export * from './modal-confirm-container.component';
export * from './modal-container.component';
Expand Down

0 comments on commit a78812c

Please sign in to comment.