Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(material-luxon-adapter): add option to set default calendar #27453

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this will affect uses of Luxon outside of Angular Material which we generally try to avoid. Can we do this only when constructing a date instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code. Thanks

}

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
Loading