Skip to content

Commit

Permalink
feat(module:drawer): support get component instance for content (NG-Z…
Browse files Browse the repository at this point in the history
  • Loading branch information
hsuanxyz authored Jul 9, 2020
1 parent ca70136 commit be2f917
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 21 deletions.
4 changes: 2 additions & 2 deletions components/drawer/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { NzDrawerModule } from 'ng-zorro-antd/drawer';

| Method | Description | Params | Return |
| --- | --- | --- | --- |
| create | create and open an Drawer | `NzDrawerOptions<T, D, R>`| `NzDrawerRef<R>` |
| create<T, D, R> | create and open an Drawer | `NzDrawerOptions<T, D>`| `NzDrawerRef<T, R>` |

### NzDrawerOptions

Expand Down Expand Up @@ -77,7 +77,7 @@ import { NzDrawerModule } from 'ng-zorro-antd/drawer';
| --- | --- | --- |
| close | close the drawer. | `(result?: R) => void` |
| open | open the drawer. | `() => void` |

| getContentComponent| Returns the instance when `nzContent` is the component. | `() => T \| null` |

#### Property
| Name | Description | Type |
Expand Down
3 changes: 2 additions & 1 deletion components/drawer/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { NzDrawerModule } from 'ng-zorro-antd/drawer';

| 方法名 | 说明 | 参数 | 返回 |
| --- | --- | --- | --- |
| create | 创建并打开一个 Drawer | `NzDrawerOptions<T, D, R>`| `NzDrawerRef<R>` |
| create<T, D, R> | 创建并打开一个 Drawer | `NzDrawerOptions<T, D>`| `NzDrawerRef<T, R>` |

### NzDrawerOptions

Expand Down Expand Up @@ -77,6 +77,7 @@ import { NzDrawerModule } from 'ng-zorro-antd/drawer';
| --- | --- | --- |
| close | 关闭 Drawer | `(result?: R) => void` |
| open | 打开 Drawer | `() => void` |
| getContentComponent| 返回 `nzContent` 为组件时的组件实例 | `() => T \| null` |

#### 属性

Expand Down
3 changes: 2 additions & 1 deletion components/drawer/drawer-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { Observable } from 'rxjs';
import { NzDrawerPlacement } from './drawer-options';

export abstract class NzDrawerRef<R = NzSafeAny> {
export abstract class NzDrawerRef<T = NzSafeAny, R = NzSafeAny> {
abstract afterClose: Observable<R>;
abstract afterOpen: Observable<void>;
abstract close(result?: R): void;
abstract open(): void;
abstract getContentComponent(): T | null;

abstract nzClosable?: boolean;
abstract nzNoAnimation?: boolean;
Expand Down
9 changes: 8 additions & 1 deletion components/drawer/drawer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const NZ_CONFIG_COMPONENT_NAME = 'drawer';
preserveWhitespaces: false,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class NzDrawerComponent<T = NzSafeAny, R = NzSafeAny, D = NzSafeAny> extends NzDrawerRef<R>
export class NzDrawerComponent<T = NzSafeAny, R = NzSafeAny, D = NzSafeAny> extends NzDrawerRef<T, R>
implements OnInit, OnDestroy, AfterViewInit, OnChanges, NzDrawerOptionsOfComponent {
static ngAcceptInputType_nzClosable: BooleanInput;
static ngAcceptInputType_nzMaskClosable: BooleanInput;
Expand All @@ -120,6 +120,7 @@ export class NzDrawerComponent<T = NzSafeAny, R = NzSafeAny, D = NzSafeAny> exte
@Input() nzZIndex = 1000;
@Input() nzOffsetX = 0;
@Input() nzOffsetY = 0;
private componentInstance: T | null = null;

@Input()
set nzVisible(value: boolean) {
Expand Down Expand Up @@ -288,6 +289,7 @@ export class NzDrawerComponent<T = NzSafeAny, R = NzSafeAny, D = NzSafeAny> exte
this.restoreFocus();
this.nzAfterClose.next(result);
this.nzAfterClose.complete();
this.componentInstance = null;
}, this.getAnimationDuration());
}

Expand All @@ -305,6 +307,10 @@ export class NzDrawerComponent<T = NzSafeAny, R = NzSafeAny, D = NzSafeAny> exte
}, this.getAnimationDuration());
}

getContentComponent(): T | null {
return this.componentInstance;
}

