Skip to content

Commit 9ee29ae

Browse files
authored
feat: 新增safe-null-pipe管道,数据空值时可进行占位符展示 (#269)
* feat: 新增safe-null-pipe管道,数据空值时可进行占位符展示
1 parent b811399 commit 9ee29ae

15 files changed

+126
-27
lines changed

devui/common/common.module.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
import { ClipboardModule } from '@angular/cdk/clipboard';
22
import { CommonModule } from '@angular/common';
33
import { NgModule } from '@angular/core';
4-
import { AutoFocusDirective } from './auto-focus.directive';
5-
import { ClipboardDirective } from './clipboard.directive';
4+
65
import { DatePipe } from './date-pipe';
6+
import { SafeNullPipe } from './safe-null.pipe';
7+
78
import { SimulateATagDirective } from './helper-utils';
9+
import { ClipboardDirective } from './clipboard.directive';
10+
import { AutoFocusDirective } from './auto-focus.directive';
811
import { IframeEventPropagateDirective } from './iframe-event-propagate.directive';
912

1013
@NgModule({
11-
imports: [
12-
CommonModule,
13-
ClipboardModule
14-
],
14+
imports: [CommonModule, ClipboardModule],
1515
exports: [
16-
AutoFocusDirective,
1716
DatePipe,
17+
SafeNullPipe,
18+
AutoFocusDirective,
1819
SimulateATagDirective,
1920
IframeEventPropagateDirective,
20-
ClipboardDirective
21+
ClipboardDirective,
2122
],
2223
declarations: [
23-
AutoFocusDirective,
2424
DatePipe,
25+
SafeNullPipe,
26+
AutoFocusDirective,
2527
SimulateATagDirective,
2628
IframeEventPropagateDirective,
27-
ClipboardDirective
29+
ClipboardDirective,
2830
],
2931
providers: [],
3032
})
31-
export class DCommonModule {
32-
}
33+
export class DCommonModule {}

devui/common/demo/common-demo.component.html

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<div class="devui-demo-container" dAnchorBox dAnchorHashSupport [view]="{ top: 180, bottom: 0 }">
22
<d-demo-nav [navItems]="navItems"></d-demo-nav>
33
<div class="devui-demo-example" [dAnchor]="'date-pipe'">
4-
<div class="devui-demo-title">{{ 'components.common.pipeDemo.title' | translate }}</div>
4+
<div class="devui-demo-title">{{ 'components.common.pipeDemo.dDatePipe.title' | translate }}</div>
55
<div class="devui-demo-text"></div>
6-
<d-codebox id="common-basic-usage" [sourceData]="pipeSource">
7-
<d-common-pipe demo></d-common-pipe>
6+
<d-codebox id="common-basic-usage" [sourceData]="datePipeSource">
7+
<d-date-pipe demo></d-date-pipe>
8+
</d-codebox>
9+
</div>
10+
<div class="devui-demo-example" [dAnchor]="'safe-null-pipe'">
11+
<div class="devui-demo-title">{{ 'components.common.pipeDemo.dSafeNullPipe.title' | translate }}</div>
12+
<div class="devui-demo-text">{{ 'components.common.pipeDemo.dSafeNullPipe.description' | translate }}</div>
13+
<div class="devui-demo-text"></div>
14+
<d-codebox id="common-basic-usage" [sourceData]="safeNullPipeSource">
15+
<d-safe-null-pipe demo></d-safe-null-pipe>
816
</d-codebox>
917
</div>
1018

devui/common/demo/common-demo.component.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import { Subscription } from 'rxjs';
88
templateUrl: './common-demo.component.html',
99
})
1010
export class CommonDemoComponent implements OnInit, OnDestroy {
11-
pipeSource: Array<DevuiSourceData> = [
12-
{ title: 'HTML', language: 'html', code: require('./pipe/pipe.component.html?raw') },
13-
{ title: 'TS', language: 'typescript', code: require('./pipe/pipe.component.ts?raw') },
11+
datePipeSource: Array<DevuiSourceData> = [
12+
{ title: 'HTML', language: 'html', code: require('./pipe/date-pipe/date-pipe.component.html?raw') },
13+
{ title: 'TS', language: 'typescript', code: require('./pipe/date-pipe/date-pipe.component.ts?raw') },
14+
];
15+
safeNullPipeSource: Array<DevuiSourceData> = [
16+
{ title: 'HTML', language: 'html', code: require('./pipe/safe-null-pipe/safe-null-pipe.component.html?raw') },
17+
{ title: 'TS', language: 'typescript', code: require('./pipe/safe-null-pipe/safe-null-pipe.component.ts?raw') },
1418
];
1519

1620
helperBrowserSource: Array<DevuiSourceData> = [
@@ -67,6 +71,7 @@ export class CommonDemoComponent implements OnInit, OnDestroy {
6771
setNavValues(values) {
6872
this.navItems = [
6973
{ dAnchorLink: 'date-pipe', value: values['date-pipe'] },
74+
{ dAnchorLink: 'safe-null-pipe', value: values['safe-null-pipe'] },
7075
{ dAnchorLink: 'browser-version', value: values['browser-version'] },
7176
{ dAnchorLink: 'open-url', value: values['open-url'] },
7277
{ dAnchorLink: 'download-file', value: values['download-file'] },

devui/common/demo/common-demo.module.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import { HelperDownloadDemoComponent } from './helper-download/helper-download.c
2222
import { HelperJumpDemoComponent } from './helper-jump/helper-jump.component';
2323
import { IframPropagateDemoComponent } from './iframe-propagate/iframe-propagate.component';
2424
import { LazyLoadComponent } from './lazy-load/lazy-load.component';
25-
import { PipeDemoComponent } from './pipe/pipe.component';
25+
import { DatePipeDemoComponent } from './pipe/date-pipe/date-pipe.component';
26+
import { SafeNullPipeComponent } from './pipe/safe-null-pipe/safe-null-pipe.component';
27+
2628

2729
@NgModule({
2830
imports: [
@@ -55,14 +57,15 @@ import { PipeDemoComponent } from './pipe/pipe.component';
5557
],
5658
exports: [CommonDemoComponent],
5759
declarations: [
58-
PipeDemoComponent,
60+
DatePipeDemoComponent,
5961
CommonDemoComponent,
6062
HelperBrowserComponent,
6163
HelperJumpDemoComponent,
6264
HelperDownloadDemoComponent,
6365
IframPropagateDemoComponent,
6466
LazyLoadComponent,
65-
ClipboardDemoComponent
67+
ClipboardDemoComponent,
68+
SafeNullPipeComponent
6669
],
6770
})
6871
export class CommonDemoModule {}

devui/common/demo/pipe/pipe.component.ts renamed to devui/common/demo/pipe/date-pipe/date-pipe.component.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Component, OnInit } from '@angular/core';
22

33
@Component({
4-
selector: 'd-common-pipe',
5-
templateUrl: './pipe.component.html',
4+
selector: 'd-date-pipe',
5+
templateUrl: './date-pipe.component.html',
66
})
7-
export class PipeDemoComponent implements OnInit {
7+
export class DatePipeDemoComponent implements OnInit {
88
date = new Date(2014, 1, 11, 13, 1, 22);
99
date1 = new Date(2015, 4, 5);
1010
constructor() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<pre>
2+
Name:{{ name | dSafeNullPipe }}
3+
4+
Age:{{ age | dSafeNullPipe }}
5+
6+
Gender:{{ gender | dSafeNullPipe: '*' }}
7+
8+
Address:{{ address | dSafeNullPipe: '****' }}
9+
</pre>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
2+
3+
@Component({
4+
selector: 'd-safe-null-pipe',
5+
templateUrl: './safe-null-pipe.component.html',
6+
changeDetection: ChangeDetectionStrategy.OnPush,
7+
})
8+
export class SafeNullPipeComponent {
9+
name = '';
10+
gender = void 0;
11+
age = 18;
12+
address = null;
13+
}

devui/common/doc/api-cn.md

+15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ import { HelperUtils } from 'ng-devui';
3232
<p>{{ date | dDatePipe: 'y/MM/dd' }}</p>
3333
</div>
3434

35+
<!-- SafeNullPipe -->
36+
<div>
37+
<p>{{ name | dSafeNullPipe }}</p>
38+
<p>{{ age | dSafeNullPipe:'***' }}</p>
39+
</div>
40+
3541
<!-- SimulateATag -->
3642
<button dSimulateATag [href]="'https://angular.io'" [target]="'_blank'"></button>
3743

@@ -301,6 +307,10 @@ import { HelperUtils } from 'ng-devui';
301307
</tbody>
302308
</table>
303309

310+
# dSafeNullPipe
311+
312+
当数据为`'',null,undefined`时,自动进行占位展示,默认为:`--`
313+
304314
# HelperUtils 静态方法
305315

306316
## getBrowserName() => void
@@ -444,15 +454,19 @@ export type PositionType = 'top' | 'right' | 'bottom' | 'left';
444454
```
445455

446456
### TargetType
457+
447458
```ts
448459
export type TargetType = '_blank' | '_self' | '_parent' | '_top' | ;
449460
```
461+
450462
### EncType
463+
451464
```ts
452465
export type EncType = 'application/x-www-form-urlencoded' | 'multipart/form-data' | 'text-plain'
453466
```
454467
455468
### CopyResult
469+
456470
```ts
457471
export interface CopyResult {
458472
isSupported: boolean;
@@ -462,6 +476,7 @@ export interface CopyResult {
462476
```
463477

464478
### HttpProgressEvent
479+
465480
```typescript
466481
interface HttpProgressEvent {
467482
type: HttpEventType.DownloadProgress | HttpEventType.UploadProgress;

devui/common/doc/api-en.md

+15
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ In the page:
2626
<p>{{ date | dDatePipe: 'y/MM/dd' }}</p>
2727
</div>
2828

29+
<!-- SafeNullPipe -->
30+
<div>
31+
<p>{{ name | dSafeNullPipe }}</p>
32+
<p>{{ age | dSafeNullPipe:'***' }}</p>
33+
</div>
34+
2935
<!-- SimulateATag -->
3036
<button dSimulateATag [href]="'https://angular.io'" [target]="'_blank'"></button>
3137

@@ -295,6 +301,10 @@ The formatting format is slightly different from the format provided by the Angu
295301
</tbody>
296302
</table>
297303

304+
# dSafeNullPipe
305+
306+
Placeholder display automatically when data is `'', null, undefined`,default is: `--`
307+
298308
# HelperUtils Static Methods
299309

300310
## getBrowserName() => void
@@ -436,17 +446,21 @@ export interface ClientDownloadOptions {
436446
```ts
437447
export type PositionType = 'top' | 'right' | 'bottom' | 'left';
438448
```
449+
439450
### TargetType
451+
440452
```ts
441453
export type TargetType = '_blank' | '_self' | '_parent' | '_top' | ;
442454
```
443455

444456
### EncType
457+
445458
```ts
446459
export type EncType = 'application/x-www-form-urlencoded' | 'multipart/form-data' | 'text-plain'
447460
```
448461
449462
### CopyResult
463+
450464
```ts
451465
export interface CopyResult {
452466
isSupported: boolean;
@@ -456,6 +470,7 @@ export interface CopyResult {
456470
```
457471

458472
### HttpProgressEvent
473+
459474
```typescript
460475
interface HttpProgressEvent {
461476
type: HttpEventType.DownloadProgress | HttpEventType.UploadProgress;

devui/common/public-api.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export * from './auto-focus.directive';
22
export * from './clipboard.directive';
33
export * from './common.module';
44
export * from './date-pipe';
5+
export * from './safe-null.pipe';
56
export * from './helper-utils';
67
export * from './iframe-event-propagate.directive';

devui/common/safe-null.pipe.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
3+
@Pipe({
4+
name: 'dSafeNullPipe',
5+
})
6+
export class SafeNullPipe implements PipeTransform {
7+
transform(value: unknown, replace: string = '--'): unknown {
8+
if (typeof value === 'undefined' || value === null || value === '') {
9+
return replace;
10+
}
11+
return value;
12+
}
13+
}

src/app/component/scope-list.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export const newScopeList: Array<string> | string = [
2424
'mention',
2525
'icon',
2626
'navSprite',
27-
'NumberTranslator'
27+
'NumberTranslator',
28+
'common'
2829
];
2930

3031
export const sunsetScopeList: Array<string> | string = [

src/assets/i18n/en-us.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,14 @@
883883
"p2": "Scroll monitoring uses other elements, such as window."
884884
},
885885
"pipeDemo": {
886-
"title": "DatePipe"
886+
"dDatePipe": {
887+
"title": "DatePipe"
888+
},
889+
"dSafeNullPipe": {
890+
"title": "SafeNullPipe",
891+
"description": "When the data is '', null, undefined can be specified any character placeholder display, the default is: '--'"
892+
893+
}
887894
},
888895
"browserDemo": {
889896
"title": "Current browser name and version"
@@ -905,6 +912,7 @@
905912
"lazy-load": "Lazyload Directive",
906913
"browser-version": "Current browser name and version",
907914
"date-pipe": "Datepipe",
915+
"safe-null-pipe": "Safe null pipe",
908916
"open-url": "Open URL in A New Tag",
909917
"download-file": "Download File",
910918
"iframe-propagate": "iframe Event Propagate API",

src/assets/i18n/zh-cn.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,13 @@
869869
"p2": "滚动监听使用其他元素,比如window·"
870870
},
871871
"pipeDemo": {
872-
"title": "日期解析器"
872+
"dDatePipe": {
873+
"title": "日期解析器"
874+
},
875+
"dSafeNullPipe": {
876+
"title": "空值占位管道",
877+
"description": "当数据为 ''、null、undefined时可指定任意字符进行占位展示,默认为:'--'"
878+
}
873879
},
874880
"browserDemo": {
875881
"title": "当前浏览器名和版本"
@@ -891,6 +897,7 @@
891897
"lazy-load": "懒加载指令",
892898
"browser-version": "当前浏览器名和版本",
893899
"date-pipe": "日期解析器",
900+
"safe-null-pipe": "空值占位管道",
894901
"open-url": "新标签页打开网页",
895902
"download-file": "文件下载",
896903
"iframe-propagate": "iframe 冒泡事件",

0 commit comments

Comments
 (0)