angular / components

Component infrastructure and Material Design components for Angular
https://material.angular.io
MIT License
24.33k stars 6.73k forks source link

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

Open DmitryEfimenko opened 4 years ago

DmitryEfimenko commented 4 years ago

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]
  }
]
angular-robot[bot] commented 2 years ago

Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

Find more details about Angular's feature request process in our documentation.

angular-robot[bot] commented 2 years ago

Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

You can find more details about the feature request process in our documentation.