closeClick(): void {
this.nzOnClose.emit();
}
Expand All @@ -322,6 +328,7 @@ export class NzDrawerComponent<T = NzSafeAny, R = NzSafeAny, D = NzSafeAny> exte
const childInjector = new PortalInjector(this.injector, new WeakMap([[NzDrawerRef, this]]));
const componentPortal = new ComponentPortal<T>(this.nzContent, null, childInjector);
const componentRef = this.bodyPortalOutlet!.attachComponentPortal(componentPortal);
this.componentInstance = componentRef.instance;
Object.assign(componentRef.instance, this.nzContentParams);
componentRef.changeDetectorRef.detectChanges();
}
Expand Down
32 changes: 16 additions & 16 deletions components/drawer/drawer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { Overlay, OverlayRef } from '@angular/cdk/overlay';
import { ComponentPortal } from '@angular/cdk/portal';
import { ComponentRef, Injectable } from '@angular/core';
import { Injectable } from '@angular/core';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
Expand All @@ -14,56 +14,56 @@ import { NzDrawerRef } from './drawer-ref';
import { NzDrawerComponent } from './drawer.component';
import { NzDrawerServiceModule } from './drawer.service.module';

export class DrawerBuilderForService<R> {
private drawerRef: ComponentRef<NzDrawerComponent> | null;
export class DrawerBuilderForService<T, R> {
private drawerRef: NzDrawerComponent<T, R> | null;
private overlayRef: OverlayRef;
private unsubscribe$ = new Subject<void>();

constructor(private overlay: Overlay, private options: NzDrawerOptions) {
/** pick {@link NzDrawerOptions.nzOnCancel} and omit this option */
const { nzOnCancel, ...componentOption } = this.options;
this.overlayRef = this.overlay.create();
this.drawerRef = this.overlayRef.attach(new ComponentPortal(NzDrawerComponent));
this.drawerRef = this.overlayRef.attach(new ComponentPortal(NzDrawerComponent)).instance;
this.updateOptions(componentOption);
// Prevent repeatedly open drawer when tap focus element.
this.drawerRef!.instance.savePreviouslyFocusedElement();
this.drawerRef!.instance.nzOnViewInit.pipe(takeUntil(this.unsubscribe$)).subscribe(() => {
this.drawerRef!.instance.open();
this.drawerRef.savePreviouslyFocusedElement();
this.drawerRef.nzOnViewInit.pipe(takeUntil(this.unsubscribe$)).subscribe(() => {
this.drawerRef!.open();
});
this.drawerRef!.instance.nzOnClose.subscribe(() => {
this.drawerRef.nzOnClose.subscribe(() => {
if (nzOnCancel) {
nzOnCancel().then(canClose => {
if (canClose !== false) {
this.drawerRef!.instance.close();
this.drawerRef!.close();
}
});
} else {
this.drawerRef!.instance.close();
this.drawerRef!.close();
}
});

this.drawerRef!.instance.afterClose.pipe(takeUntil(this.unsubscribe$)).subscribe(() => {
this.drawerRef.afterClose.pipe(takeUntil(this.unsubscribe$)).subscribe(() => {
this.overlayRef.dispose();
this.drawerRef = null;
this.unsubscribe$.next();
this.unsubscribe$.complete();
});
}

getInstance(): NzDrawerRef<R> {
return this.drawerRef! && this.drawerRef!.instance;
getInstance(): NzDrawerRef<T, R> {
return this.drawerRef!;
}

updateOptions(options: NzDrawerOptionsOfComponent): void {
Object.assign(this.drawerRef!.instance, options);
Object.assign(this.drawerRef!, options);
}
}

@Injectable({ providedIn: NzDrawerServiceModule })
export class NzDrawerService {
constructor(private overlay: Overlay) {}

create<T = NzSafeAny, D = undefined, R = NzSafeAny>(options: NzDrawerOptions<T, D extends undefined ? {} : D>): NzDrawerRef<R> {
return new DrawerBuilderForService<R>(this.overlay, options).getInstance();
create<T = NzSafeAny, D = undefined, R = NzSafeAny>(options: NzDrawerOptions<T, D extends undefined ? {} : D>): NzDrawerRef<T, R> {
return new DrawerBuilderForService<T, R>(this.overlay, options).getInstance();
}
}
3 changes: 3 additions & 0 deletions components/drawer/drawer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ describe('NzDrawerService', () => {
component.openTemplate();
fixture.detectChanges();
tick(300);
expect(component.templateDrawerRef?.getContentComponent()).toBeNull();
expect(component.templateOpenSpy).toHaveBeenCalled();
fixture.detectChanges();
(overlayContainerElement.querySelector('.ant-drawer .ant-drawer-mask') as HTMLElement).click();
Expand All @@ -439,13 +440,15 @@ describe('NzDrawerService', () => {
drawerRef.afterClose.subscribe(closeSpy);
fixture.detectChanges();
expect(openSpy).not.toHaveBeenCalled();
expect(drawerRef.getContentComponent()).not.toBeNull();
tick(300);
expect(openSpy).toHaveBeenCalled();
(overlayContainerElement.querySelector('.ant-drawer .close-btn') as HTMLElement).click();
fixture.detectChanges();
tick(300);
expect(closeSpy).toHaveBeenCalled();
fixture.detectChanges();
expect(drawerRef.getContentComponent()).toBeNull();
}));

it('should `nzOnCancel` work', fakeAsync(() => {
Expand Down

0 comments on commit be2f917

Please sign in to comment.