Skip to content

Commit 67247ac

Browse files
crisbetoandrewseguin
authored andcommitted
fix(datepicker): range selection strategy not picking up custom date adapter (#19914)
The default date range selection strategy was provided at the module level which meant that it wouldn't pick up custom date adapters down in the DI tree. These changes provide the token at the level of the range picker instead. Fixes #19902. (cherry picked from commit 984ca88)
1 parent 706b400 commit 67247ac

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

src/material/datepicker/date-range-picker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {ChangeDetectionStrategy, Component, ViewEncapsulation} from '@angular/core';
1010
import {MatDatepickerBase, MatDatepickerContent, MatDatepickerControl} from './datepicker-base';
1111
import {MAT_RANGE_DATE_SELECTION_MODEL_PROVIDER, DateRange} from './date-selection-model';
12+
import {MAT_CALENDAR_RANGE_STRATEGY_PROVIDER} from './date-range-selection-strategy';
1213

1314
/**
1415
* Input that can be associated with a date range picker.
@@ -29,7 +30,10 @@ export interface MatDateRangePickerInput<D> extends MatDatepickerControl<D> {
2930
exportAs: 'matDateRangePicker',
3031
changeDetection: ChangeDetectionStrategy.OnPush,
3132
encapsulation: ViewEncapsulation.None,
32-
providers: [MAT_RANGE_DATE_SELECTION_MODEL_PROVIDER]
33+
providers: [
34+
MAT_RANGE_DATE_SELECTION_MODEL_PROVIDER,
35+
MAT_CALENDAR_RANGE_STRATEGY_PROVIDER,
36+
]
3337
})
3438
export class MatDateRangePicker<D> extends MatDatepickerBase<MatDateRangePickerInput<D>,
3539
DateRange<D>, D> {

src/material/datepicker/date-range-selection-strategy.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Injectable, InjectionToken} from '@angular/core';
9+
import {Injectable, InjectionToken, Optional, SkipSelf, FactoryProvider} from '@angular/core';
1010
import {DateAdapter} from '@angular/material/core';
1111
import {DateRange} from './date-selection-model';
1212

@@ -70,3 +70,17 @@ export class DefaultMatCalendarRangeStrategy<D> implements MatDateRangeSelection
7070
return new DateRange<D>(start, end);
7171
}
7272
}
73+
74+
75+
/** @docs-private */
76+
export function MAT_CALENDAR_RANGE_STRATEGY_PROVIDER_FACTORY(
77+
parent: MatDateRangeSelectionStrategy<unknown>, adapter: DateAdapter<unknown>) {
78+
return parent || new DefaultMatCalendarRangeStrategy(adapter);
79+
}
80+
81+
/** @docs-private */
82+
export const MAT_CALENDAR_RANGE_STRATEGY_PROVIDER: FactoryProvider = {
83+
provide: MAT_DATE_RANGE_SELECTION_STRATEGY,
84+
deps: [[new Optional(), new SkipSelf(), MAT_DATE_RANGE_SELECTION_STRATEGY], DateAdapter],
85+
useFactory: MAT_CALENDAR_RANGE_STRATEGY_PROVIDER_FACTORY,
86+
};

src/material/datepicker/datepicker-module.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ import {MatYearView} from './year-view';
3030
import {MatDateRangeInput} from './date-range-input';
3131
import {MatStartDate, MatEndDate} from './date-range-input-parts';
3232
import {MatDateRangePicker} from './date-range-picker';
33-
import {
34-
MAT_DATE_RANGE_SELECTION_STRATEGY,
35-
DefaultMatCalendarRangeStrategy
36-
} from './date-range-selection-strategy';
3733

3834

3935
@NgModule({
@@ -82,11 +78,7 @@ import {
8278
],
8379
providers: [
8480
MatDatepickerIntl,
85-
MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY_PROVIDER,
86-
{
87-
provide: MAT_DATE_RANGE_SELECTION_STRATEGY,
88-
useClass: DefaultMatCalendarRangeStrategy
89-
}
81+
MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY_PROVIDER
9082
],
9183
entryComponents: [
9284
MatDatepickerContent,

src/material/datepicker/public-api.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ export * from './datepicker-module';
1010
export * from './calendar';
1111
export * from './calendar-body';
1212
export * from './datepicker';
13-
export * from './date-range-selection-strategy';
13+
export {
14+
MAT_DATE_RANGE_SELECTION_STRATEGY,
15+
MatDateRangeSelectionStrategy,
16+
DefaultMatCalendarRangeStrategy,
17+
} from './date-range-selection-strategy';
1418
export * from './datepicker-animations';
1519
export {
1620
MAT_DATEPICKER_SCROLL_STRATEGY,

0 commit comments

Comments
 (0)