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

refactor(module:steps): refactor steps #2512

Merged
merged 1 commit into from
Nov 30, 2018
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
3 changes: 3 additions & 0 deletions components/core/interface/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface ClassMap {
[ key: string ]: boolean;
}
3 changes: 3 additions & 0 deletions components/core/style/map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function classMapToString(map: { [ key: string ]: boolean }): string {
return Object.keys(map).filter(item => !!map[ item ]).join(' ');
}
2 changes: 1 addition & 1 deletion components/steps/demo/icon.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ title:

## zh-CN

通过设置 `nz-step` 的 `nzIcon` 属性,可以启用自定义图标。旧的 API 仍然可用,但我们建议您迁移到新的 API。
通过设置 `nz-step` 的 `nzIcon` 属性,可以启用自定义图标。

## en-US

Expand Down
11 changes: 6 additions & 5 deletions components/steps/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ The whole of the step bar.

| Property | Description | Type | Default |
| -------- | ----------- | ---- | ------- |
| `[nzCurrent]` | to set the current step, counting from 0. You can overwrite this state by using `nzStatus` of `nz-step` | number | 0 |
| `[nzDirection]` | to specify the direction of the step bar, `horizontal` and `vertical` are currently supported | string | `horizontal` |
| `[nzCurrent]` | To set the current step, counting from 0. You can overwrite this state by using `nzStatus` of `nz-step` | number | 0 |
| `[nzDirection]` | To specify the direction of the step bar, `horizontal` and `vertical` are currently supported | string | `horizontal` |
| `[nzLabelPlacement]` | Support vertical title and description | string | `horizontal` |
| `[nzProgressDot]` | Steps with progress dot style, customize the progress dot by setting it with TemplateRef | Boolean 丨 `TemplateRef<{ $implicit: TemplateRef<void>, status: string, index: number }>` | false |
| `[nzSize]` | to specify the size of the step bar, `default` and `small` are currently supported | string | `default` |
| `[nzStatus]` | to specify the status of current step, can be set to one of the following values: `wait` `process` `finish` `error` | string | `process` |
| `[nzStartIndex]` | to specify the starting number | number | 0 |
| `[nzSize]` | To specify the size of the step bar, `default` and `small` are currently supported | string | `default` |
| `[nzStatus]` | To specify the status of current step, can be set to one of the following values: `wait` `process` `finish` `error` | string | `process` |
| `[nzStartIndex]` | To specify the starting number | number | 0 |

### nz-step

