Skip to content

fr(datepicker): Allow changing format of the days shown as column headers #18824

@DmitryEfimenko

Description

@DmitryEfimenko

Feature Description

Currently, when using NativeDateAdapter, the days are formatted to use one letter:
image

When using MomentDateAdapter, the days are formatted to use two letters:
image

I'm not sure if this discrepancy is intentional or if it is at least known about. In either case, the developer does not have any control to change that without writing a custom DateAdapter.

Use Case

I have a requirement to display three letters for each day.

Proposed Implementation 1

Add a couple properties to the MAT_DATE_FORMATS:

export declare type MatDateFormats = {
    parse: { ... };
    display: {
        ...
        dayShortLabel: string;
        dayShortA11yLabel: string;
    };
};

I personally like this option better then the one below.

Proposed Implementation 2

Allow developer to implement a custom component to replace this portion of the calendar similar to how it's done with @Input() calendarHeaderComponent.

The workaround

As a scrappy workaround, I extended the MomentDateAdapter and overrode it's private property: the _localeData. Of course this makes this solution fragile to future updates.

import { Inject, Optional } from '@angular/core';
import { MAT_DATE_LOCALE } from '@angular/material';
import {
  MAT_MOMENT_DATE_ADAPTER_OPTIONS,
  MatMomentDateAdapterOptions,
  MomentDateAdapter,
} from '@angular/material-moment-adapter';

export class MyMomentDateAdapter extends MomentDateAdapter {
  constructor(
    @Optional() @Inject(MAT_DATE_LOCALE) dateLocale: string,
    @Optional() @Inject(MAT_MOMENT_DATE_ADAPTER_OPTIONS) options?: MatMomentDateAdapterOptions
  ) {
    super(dateLocale, options);

    // IMPORTANT! USE OF PRIVATE API:
    // MomentDateAdapter uses 'narrow' format for days displayed as column headers:
    // https://github.com/angular/components/blob/5e34de2fd2ae04d15f9bdbedc7835408633a2fa4/src/material/datepicker/month-view.ts#L268
    this['_localeData'].narrowDaysOfWeek = this['_localeData'].shortDaysOfWeek;
  }
}

Then use it in the module:

providers: [
  {
    provide: DateAdapter,
    useClass: MyMomentDateAdapter,
    deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
  }
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4A relatively minor issue that is not relevant to core functionsarea: material/datepickerfeatureThis issue represents a new feature or feature request rather than a bug or bug fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions