Skip to content

Commit

Permalink
fixing date time formatter for respect displayformat attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorTomaili committed Jun 26, 2024
1 parent a01bf00 commit d4f02b1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/corelib/dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4886,7 +4886,9 @@ declare class DateFormatter implements Formatter {
format(ctx: FormatterContext): string;
}
declare class DateTimeFormatter extends DateFormatter {
constructor();
constructor(props?: {
displayFormat?: string;
});
}
declare class EnumFormatter implements Formatter {
readonly props: {
Expand Down
6 changes: 3 additions & 3 deletions packages/corelib/src/ui/formatters/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ export class DateFormatter implements Formatter {

@Decorators.registerFormatter('Serenity.DateTimeFormatter')
export class DateTimeFormatter extends DateFormatter {
constructor() {
super();
this.displayFormat = Culture.dateTimeFormat;
constructor(props: { displayFormat?: string } = {}) {
props.displayFormat ??= Culture.dateTimeFormat;
super(props)
}
}

Expand Down
39 changes: 38 additions & 1 deletion packages/corelib/src/ui/helpers/slickhelpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addLocalText } from "../../base";
import { Culture, addLocalText } from "../../base";
import { PropertyItemSlickConverter } from "./slickhelpers";

describe('SlickHelpers.toSlickColumn', () => {
Expand All @@ -17,4 +17,41 @@ describe('SlickHelpers.toSlickColumn', () => {

expect(converted2.name).toBe('translated');
});

it('should pass date formatter to slick formatter', () => {
var converted = PropertyItemSlickConverter.toSlickColumn({
title: 'Test.Local.Text.Key',
formatterType: 'Serenity.DateFormatter'
});

Culture.dateSeparator = '/';
Culture.dateOrder = 'dmy';
Culture.dateFormat = 'dd/MM/yyyy';
Culture.dateTimeFormat = 'dd/MM/yyyy HH:mm:ss';

expect(converted.format).toBeDefined();
var formattedDate = converted.format.call(null, { value: '2021-01-01T00:00:00' });
expect(formattedDate).toBe('01/01/2021');
});


it('should pass date time formatter parameters to slick formatter', () => {
var converted = PropertyItemSlickConverter.toSlickColumn({
title: 'Test.Local.Text.Key',
formatterType: 'Serenity.DateTimeFormatter',
formatterParams: {
displayFormat: 'g'
}
});

Culture.dateSeparator = '/';
Culture.dateOrder = 'dmy';
Culture.dateFormat = 'dd/MM/yyyy';
Culture.dateTimeFormat = 'dd/MM/yyyy HH:mm:ss';

expect(converted.format).toBeDefined();
var formattedDate = converted.format.call(null, { value: '2021-01-01T00:00:00' });
console.log(formattedDate)
expect(formattedDate).toBe('01/01/2021 00:00');
});
});
4 changes: 3 additions & 1 deletion packages/corelib/wwwroot/index.global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6175,7 +6175,9 @@ declare namespace Serenity {
format(ctx: Slick.FormatterContext): string;
}
class DateTimeFormatter extends DateFormatter {
constructor();
constructor(props?: {
displayFormat?: string;
});
}
class EnumFormatter implements Formatter {
readonly props: {
Expand Down

0 comments on commit d4f02b1

Please sign in to comment.