Expand Down
3 changes: 2 additions & 1 deletion components/steps/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ title: Steps
| 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| `[nzCurrent]` | 指定当前步骤,从 0 开始记数。在子 `nz-step` 元素中,可以通过 `nzStatus` 属性覆盖状态 | number | 0 |
| `[nzDirection]` | 指定步骤条方向。目前支持水平(`horizontal`)和竖直(`vertical`)两种方向 | string | horizontal |
| `[nzDirection]` | 指定步骤条方向。目前支持水平(`horizontal`)和竖直(`vertical`)两种方向 | string | `horizontal` |
| `[nzLabelPlacement]` | 指定标签放置位置,默认水平放图标右侧,可选 `vertical` 放图标下方 | string | `horizontal` |
| `[nzProgressDot]` | 点状步骤条,可以设置为一个 TemplateRef | Boolean 丨 `TemplateRef<{ $implicit: TemplateRef<void>, status: string, index: number }>` | false |
| `[nzSize]` | 指定大小,目前支持普通(`default`)和迷你(`small`) | string | default |
| `[nzStatus]` | 指定当前步骤的状态,可选 `wait` `process` `finish` `error` | string | process |
Expand Down
29 changes: 10 additions & 19 deletions components/steps/nz-step.component.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
<ng-template #titleTemplate>
<ng-template [ngTemplateOutlet]="nzTitle"></ng-template>
</ng-template>
<ng-template #descriptionTemplate>
<ng-template [ngTemplateOutlet]="nzDescription"></ng-template>
</ng-template>
<div class="ant-steps-item-tail" *ngIf="last !== true"></div>
<div class="ant-steps-item-icon">
<ng-template [ngIf]="!showProcessDot">
<span class="ant-steps-icon" *ngIf="nzStatus === 'finish' && !nzIcon">
<i nz-icon type="check"></i>
</span>
<span class="ant-steps-icon" *ngIf="nzStatus === 'error'">
<i nz-icon type="close"></i>
</span>
<span class="ant-steps-icon" *ngIf="(nzStatus === 'process' || nzStatus === 'wait') && !nzIcon">
{{ index + 1 }}
</span>
<span class="ant-steps-icon" *ngIf="nzStatus === 'finish' && !nzIcon"><i nz-icon type="check"></i></span>
<span class="ant-steps-icon" *ngIf="nzStatus === 'error'"><i nz-icon type="close"></i></span>
<span class="ant-steps-icon" *ngIf="(nzStatus === 'process' || nzStatus === 'wait') && !nzIcon">{{ index + 1 }}</span>
<span class="ant-steps-icon" *ngIf="nzIcon">
<ng-container *ngIf="isIconString; else iconTemplate">
<i nz-icon [type]="!oldAPIIcon && nzIcon" [ngClass]="oldAPIIcon && nzIcon"></i>
Expand All @@ -30,15 +18,18 @@
<ng-template #processDotTemplate>
<span class="ant-steps-icon-dot"></span>
</ng-template>
<ng-template [ngTemplateOutlet]="customProcessTemplate||processDotTemplate" [ngTemplateOutletContext]="{ $implicit: processDotTemplate, status:nzStatus, index:index }"></ng-template>
<ng-template
[ngTemplateOutlet]="customProcessTemplate||processDotTemplate"
[ngTemplateOutletContext]="{ $implicit: processDotTemplate, status:nzStatus, index:index }">
</ng-template>
</span>
</ng-template>
</div>
<div class="ant-steps-item-content">
<div class="ant-steps-item-title">
<ng-container *ngIf="isTitleString; else titleTemplate">{{ nzTitle }}</ng-container>
<ng-container *nzStringTemplateOutlet="nzTitle">{{ nzTitle }}</ng-container>
</div>
<div class="ant-steps-item-description">
<ng-container *ngIf="isDescriptionString; else descriptionTemplate">{{ nzDescription }}</ng-container>
<ng-container *nzStringTemplateOutlet="nzDescription">{{ nzDescription }}</ng-container>
</div>
</div>
</div>
134 changes: 43 additions & 91 deletions components/steps/nz-step.component.ts
Original file line number Diff line number Diff line change
@@ -1,124 +1,76 @@
import {
Component,
ElementRef,
Input,
TemplateRef,
ViewChild
} from '@angular/core';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, TemplateRef, ViewChild, ViewEncapsulation } from '@angular/core';

import { NzUpdateHostClassService } from '../core/services/update-host-class.service';
import { NgClassType } from '../core/types/ng-class';

