Skip to content

Commit

Permalink
feat(module:progress): support TemplateRef for nzFormat (#4598)
Browse files Browse the repository at this point in the history
* feat(module:progress): support template ref for nzFormat

close #4596

* docs: add docs

* test: fix tests
  • Loading branch information
Wendell authored and vthinkxie committed Jan 6, 2020
1 parent 038691f commit edf0e9c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion components/progress/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { NzProgressModule } from 'ng-zorro-antd/progress';
| Property | Description | Type | Default |
| -------- | ----------- | ---- | ------- |
| `[nzType]` | to set the type | `'line' \| 'circle' \| 'dashboard'` | `'line'` |
| `[nzFormat]` | template function of the content | `(percent: number) => string` | `percent => percent + '%'` |
| `[nzFormat]` | template function of the content | `(percent: number) => string \| TemplateRef<{ $implicit: number }>` | `percent => percent + '%'` |
| `[nzPercent]` | to set the completion percentage | `number` | `0` |
| `[nzShowInfo]` | whether to display the progress value and the status icon | `boolean` | `true` ||
| `[nzStatus]` | to set the status of the Progress | `'success' \| 'exception' \| 'active' \| 'normal'` | - |
Expand Down
2 changes: 1 addition & 1 deletion components/progress/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { NzProgressModule } from 'ng-zorro-antd/progress';
| 属性 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| `[nzType]` | 类型 | `'line' \| 'circle' \| 'dashboard'` | `'line'` |
| `[nzFormat]` | 内容的模板函数 | `(percent: number) => string` | `percent => percent + '%'` |
| `[nzFormat]` | 内容的模板函数 | `(percent: number) => string \| TemplateRef<{ $implicit: number }>` | `percent => percent + '%'` |
| `[nzPercent]` | 百分比 | `number` | `0` |
| `[nzShowInfo]` | 是否显示进度数值或状态图标 | `boolean` | `true` ||
| `[nzStatus]` | 状态 | `'success' \| 'exception' \| 'active' \| 'normal'` | - |
Expand Down
4 changes: 3 additions & 1 deletion components/progress/nz-progress.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
<i nz-icon [nzType]="icon"></i>
</ng-container>
<ng-template #formatTemplate>
{{ formatter(nzPercent) }}
<ng-container *nzStringTemplateOutlet="formatter; context: { $implicit: nzPercent }">
{{ formatter(nzPercent) }}
</ng-container>
</ng-template>
</span>
</ng-template>
Expand Down
3 changes: 2 additions & 1 deletion components/progress/nz-progress.definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

import { TemplateRef } from '@angular/core';
import { NgStyleInterface } from 'ng-zorro-antd/core';

export type NzProgressGapPositionType = 'top' | 'bottom' | 'left' | 'right';
Expand All @@ -29,7 +30,7 @@ export type NzProgressColorGradient = { direction?: string } & (NzProgressGradie

export type NzProgressStrokeColorType = string | NzProgressColorGradient;

export type NzProgressFormatter = (percent: number) => string;
export type NzProgressFormatter = ((percent: number) => string) | TemplateRef<{ $implicit: number }>;

export interface NzProgressCirclePath {
stroke: string | null;
Expand Down
3 changes: 2 additions & 1 deletion components/progress/nz-progress.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { NzAddOnModule } from 'ng-zorro-antd/core';
import { NzIconModule } from 'ng-zorro-antd/icon';

import { NzProgressComponent } from './nz-progress.component';

@NgModule({
exports: [NzProgressComponent],
declarations: [NzProgressComponent],
imports: [CommonModule, NzIconModule]
imports: [CommonModule, NzIconModule, NzAddOnModule]
})
export class NzProgressModule {}
16 changes: 11 additions & 5 deletions components/progress/nz-progress.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, DebugElement } from '@angular/core';
import { Component, DebugElement, TemplateRef, ViewChild } from '@angular/core';
import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';

Expand Down Expand Up @@ -79,13 +79,17 @@ describe('progress', () => {
expect(progress.nativeElement.querySelector('.ant-progress')!.classList).not.toContain('ant-progress-status-success');
});

it('should format work', () => {
testComponent.format = (percent: number) => `${percent} percent`;
it('should formatter work', () => {
testComponent.formatter = (percent: number) => `${percent} percent`;
fixture.detectChanges();
expect(progress.nativeElement.querySelector('.ant-progress-text').innerText.trim()).toBe('0 percent');
testComponent.percent = 100;
fixture.detectChanges();
expect(progress.nativeElement.querySelector('.ant-progress-text').innerText.trim()).toBe('100 percent');

testComponent.formatter = testComponent.formatterTemplate;
fixture.detectChanges();
expect(progress.nativeElement.querySelector('.ant-progress-text').innerText.trim()).toBe('100 / 100');
});

it('should status work', () => {
Expand Down Expand Up @@ -373,7 +377,7 @@ describe('progress', () => {
<nz-progress
[nzSize]="size"
[nzSuccessPercent]="successPercent"
[nzFormat]="format"
[nzFormat]="formatter"
[nzStatus]="status"
[nzShowInfo]="showInfo"
[nzStrokeWidth]="strokeWidth"
Expand All @@ -382,12 +386,14 @@ describe('progress', () => {
[nzStrokeLinecap]="strokeLinecap"
>
</nz-progress>
<ng-template #formatterTemplate let-percent> {{ percent }} / 100 </ng-template>
`
})
export class NzTestProgressLineComponent {
@ViewChild('formatterTemplate') formatterTemplate: TemplateRef<{ $implicit: number }>;
size: string;
status: string;
format: NzProgressFormatter;
formatter: NzProgressFormatter;
strokeWidth: number;
percent = 0;
successPercent = 0;
Expand Down

0 comments on commit edf0e9c

Please sign in to comment.