Skip to content

Commit

Permalink
feat(theme:pipe:date): add global config
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Nov 21, 2023
1 parent 1abe2d5 commit aebcce3
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
7 changes: 1 addition & 6 deletions packages/abc/st/test/st-data-source.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ describe('abc: table: data-souce', () => {
return val;
}
}
class MockNzI18nService {
getDateLocale(): null {
return null;
}
}

function genModule(): void {
options = {
Expand All @@ -79,7 +74,7 @@ describe('abc: table: data-souce', () => {
paginator: true
};
mockDomSanitizer = new MockDomSanitizer() as any;
datePipe = new DatePipe(new MockNzI18nService() as any);
datePipe = new DatePipe();
ynPipe = new YNPipe(mockDomSanitizer as any);
decimalPipe = new DecimalPipe('zh-CN');
http = new MockHttpClient();
Expand Down
17 changes: 3 additions & 14 deletions packages/abc/st/test/st.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,7 @@ import { NzPaginationComponent } from 'ng-zorro-antd/pagination';
import { NzTooltipDirective } from 'ng-zorro-antd/tooltip';

import { STWidgetRegistry } from './../st-widget';
import {
PS,
DEFAULTCOUNT,
USERS,
MOCKDATE,
MOCKIMG,
genData,
MockNzI18nService,
PageObject,
TestComponent,
genModule
} from './base.spec';
import { PS, DEFAULTCOUNT, USERS, MOCKDATE, MOCKIMG, genData, PageObject, TestComponent, genModule } from './base.spec';
import { STDataSource } from '../st-data-source';
import { STExport } from '../st-export';
import { STComponent } from '../st.component';
Expand Down Expand Up @@ -269,7 +258,7 @@ describe('abc: st', () => {
it(`should be render date`, fakeAsync(() => {
page
.updateColumn([{ title: '', index: 'date', type: 'date' }])
.expectCell(new DatePipe(new MockNzI18nService() as any).transform(MOCKDATE, 'yyyy-MM-dd HH:mm'))
.expectCell(new DatePipe().transform(MOCKDATE, 'yyyy-MM-dd HH:mm'))
.asyncEnd();
}));
it(`should be custom render date format`, fakeAsync(() => {
Expand All @@ -282,7 +271,7 @@ describe('abc: st', () => {
dateFormat: 'yyyy-MM'
}
])
.expectCell(new DatePipe(new MockNzI18nService() as any).transform(MOCKDATE, 'yyyy-MM'))
.expectCell(new DatePipe().transform(MOCKDATE, 'yyyy-MM'))
.asyncEnd();
}));
it(`should be text center`, fakeAsync(() => {
Expand Down
10 changes: 6 additions & 4 deletions packages/theme/src/pipes/date/date.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Pipe, PipeTransform } from '@angular/core';
import { Pipe, PipeTransform, inject } from '@angular/core';

import { AlainConfigService } from '@delon/util/config';
import { formatDate } from '@delon/util/date-time';
import { NzI18nService } from 'ng-zorro-antd/i18n';

@Pipe({ name: '_date', standalone: true })
export class DatePipe implements PipeTransform {
constructor(private nzI18n: NzI18nService) {}
private nzI18n = inject(NzI18nService);
private defFormat = inject(AlainConfigService).get('themePipe')?.dateFormat ?? 'yyyy-MM-dd HH:mm';

transform(value: Date | string | number, formatString: string = 'yyyy-MM-dd HH:mm'): string {
return formatDate(value, formatString, this.nzI18n.getDateLocale());
transform(value: Date | string | number, formatString?: string | null): string {
return formatDate(value, formatString ?? this.defFormat, this.nzI18n.getDateLocale());
}
}
2 changes: 2 additions & 0 deletions packages/util/config/config.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { AlainChartConfig } from './chart/chart.type';
import { AlainMockConfig } from './mock/mock.type';
import { AlainSFConfig } from './sf/sf.type';
import { AlainThemeHttpClientConfig, AlainThemeResponsiveConfig, AlainThemeI18nConfig } from './theme/index';
import { AlainThemePipeConfig } from './theme/pipe.type';
import { AlainUtilArrayConfig } from './util/array.type';
import { AlainUtilCurrencyConfig } from './util/currency.type';

Expand Down Expand Up @@ -60,6 +61,7 @@ export interface AlainConfig {
themeHttp?: AlainThemeHttpClientConfig;
themeResponsive?: AlainThemeResponsiveConfig;
themeI18n?: AlainThemeI18nConfig;
themePipe?: AlainThemePipeConfig;
}

export type AlainConfigKey = keyof AlainConfig;
Expand Down
3 changes: 3 additions & 0 deletions packages/util/config/theme/pipe.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface AlainThemePipeConfig {
dateFormat?: string;
}

0 comments on commit aebcce3

Please sign in to comment.