Skip to content

Commit

Permalink
feat(module:message): support nzTop (#3047)
Browse files Browse the repository at this point in the history
* feat(module:message): support nzTop

* fix: types

* fix: add new test case of nzTop & nzBottom

* fix: lint

* docs: add unit

close #3041
  • Loading branch information
Wendell authored and hsuanxyz committed Mar 20, 2019
1 parent 93a06cf commit 351135b
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 87 deletions.
1 change: 1 addition & 0 deletions components/message/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Methods for destruction are also provided:
| nzMaxStack | The maximum number of messages that can be displayed at the same time | `number` | `8` |
| nzPauseOnHover | Do not remove automatically when mouse is over while setting to `true` | `boolean` | `true` |
| nzAnimate | Whether to turn on animation | `boolean` | `true` |
| nzTop | Distance from top | `number|string` | `24` |

### NzMessageDataFilled

Expand Down
1 change: 1 addition & 0 deletions components/message/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ title: Message
| nzMaxStack | 同一时间可展示的最大提示数量 | `number` | `8` |
| nzPauseOnHover | 鼠标移上时禁止自动移除 | `boolean` | `true` |
| nzAnimate | 开关动画效果 | `boolean` | `true` |
| nzTop | 消息距离顶部的位置 | `number|string` | `24` |

### NzMessageDataFilled

Expand Down
16 changes: 8 additions & 8 deletions components/message/nz-message-config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { InjectionToken } from '@angular/core';

export interface NzMessageConfig {
// For all messages as default config (can override when dynamically created)
nzDuration?: number;
nzPauseOnHover?: boolean;
nzAnimate?: boolean;
// For message container only
nzDuration?: number;
nzMaxStack?: number;
/* tslint:disable-next-line:no-any */
[index: string]: any;
nzPauseOnHover?: boolean;
nzTop?: number | string;

[index: string]: any; // tslint:disable-line:no-any
}

export const NZ_MESSAGE_DEFAULT_CONFIG = new InjectionToken<NzMessageConfig>('NZ_MESSAGE_DEFAULT_CONFIG');
Expand All @@ -18,9 +17,10 @@ export const NZ_MESSAGE_CONFIG = new InjectionToken<NzMessageConfig>('NZ_MESSAGE
export const NZ_MESSAGE_DEFAULT_CONFIG_PROVIDER = {
provide : NZ_MESSAGE_DEFAULT_CONFIG,
useValue: {
nzDuration : 3000,
nzAnimate : true,
nzDuration : 3000,
nzMaxStack : 7,
nzPauseOnHover: true,
nzMaxStack : 7
nzTop : 24
}
};
2 changes: 1 addition & 1 deletion components/message/nz-message-container.component.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div class="ant-message">
<div class="ant-message" [style.top]="top">
<nz-message *ngFor="let message of messages; let i = index" [nzMessage]="message" [nzIndex]="i"></nz-message>
</div>
25 changes: 22 additions & 3 deletions components/message/nz-message-container.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Inject, Optional, ViewEncapsulation } from '@angular/core';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Inject,
Optional,
ViewEncapsulation
} from '@angular/core';
import { Subject } from 'rxjs';

import { NzMessageConfig, NZ_MESSAGE_CONFIG, NZ_MESSAGE_DEFAULT_CONFIG } from './nz-message-config';
import { NzMessageDataFilled, NzMessageDataOptions } from './nz-message.definitions';
import { toCssPixel } from '../core/util';

import {
NzMessageConfig,
NZ_MESSAGE_CONFIG,
NZ_MESSAGE_DEFAULT_CONFIG
} from './nz-message-config';
import {
NzMessageDataFilled,
NzMessageDataOptions
} from './nz-message.definitions';

