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

fix(datepicker): Create a new injection token to avoid overriding LOCALE_ID #6708

Merged
merged 4 commits into from
Sep 1, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
9 changes: 6 additions & 3 deletions src/lib/core/datetime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* found in the LICENSE file at https://angular.io/license
*/

import {NgModule} from '@angular/core';
import {LOCALE_ID, NgModule} from '@angular/core';
import {DateAdapter} from './date-adapter';
import {NativeDateAdapter} from './native-date-adapter';
import {NativeDateAdapter, MAT_DATE_LOCALE} from './native-date-adapter';
import {MD_DATE_FORMATS} from './date-formats';
import {MD_NATIVE_DATE_FORMATS} from './native-date-formats';

Expand All @@ -20,7 +20,10 @@ export * from './native-date-formats';


@NgModule({
providers: [{provide: DateAdapter, useClass: NativeDateAdapter}],
providers: [
{provide: DateAdapter, useClass: NativeDateAdapter},
{provide: MAT_DATE_LOCALE, useExisting: LOCALE_ID}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should go in the MdDatepickerModule

Copy link
Contributor

@mmalerba mmalerba Aug 30, 2017

Choose a reason for hiding this comment

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

@jelbourn can you think of a good way to avoid putting this in NativeDateModule, MomentDateModule, etc? I was thinking maybe we could put it in MdCommonModule and have this module import that?

Copy link
Member

Choose a reason for hiding this comment

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

I think it doesn't really belong in MdCommonModule since it is only used in a few components. You could create a separate MdCommonDateModule, but that also seems like overkill. Easier thing would probably be just to make a common provider, e.g.

export const MAT_DATE_LOCALE_PROVIDER = {provide: MAT_DATE_LOCALE, useExisting: LOCALE_ID};

Adding that is about the same as importing another module.

],
})
export class NativeDateModule {}

Expand Down
30 changes: 27 additions & 3 deletions src/lib/core/datetime/native-date-adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {TestBed, async, inject} from '@angular/core/testing';
import {LOCALE_ID} from '@angular/core';
import {NativeDateAdapter, NativeDateModule, DateAdapter} from './index';
import {NativeDateAdapter, NativeDateModule, DateAdapter, MAT_DATE_LOCALE} from './index';
import {Platform} from '../platform/index';
import {DEC, FEB, JAN, MAR} from '../testing/month-constants';
import {LOCALE_ID} from '@angular/core';

const SUPPORTS_INTL = typeof Intl != 'undefined';

Expand Down Expand Up @@ -334,6 +334,30 @@ describe('NativeDateAdapter', () => {
});


describe('NativeDateAdapter with MAT_DATE_LOCALE override', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we also have tests for LOCAL_ID override? this should still cascade to MAT_DATE_LOCALE

let adapter: NativeDateAdapter;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [NativeDateModule],
providers: [{provide: MAT_DATE_LOCALE, useValue: 'da-DK'}]
}).compileComponents();
}));

beforeEach(inject([DateAdapter], (d: NativeDateAdapter) => {
adapter = d;
}));

it('should take the default locale id from the MAT_DATE_LOCALE injection token', () => {
const expectedValue = SUPPORTS_INTL ?
['søndag', 'mandag', 'tirsdag', 'onsdag', 'torsdag', 'fredag', 'lørdag'] :
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

expect(adapter.getDayOfWeekNames('long')).toEqual(expectedValue);
});

});

describe('NativeDateAdapter with LOCALE_ID override', () => {
let adapter: NativeDateAdapter;

Expand All @@ -348,7 +372,7 @@ describe('NativeDateAdapter with LOCALE_ID override', () => {
adapter = d;
}));

it('should take the default locale id from the LOCALE_ID injection token', () => {
it('should cascade locale id from the LOCALE_ID injection token to MAT_DATE_LOCALE', () => {
const expectedValue = SUPPORTS_INTL ?
['søndag', 'mandag', 'tirsdag', 'onsdag', 'torsdag', 'fredag', 'lørdag'] :
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
Expand Down
9 changes: 5 additions & 4 deletions src/lib/core/datetime/native-date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Inject, Injectable, Optional, LOCALE_ID} from '@angular/core';
import {Inject, Injectable, Optional, InjectionToken} from '@angular/core';
import {DateAdapter} from './date-adapter';
import {extendObject} from '../util/object-extend';


// TODO(mmalerba): Remove when we no longer support safari 9.
/** Whether the browser supports the Intl API. */
const SUPPORTS_INTL_API = typeof Intl != 'undefined';
Expand Down Expand Up @@ -48,13 +47,15 @@ function range<T>(length: number, valueFunction: (index: number) => T): T[] {
return valuesArray;
}

/** InjectionToken for datepicker that can be used to override default locale code. */
export const MAT_DATE_LOCALE = new InjectionToken<string>('MAT_DATE_LOCALE');
Copy link
Contributor

Choose a reason for hiding this comment

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

can we move this to DateAdapter since we expect all adapters to use it not just NativeDateAdapter?


/** Adapts the native JS Date for use with cdk-based components that work with dates. */
@Injectable()
export class NativeDateAdapter extends DateAdapter<Date> {
constructor(@Optional() @Inject(LOCALE_ID) localeId: any) {
constructor(@Optional() @Inject(MAT_DATE_LOCALE) matDateLocale: string) {
super();
super.setLocale(localeId);
super.setLocale(matDateLocale);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/lib/datepicker/datepicker.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ three pieces via injection:
3. The message strings used in the datepicker's UI.

#### Setting the locale code
By default the datepicker will use the locale code from the `LOCALE_ID` injection token from
`@angular/core`. If you want to override it, you can provide a new value for the token:

By default `MAT_DATE_LOCALE` injection token will use the existing `LOCALE_ID` locale code from
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: By default MAT_DATE_LOCALE injection token -> By default, the MAT_DATE_LOCALE injection token

`@angular/core`. If you want to override it, you can provide a new value for the `MAT_DATE_LOCALE`
token:
```ts
@NgModule({
providers: [
{provide: LOCALE_ID, useValue: 'en-GB'},
{provide: MAT_DATE_LOCALE, useValue: 'en-GB'},
],
})
export class MyApp {}
Expand Down