Skip to content

Commit

Permalink
feat(material-luxon-adapter): add option to set default calendar
Browse files Browse the repository at this point in the history
Add defaultOutputCalendar option to be able to change default calndar through MAT_LUXON_DATE_ADAPTER_OPTIONS
  • Loading branch information
adotnusiyan committed Jul 13, 2023
1 parent 7cb8fed commit aae0ee7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/material-luxon-adapter/adapter/luxon-date-adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,40 @@ describe('LuxonDateAdapter with MAT_LUXON_DATE_ADAPTER_OPTIONS override', () =>
});
});

describe('LuxonDateAdapter with MAT_LUXON_DATE_ADAPTER_OPTIONS override for defaultOutputCalendar option', () => {
let adapter: DateAdapter<DateTime>;

const calendarExample = 'islamic';
Settings.defaultOutputCalendar = calendarExample;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [LuxonDateModule],
providers: [
{
provide: MAT_LUXON_DATE_ADAPTER_OPTIONS,
useValue: {defaultOutputCalendar: calendarExample},
},
],
}).compileComponents();

adapter = TestBed.inject(DateAdapter);
}));

describe('use Islamic calendar', () => {
it('should create Luxon date in Islamic calendar', () => {
// Use 0 since createDate takes 0-indexed months.
expect(adapter.createDate(2017, 0, 2).toLocaleString()).toBe(
DateTime.local(2017, JAN, 2).toLocaleString(),
);
});

it('should get year name in Islamic calendar', () => {
expect(adapter.getYearName(DateTime.local(2017, JAN, 1))).toBe('1438');
});
});
});

function assertValidDate(adapter: DateAdapter<DateTime>, d: DateTime | null, valid: boolean) {
expect(adapter.isDateInstance(d))
.not.withContext(`Expected ${d} to be a date instance`)
Expand Down
19 changes: 18 additions & 1 deletion src/material-luxon-adapter/adapter/luxon-date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
DateTime as LuxonDateTime,
Info as LuxonInfo,
DateTimeOptions as LuxonDateTimeOptions,
CalendarSystem as LuxonCalendarSystem,
Settings as LuxonSettings,
} from 'luxon';

/** Configurable options for the `LuxonDateAdapter`. */
Expand All @@ -27,6 +29,12 @@ export interface MatLuxonDateAdapterOptions {
* Changing this will change how Angular Material components like DatePicker shows start of week.
*/
firstDayOfWeek: number;

/**
* Sets the output Calendar.
* Changing this will change how Angular Material components like DatePicker output dates.
*/
defaultOutputCalendar: LuxonCalendarSystem;
}

/** InjectionToken for LuxonDateAdapter to configure options. */
Expand All @@ -43,6 +51,7 @@ export function MAT_LUXON_DATE_ADAPTER_OPTIONS_FACTORY(): MatLuxonDateAdapterOpt
return {
useUtc: false,
firstDayOfWeek: 0,
defaultOutputCalendar: 'gregory',
};
}

Expand All @@ -60,6 +69,7 @@ function range<T>(length: number, valueFunction: (index: number) => T): T[] {
export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
private _useUTC: boolean;
private _firstDayOfWeek: number;
private _defaultOutputCalendar: LuxonCalendarSystem;

constructor(
@Optional() @Inject(MAT_DATE_LOCALE) dateLocale: string,
Expand All @@ -71,6 +81,9 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
this._useUTC = !!options?.useUtc;
this._firstDayOfWeek = options?.firstDayOfWeek || 0;
this.setLocale(dateLocale || LuxonDateTime.local().locale);

this._defaultOutputCalendar = options?.defaultOutputCalendar || 'gregory';
LuxonSettings.defaultOutputCalendar = this._defaultOutputCalendar;
}

getYear(date: LuxonDateTime): number {
Expand All @@ -91,7 +104,11 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
}

getMonthNames(style: 'long' | 'short' | 'narrow'): string[] {
return LuxonInfo.months(style, {locale: this.locale});
// Adding outputCalendar option, because LuxonInfo doesn't get effected by LuxonSettings
return LuxonInfo.months(style, {
locale: this.locale,
outputCalendar: this._defaultOutputCalendar,
});
}

getDateNames(): string[] {
Expand Down

0 comments on commit aae0ee7

Please sign in to comment.