@Component({
changeDetection : ChangeDetectionStrategy.OnPush,
Expand All @@ -14,6 +30,7 @@ import { NzMessageDataFilled, NzMessageDataOptions } from './nz-message.definiti
export class NzMessageContainerComponent {
messages: NzMessageDataFilled[] = [];
config: Required<NzMessageConfig>;
top: string | null;

constructor(
protected cdr: ChangeDetectorRef,
Expand All @@ -25,6 +42,8 @@ export class NzMessageContainerComponent {

setConfig(config: NzMessageConfig): void {
this.config = { ...this.config, ...config };
this.top = toCssPixel(this.config.nzTop);
this.cdr.markForCheck();
}

/**
Expand Down
23 changes: 15 additions & 8 deletions components/message/nz-message.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ describe('NzMessage', () => {
let messageService: NzMessageService;
let overlayContainer: OverlayContainer;
let overlayContainerElement: HTMLElement;
let demoAppFixture: ComponentFixture<DemoAppComponent>;
let demoAppFixture: ComponentFixture<NzTestMessageBasicComponent>;

beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
imports: [ NzMessageModule, NoopAnimationsModule ],
declarations: [ DemoAppComponent ],
providers: [ { provide: NZ_MESSAGE_CONFIG, useValue: { nzMaxStack: 2 } } ] // Override default config
declarations: [ NzTestMessageBasicComponent ],
providers: [ { provide: NZ_MESSAGE_CONFIG, useValue: { nzMaxStack: 2, nzTop: 24 } } ]
});

TestBed.compileComponents();
Expand All @@ -37,7 +37,7 @@ describe('NzMessage', () => {
});

beforeEach(() => {
demoAppFixture = TestBed.createComponent(DemoAppComponent);
demoAppFixture = TestBed.createComponent(NzTestMessageBasicComponent);
});

it('should open a message box with success', (() => {
Expand Down Expand Up @@ -155,21 +155,28 @@ describe('NzMessage', () => {

it('should emit event when message close', fakeAsync(() => {
let onCloseFlag = false;

const msg = messageService.create('loading', 'CLOSE');
msg.onClose!.subscribe(() => {
onCloseFlag = true;
});

demoAppFixture.detectChanges();
tick(50000);

expect(onCloseFlag).toBeTruthy();
}));

it('should container top to configured', fakeAsync(() => {
messageService.create('top', 'CHANGE');
demoAppFixture.detectChanges();

const messageContainerElement = overlayContainerElement.querySelector('.ant-message') as HTMLElement;
expect(messageContainerElement.style.top).toBe('24px');

tick(50000);
}));
});

@Component({
selector: 'nz-demo-app-component',
template: ``
})
export class DemoAppComponent {}
export class NzTestMessageBasicComponent {}
4 changes: 2 additions & 2 deletions components/notification/nz-notification-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { InjectionToken } from '@angular/core';
import { NzMessageConfig } from '../message/nz-message-config';

export interface NzNotificationConfig extends NzMessageConfig {
nzTop?: string;
nzBottom?: string;
nzTop?: string | number;
nzBottom?: string | number;
nzPlacement?: 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight' | string;
}

Expand Down
14 changes: 9 additions & 5 deletions components/notification/nz-notification-container.component.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<div
class="ant-notification ant-notification-{{config.nzPlacement}}"
[style.top]="(config.nzPlacement==='topLeft'||config.nzPlacement=='topRight')? config.nzTop:null"
[style.bottom]="(config.nzPlacement==='bottomLeft'||config.nzPlacement=='bottomRight')? config.nzBottom:null"
[style.right]="(config.nzPlacement==='bottomRight'||config.nzPlacement=='topRight')?'0px':null"
[style.left]="(config.nzPlacement==='topLeft'||config.nzPlacement=='bottomLeft')?'0px':null">
<nz-notification *ngFor="let message of messages; let i = index" [nzMessage]="message" [nzIndex]="i"></nz-notification>
[style.top]="(config.nzPlacement === 'topLeft' || config.nzPlacement === 'topRight') ? top : null"
[style.bottom]="(config.nzPlacement === 'bottomLeft' || config.nzPlacement === 'bottomRight') ? bottom : null"
[style.right]="(config.nzPlacement === 'bottomRight' || config.nzPlacement === 'topRight') ? '0px' : null"
[style.left]="(config.nzPlacement === 'topLeft' || config.nzPlacement === 'bottomLeft') ? '0px' : null">
<nz-notification
*ngFor="let message of messages; let i = index"
[nzMessage]="message"
[nzIndex]="i">
</nz-notification>
</div>
44 changes: 38 additions & 6 deletions components/notification/nz-notification-container.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ import {
} from '@angular/core';
import { Subject } from 'rxjs';

import { toCssPixel } from '../core/util';
import { NzMessageContainerComponent } from '../message/nz-message-container.component';
import { NzNotificationConfig, NZ_NOTIFICATION_CONFIG, NZ_NOTIFICATION_DEFAULT_CONFIG } from './nz-notification-config';
import { NzNotificationDataFilled, NzNotificationDataOptions } from './nz-notification.definitions';

import {
NzNotificationConfig,
NZ_NOTIFICATION_CONFIG,
NZ_NOTIFICATION_DEFAULT_CONFIG
} from './nz-notification-config';
import {
NzNotificationDataFilled,
NzNotificationDataOptions
} from './nz-notification.definitions';

@Component({
changeDetection : ChangeDetectionStrategy.OnPush,
Expand All @@ -20,6 +29,14 @@ import { NzNotificationDataFilled, NzNotificationDataOptions } from './nz-notifi
templateUrl : './nz-notification-container.component.html'
})
export class NzNotificationContainerComponent extends NzMessageContainerComponent {
config: Required<NzNotificationConfig>;
bottom: string | null;

/**
* @override
*/
messages: Array<Required<NzNotificationDataFilled>> = [];

constructor(
cdr: ChangeDetectorRef,
@Optional() @Inject(NZ_NOTIFICATION_DEFAULT_CONFIG) defaultConfig: NzNotificationConfig,
Expand All @@ -29,14 +46,26 @@ export class NzNotificationContainerComponent extends NzMessageContainerComponen
}

/**
* A list of notifications displayed on the screen.
* @override
*/
messages: Array<Required<NzNotificationDataFilled>> = [];
setConfig(config: NzNotificationConfig): void {
const newConfig = this.config = { ...this.config, ...config };
const placement = this.config.nzPlacement;

this.top = placement === 'topLeft' || placement === 'topRight'
? toCssPixel(newConfig.nzTop)
: null;
this.bottom = placement === 'bottomLeft' || placement === 'bottomRight'
? toCssPixel(newConfig.nzBottom)
: null;

this.cdr.markForCheck();
}

/**
* Create a new notification.
* If there's a notification whose `nzKey` is same with `nzKey` in `NzNotificationDataFilled`, replace its content instead of create a new one.
* If there's a notification whose `nzKey` is same with `nzKey` in `NzNotificationDataFilled`,
* replace its content instead of create a new one.
* @override
* @param notification
*/
Expand All @@ -59,7 +88,10 @@ export class NzNotificationContainerComponent extends NzMessageContainerComponen
this.cdr.detectChanges();
}

private replaceNotification(old: NzNotificationDataFilled, _new: NzNotificationDataFilled): void {
private replaceNotification(
old: NzNotificationDataFilled,
_new: NzNotificationDataFilled
): void {
old.title = _new.title;
old.content = _new.content;
old.template = _new.template;
Expand Down
Loading

0 comments on commit 351135b

Please sign in to comment.