@Component({
changeDetection : ChangeDetectionStrategy.OnPush,
encapsulation : ViewEncapsulation.None,
selector : 'nz-step',
providers : [ NzUpdateHostClassService ],
preserveWhitespaces: false,
templateUrl : './nz-step.component.html'
templateUrl : './nz-step.component.html',
host : {
'[class.ant-steps-item]' : 'true',
'[class.ant-steps-item-wait]' : 'nzStatus === "wait"',
'[class.ant-steps-item-process]': 'nzStatus === "process"',
'[class.ant-steps-item-finish]' : 'nzStatus === "finish"',
'[class.ant-steps-item-error]' : 'nzStatus === "error"',
'[class.ant-steps-custom]' : '!!nzIcon',
'[class.ant-steps-next-error]' : '(outStatus === "error") && (currentIndex === index + 1)'
}
})
export class NzStepComponent {
private _status = 'wait';
private _currentIndex = 0;
private _description: string | TemplateRef<void>;
private _icon: NgClassType | TemplateRef<void>;
private _title: string | TemplateRef<void>;
private el: HTMLElement = this.elementRef.nativeElement;
oldAPIIcon = true; // Make the user defined icon compatible to old API. Should be removed in 2.0.
isCustomStatus = false;
isDescriptionString = true;
isTitleString = true;
isIconString = true;
last = false;
showProcessDot = false;
direction = 'horizontal';
outStatus = 'process';
index = 0;
@ViewChild('processDotTemplate') processDotTemplate: TemplateRef<void>;
customProcessTemplate: TemplateRef<{ $implicit: TemplateRef<void>, status: string, index: number }>;

@Input()
set nzTitle(value: string | TemplateRef<void>) {
this.isTitleString = !(value instanceof TemplateRef);
this._title = value;
}
@Input() nzTitle: string | TemplateRef<void>;
@Input() nzDescription: string | TemplateRef<void>;

get nzTitle(): string | TemplateRef<void> {
return this._title;
@Input()
get nzStatus(): string { return this._status; }
set nzStatus(status: string) {
this._status = status;
this.isCustomStatus = true;
}
isCustomStatus = false;
private _status = 'wait';

@Input()
get nzIcon(): NgClassType | TemplateRef<void> { return this._icon; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set nzIcon(value: NgClassType | TemplateRef<void>) {
if (!(value instanceof TemplateRef)) {
this.isIconString = true;
if (typeof value === 'string') {
const str = value as string;
this.oldAPIIcon = str.indexOf('anticon') > -1;
} else {
this.oldAPIIcon = true;
}
this.oldAPIIcon = typeof value === 'string' && value.indexOf('anticon') > -1;
} else {
this.isIconString = false;
}
this._icon = value;
}
oldAPIIcon = true;
isIconString = true;
private _icon: NgClassType | TemplateRef<void>;

get nzIcon(): NgClassType | TemplateRef<void> {
return this._icon;
}

@Input()
set nzStatus(status: string) {
this._status = status;
this.isCustomStatus = true;
this.updateClassMap();
}

get nzStatus(): string {
return this._status;
}

@Input()
set nzDescription(value: string | TemplateRef<void>) {
this.isDescriptionString = !(value instanceof TemplateRef);
this._description = value;
}

get nzDescription(): string | TemplateRef<void> {
return this._description;
}

get currentIndex(): number {
return this._currentIndex;
}
customProcessTemplate: TemplateRef<{ $implicit: TemplateRef<void>, status: string, index: number }>; // Set by parent.
direction = 'horizontal';
index = 0;
last = false;
outStatus = 'process';
showProcessDot = false;

get currentIndex(): number { return this._currentIndex; }
set currentIndex(current: number) {
this._currentIndex = current;
if (!this.isCustomStatus) {
if (current > this.index) {
this._status = 'finish';
} else if (current === this.index) {
if (this.outStatus) {
this._status = this.outStatus;
}
} else {
this._status = 'wait';
}
this._status = current > this.index
? 'finish'
: current === this.index
? this.outStatus || ''
: 'wait';
}
this.updateClassMap();
}
private _currentIndex = 0;

updateClassMap(): void {
const classMap = {
[ 'ant-steps-item' ] : true,
[ `ant-steps-item-wait` ] : this.nzStatus === 'wait',
[ `ant-steps-item-process` ]: this.nzStatus === 'process',
[ `ant-steps-item-finish` ] : this.nzStatus === 'finish',
[ `ant-steps-item-error` ] : this.nzStatus === 'error',
[ 'ant-steps-custom' ] : !!this.nzIcon,
[ 'ant-steps-next-error' ] : (this.outStatus === 'error') && (this.currentIndex === this.index + 1)
};
this.nzUpdateHostClassService.updateHostClass(this.el, classMap);
}
constructor(private cdr: ChangeDetectorRef) {}

constructor(private elementRef: ElementRef, private nzUpdateHostClassService: NzUpdateHostClassService) {
detectChanges(): void {
this.cdr.detectChanges();
}
}
2 changes: 1 addition & 1 deletion components/steps/nz-steps.component.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div class="ant-steps" [ngClass]="stepsClassMap">
<div class="ant-steps" [ngClass]="classMap">
<ng-content></ng-content>
</div>
Loading