Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(module:drawer): add nzDrawerContent directive #6085

Merged
merged 1 commit into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions components/drawer/demo/basic-right.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { Component } from '@angular/core';
template: `
<button nz-button nzType="primary" (click)="open()">Open</button>
<nz-drawer [nzClosable]="false" [nzVisible]="visible" nzPlacement="right" nzTitle="Basic Drawer" (nzOnClose)="close()">
<p>Some contents...</p>
<p>Some contents...</p>
<p>Some contents...</p>
<ng-container *nzDrawerContent>
<p>Some contents...</p>
<p>Some contents...</p>
<p>Some contents...</p>
</ng-container>
</nz-drawer>
`
})
Expand Down
16 changes: 8 additions & 8 deletions components/drawer/demo/from-drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Component } from '@angular/core';
[nzFooter]="footerTpl"
(nzOnClose)="close()"
>
<form nz-form>
<form nz-form *nzDrawerContent>
<div nz-row [nzGutter]="8">
<div nz-col nzSpan="12">
<nz-form-item>
Expand Down Expand Up @@ -80,14 +80,14 @@ import { Component } from '@angular/core';
</nz-form-item>
</div>
</div>

<ng-template #footerTpl>
<div style="float: right">
<button nz-button style="margin-right: 8px;" (click)="close()">Cancel</button>
<button nz-button nzType="primary" (click)="close()">Submit</button>
</div>
</ng-template>
</form>

