Skip to content

Commit

Permalink
feat(material-luxon-adapter): add option to set first day of week (#2…
Browse files Browse the repository at this point in the history
…4027)

* feat(material-luxon-adapter): add option to set first day of week

Add firstDayOfWeek option to be able to change first day of week behaviour through MAT_LUXON_DATE_ADAPTER_OPTIONS

* test(material-luxon-adapter): Added one more test to trigger CI
  • Loading branch information
JiriBalcar authored Dec 3, 2021
1 parent 1ca8a78 commit a52da04
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
25 changes: 24 additions & 1 deletion src/material-luxon-adapter/adapter/luxon-date-adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ describe('LuxonDateAdapter', () => {
]);
});

it('should get month names in a czech locale', () => {
adapter.setLocale('cs-CZ');

expect(adapter.getMonthNames('long')).toEqual([
'leden',
'únor',
'březen',
'duben',
'květen',
'červen',
'červenec',
'srpen',
'září',
'říjen',
'listopad',
'prosinec',
]);
});

it('should get month names in a different locale', () => {
adapter.setLocale('da-DK');

Expand Down Expand Up @@ -575,7 +594,7 @@ describe('LuxonDateAdapter with MAT_LUXON_DATE_ADAPTER_OPTIONS override', () =>
providers: [
{
provide: MAT_LUXON_DATE_ADAPTER_OPTIONS,
useValue: {useUtc: true},
useValue: {useUtc: true, firstDayOfWeek: 1},
},
],
}).compileComponents();
Expand All @@ -590,6 +609,10 @@ describe('LuxonDateAdapter with MAT_LUXON_DATE_ADAPTER_OPTIONS override', () =>
expect(adapter.createDate(2017, 0, 5).toISO()).toBe(DateTime.utc(2017, JAN, 5).toISO());
});

it('should get first day of week', () => {
expect(adapter.getFirstDayOfWeek()).toBe(1);
});

it('should create today in UTC', () => {
const today = adapter.today();
expect(today.toISO()).toBe(today.toUTC().toISO());
Expand Down
13 changes: 11 additions & 2 deletions src/material-luxon-adapter/adapter/luxon-date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export interface MatLuxonDateAdapterOptions {
* {@default false}
*/
useUtc: boolean;

/**
* Sets the first day of week.
* Changing this will change how Angular Material components like DatePicker shows start of week.
* {@default 0}
*/
firstDayOfWeek: number;
}

/** InjectionToken for LuxonDateAdapter to configure options. */
Expand All @@ -37,6 +44,7 @@ export const MAT_LUXON_DATE_ADAPTER_OPTIONS = new InjectionToken<MatLuxonDateAda
export function MAT_LUXON_DATE_ADAPTER_OPTIONS_FACTORY(): MatLuxonDateAdapterOptions {
return {
useUtc: false,
firstDayOfWeek: 0,
};
}

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

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

Expand Down Expand Up @@ -109,8 +119,7 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
}

getFirstDayOfWeek(): number {
// To customize the first day of the week, and can extend this adapter and override this method.
return 0;
return this._firstDayOfWeek;
}

getNumDaysInMonth(date: LuxonDateTime): number {
Expand Down

0 comments on commit a52da04

Please sign in to comment.