<ng-template #footerTpl>
<div style="float: right">
<button nz-button style="margin-right: 8px;" (click)="close()">Cancel</button>
<button nz-button nzType="primary" (click)="close()">Submit</button>
</div>
</ng-template>
</nz-drawer>
`
})
Expand Down
4 changes: 2 additions & 2 deletions components/drawer/demo/multi-level-drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Component } from '@angular/core';
nzTitle="Cookbook"
(nzOnClose)="close()"
>
<form nz-form>
<form *nzDrawerContent nz-form>
<div nz-row>
<div nz-col nzSpan="24">
<nz-form-item>
Expand All @@ -37,7 +37,7 @@ import { Component } from '@angular/core';
</div>
</form>
<nz-drawer [nzClosable]="false" [nzVisible]="childrenVisible" nzTitle="Food" (nzOnClose)="closeChildren()">
<nz-list [nzDataSource]="vegetables" [nzRenderItem]="item">
<nz-list *nzDrawerContent [nzDataSource]="vegetables" [nzRenderItem]="item">
<ng-template #item let-item>
<nz-list-item [nzContent]="item"></nz-list-item>
</ng-template>
Expand Down
8 changes: 5 additions & 3 deletions components/drawer/demo/placement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import { NzDrawerPlacement } from 'ng-zorro-antd/drawer';
</nz-radio-group>
<button nz-button nzType="primary" (click)="open()">Open</button>
<nz-drawer [nzClosable]="false" [nzVisible]="visible" [nzPlacement]="placement" nzTitle="Basic Drawer" (nzOnClose)="close()">
<p>Some contents...</p>
<p>Some contents...</p>
<p>Some contents...</p>
<ng-container *nzDrawerContent>
<p>Some contents...</p>
<p>Some contents...</p>
<p>Some contents...</p>
</ng-container>
</nz-drawer>
`
})
Expand Down
62 changes: 31 additions & 31 deletions components/drawer/demo/user-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,37 @@ import { Component } from '@angular/core';
</ng-template>
</nz-list>
<nz-drawer [nzVisible]="visible" [nzWidth]="640" [nzClosable]="false" (nzOnClose)="close()">
<p class="title" style=" margin-bottom: 24px;">User Profile</p>
<nz-descriptions [nzColumn]="2" nzTitle="Personal">
<nz-descriptions-item nzTitle="Full Name" [nzSpan]="1">Lily</nz-descriptions-item>
<nz-descriptions-item nzTitle="Account" [nzSpan]="1">AntDesign@example.com</nz-descriptions-item>
<nz-descriptions-item nzTitle="City" [nzSpan]="1">HangZhou</nz-descriptions-item>
<nz-descriptions-item nzTitle="Country" [nzSpan]="1">China🇨🇳</nz-descriptions-item>
<nz-descriptions-item nzTitle="Birthday" [nzSpan]="1">February 2,1900</nz-descriptions-item>
<nz-descriptions-item nzTitle="Website" [nzSpan]="1"> - </nz-descriptions-item>
<nz-descriptions-item nzTitle="Message" [nzSpan]="2">
Make things as simple as possible but no simpler.
</nz-descriptions-item>
</nz-descriptions>
<nz-divider></nz-divider>
<nz-descriptions [nzColumn]="2" nzTitle="Company">
<nz-descriptions-item nzTitle="Position" [nzSpan]="1">Programmer</nz-descriptions-item>
<nz-descriptions-item nzTitle="Responsibilities" [nzSpan]="1">Coding</nz-descriptions-item>
<nz-descriptions-item nzTitle="Department" [nzSpan]="1">AFX</nz-descriptions-item>
<nz-descriptions-item nzTitle="Supervisor" [nzSpan]="1">Lin</nz-descriptions-item>
<nz-descriptions-item nzTitle="Skills" [nzSpan]="2">
C / C + +, data structures, software engineering, operating systems, computer networks, databases, compiler theory, computer
architecture, Microcomputer Principle and Interface Technology, Computer English, Java, ASP, etc.
</nz-descriptions-item>
</nz-descriptions>
<nz-divider></nz-divider>
<nz-descriptions [nzColumn]="2" nzTitle="Contacts">
<nz-descriptions-item nzTitle="Email" [nzSpan]="1">AntDesign@example.com</nz-descriptions-item>
<nz-descriptions-item nzTitle="Phone Number" [nzSpan]="1">+86 181 0000 0000</nz-descriptions-item>
<nz-descriptions-item nzTitle="Github" [nzSpan]="2">
<a href="https://github.com/NG-ZORRO/ng-zorro-antd" target="_blank">github.com/NG-ZORRO/ng-zorro-antd</a>
</nz-descriptions-item>
</nz-descriptions>
<ng-container *nzDrawerContent>
<p class="title" style=" margin-bottom: 24px;">User Profile</p>
<nz-descriptions [nzColumn]="2" nzTitle="Personal">
<nz-descriptions-item nzTitle="Full Name" [nzSpan]="1">Lily</nz-descriptions-item>
<nz-descriptions-item nzTitle="Account" [nzSpan]="1">AntDesign@example.com</nz-descriptions-item>
<nz-descriptions-item nzTitle="City" [nzSpan]="1">HangZhou</nz-descriptions-item>
<nz-descriptions-item nzTitle="Country" [nzSpan]="1">China🇨🇳</nz-descriptions-item>
<nz-descriptions-item nzTitle="Birthday" [nzSpan]="1">February 2,1900</nz-descriptions-item>
<nz-descriptions-item nzTitle="Website" [nzSpan]="1">-</nz-descriptions-item>
<nz-descriptions-item nzTitle="Message" [nzSpan]="2">Make things as simple as possible but no simpler.</nz-descriptions-item>
</nz-descriptions>
<nz-divider></nz-divider>
<nz-descriptions [nzColumn]="2" nzTitle="Company">
<nz-descriptions-item nzTitle="Position" [nzSpan]="1">Programmer</nz-descriptions-item>
<nz-descriptions-item nzTitle="Responsibilities" [nzSpan]="1">Coding</nz-descriptions-item>
<nz-descriptions-item nzTitle="Department" [nzSpan]="1">AFX</nz-descriptions-item>
<nz-descriptions-item nzTitle="Supervisor" [nzSpan]="1">Lin</nz-descriptions-item>
<nz-descriptions-item nzTitle="Skills" [nzSpan]="2">
C / C + +, data structures, software engineering, operating systems, computer networks, databases, compiler theory, computer
architecture, Microcomputer Principle and Interface Technology, Computer English, Java, ASP, etc.
</nz-descriptions-item>
</nz-descriptions>
<nz-divider></nz-divider>
<nz-descriptions [nzColumn]="2" nzTitle="Contacts">
<nz-descriptions-item nzTitle="Email" [nzSpan]="1">AntDesign@example.com</nz-descriptions-item>
<nz-descriptions-item nzTitle="Phone Number" [nzSpan]="1">+86 181 0000 0000</nz-descriptions-item>
<nz-descriptions-item nzTitle="Github" [nzSpan]="2">
<a href="https://github.com/NG-ZORRO/ng-zorro-antd" target="_blank">github.com/NG-ZORRO/ng-zorro-antd</a>
</nz-descriptions-item>
</nz-descriptions>
</ng-container>
</nz-drawer>
`,
styles: [
Expand Down
15 changes: 15 additions & 0 deletions components/drawer/drawer-content.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* 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';
import { NzSafeAny } from 'ng-zorro-antd/core/types';

@Directive({
selector: '[nzDrawerContent]',
exportAs: 'nzDrawerContent'
})
export class NzDrawerContentDirective {
constructor(public templateRef: TemplateRef<NzSafeAny>) {}
}
34 changes: 28 additions & 6 deletions components/drawer/drawer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { Overlay, OverlayConfig, OverlayKeyboardDispatcher, OverlayRef } from '@
import { CdkPortalOutlet, ComponentPortal, TemplatePortal } from '@angular/cdk/portal';
import { DOCUMENT } from '@angular/common';
import {
AfterContentInit,
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ContentChild,
EventEmitter,
Inject,
Injector,
Expand All @@ -29,12 +31,16 @@ import {
ViewChild,
ViewContainerRef
} from '@angular/core';

import { Observable, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
import { warnDeprecation } from 'ng-zorro-antd/core/logger';
import { BooleanInput, NgStyleInterface, NzSafeAny } from 'ng-zorro-antd/core/types';
import { InputBoolean, toCssPixel } from 'ng-zorro-antd/core/util';

import { Observable, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { NzDrawerContentDirective } from './drawer-content.directive';
import { NzDrawerOptionsOfComponent, NzDrawerPlacement } from './drawer-options';
import { NzDrawerRef } from './drawer-ref';

Expand Down Expand Up @@ -82,10 +88,17 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'drawer';
</div>
<div class="ant-drawer-body" [ngStyle]="nzBodyStyle">
<ng-template cdkPortalOutlet></ng-template>
<ng-container *ngIf="isTemplateRef(nzContent)">
<ng-container *ngTemplateOutlet="$any(nzContent); context: templateContext"></ng-container>
<ng-container *ngIf="nzContent; else contentElseTemp">
<ng-container *ngIf="isTemplateRef(nzContent)">
<ng-container *ngTemplateOutlet="$any(nzContent); context: templateContext"></ng-container>
</ng-container>
</ng-container>
<ng-content *ngIf="!nzContent"></ng-content>
<ng-template #contentElseTemp>
<ng-container *ngIf="contentFromContentChild">
<ng-template [ngTemplateOutlet]="contentFromContentChild"></ng-template>
</ng-container>
</ng-template>
<ng-content *ngIf="!(nzContent || contentFromContentChild)"></ng-content>
</div>
<div *ngIf="nzFooter" class="ant-drawer-footer">
<ng-container *nzStringTemplateOutlet="nzFooter"><div [innerHTML]="nzFooter"></div></ng-container>
Expand All @@ -101,7 +114,7 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'drawer';
})
export class NzDrawerComponent<T = NzSafeAny, R = NzSafeAny, D = NzSafeAny>
extends NzDrawerRef<T, R>
implements OnInit, OnDestroy, AfterViewInit, OnChanges, NzDrawerOptionsOfComponent {
implements OnInit, OnDestroy, AfterViewInit, OnChanges, AfterContentInit, NzDrawerOptionsOfComponent {
readonly _nzModuleName: NzConfigKey = NZ_CONFIG_MODULE_NAME;
static ngAcceptInputType_nzClosable: BooleanInput;
static ngAcceptInputType_nzMaskClosable: BooleanInput;
Expand Down Expand Up @@ -145,6 +158,7 @@ export class NzDrawerComponent<T = NzSafeAny, R = NzSafeAny, D = NzSafeAny>

@ViewChild('drawerTemplate', { static: true }) drawerTemplate!: TemplateRef<void>;
@ViewChild(CdkPortalOutlet, { static: false }) bodyPortalOutlet?: CdkPortalOutlet;
@ContentChild(NzDrawerContentDirective, { static: true, read: TemplateRef }) contentFromContentChild?: TemplateRef<NzSafeAny>;

destroy$ = new Subject<void>();
previouslyFocusedElement?: HTMLElement;
Expand Down Expand Up @@ -249,6 +263,14 @@ export class NzDrawerComponent<T = NzSafeAny, R = NzSafeAny, D = NzSafeAny>
});
}

ngAfterContentInit(): void {
if (!(this.contentFromContentChild || this.nzContent)) {
warnDeprecation(
'Usage `<ng-content></ng-content>` is deprecated, which will be removed in 12.0.0. Please instead use `<ng-template nzDrawerContent></ng-template>` to declare the content of the drawer.'
);
}
}

ngOnChanges(changes: SimpleChanges): void {
const { nzPlacement, nzVisible } = changes;
if (nzVisible) {
Expand Down
5 changes: 3 additions & 2 deletions components/drawer/drawer.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import { NzOutletModule } from 'ng-zorro-antd/core/outlet';

import { NzIconModule } from 'ng-zorro-antd/icon';

import { NzDrawerContentDirective } from './drawer-content.directive';
import { NzDrawerComponent } from './drawer.component';
import { NzDrawerServiceModule } from './drawer.service.module';

@NgModule({
imports: [CommonModule, OverlayModule, PortalModule, NzIconModule, NzOutletModule, NzNoAnimationModule, NzDrawerServiceModule],
exports: [NzDrawerComponent],
declarations: [NzDrawerComponent],
exports: [NzDrawerComponent, NzDrawerContentDirective],
declarations: [NzDrawerComponent, NzDrawerContentDirective],
entryComponents: [NzDrawerComponent]
})
export class NzDrawerModule {}
1 change: 1 addition & 0 deletions components/drawer/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

export * from './drawer.component';
export * from './drawer-content.directive';
export * from './drawer.module';
export * from './drawer.service';
export * from './drawer.service.module';
Expand Down