Skip to content

Commit b1254f3

Browse files
committed
feat(material): use scoped injectables
1 parent 89ea485 commit b1254f3

28 files changed

+175
-253
lines changed

src/lib/autocomplete/autocomplete-module.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,14 @@ import {NgModule} from '@angular/core';
1010
import {CommonModule} from '@angular/common';
1111
import {OverlayModule} from '@angular/cdk/overlay';
1212
import {MatOptionModule, MatCommonModule} from '@angular/material/core';
13-
import {MatAutocomplete, MAT_AUTOCOMPLETE_DEFAULT_OPTIONS} from './autocomplete';
13+
import {MatAutocomplete} from './autocomplete';
1414
import {
1515
MatAutocompleteTrigger,
16-
MAT_AUTOCOMPLETE_SCROLL_STRATEGY_PROVIDER,
1716
} from './autocomplete-trigger';
1817

1918
@NgModule({
2019
imports: [MatOptionModule, OverlayModule, MatCommonModule, CommonModule],
2120
exports: [MatAutocomplete, MatOptionModule, MatAutocompleteTrigger, MatCommonModule],
2221
declarations: [MatAutocomplete, MatAutocompleteTrigger],
23-
providers: [
24-
MAT_AUTOCOMPLETE_SCROLL_STRATEGY_PROVIDER,
25-
{provide: MAT_AUTOCOMPLETE_DEFAULT_OPTIONS, useValue: false}
26-
],
2722
})
2823
export class MatAutocompleteModule {}

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,25 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {Directionality} from '@angular/cdk/bidi';
9-
import {DOWN_ARROW, ENTER, ESCAPE, UP_ARROW, TAB} from '@angular/cdk/keycodes';
9+
import {DOWN_ARROW, ENTER, ESCAPE, TAB, UP_ARROW} from '@angular/cdk/keycodes';
1010
import {
1111
FlexibleConnectedPositionStrategy,
1212
Overlay,
13-
OverlayRef,
1413
OverlayConfig,
14+
OverlayRef,
1515
PositionStrategy,
1616
ScrollStrategy,
1717
} from '@angular/cdk/overlay';
1818
import {TemplatePortal} from '@angular/cdk/portal';
19-
import {filter} from 'rxjs/operators/filter';
20-
import {take} from 'rxjs/operators/take';
21-
import {switchMap} from 'rxjs/operators/switchMap';
22-
import {tap} from 'rxjs/operators/tap';
23-
import {delay} from 'rxjs/operators/delay';
19+
import {DOCUMENT} from '@angular/common';
2420
import {
2521
ChangeDetectorRef,
2622
Directive,
2723
ElementRef,
2824
forwardRef,
2925
Host,
3026
Inject,
27+
inject,
3128
InjectionToken,
3229
Input,
3330
NgZone,
@@ -37,19 +34,23 @@ import {
3734
} from '@angular/core';
3835
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
3936
import {
37+
_countGroupLabelsBeforeOption,
38+
_getOptionScrollPosition,
4039
MatOption,
4140
MatOptionSelectionChange,
42-
_getOptionScrollPosition,
43-
_countGroupLabelsBeforeOption,
4441
} from '@angular/material/core';
4542
import {MatFormField} from '@angular/material/form-field';
46-
import {DOCUMENT} from '@angular/common';
4743
import {Observable} from 'rxjs/Observable';
48-
import {Subject} from 'rxjs/Subject';
4944
import {defer} from 'rxjs/observable/defer';
5045
import {fromEvent} from 'rxjs/observable/fromEvent';
5146
import {merge} from 'rxjs/observable/merge';
5247
import {of as observableOf} from 'rxjs/observable/of';
48+
import {delay} from 'rxjs/operators/delay';
49+
import {filter} from 'rxjs/operators/filter';
50+
import {switchMap} from 'rxjs/operators/switchMap';
51+
import {take} from 'rxjs/operators/take';
52+
import {tap} from 'rxjs/operators/tap';
53+
import {Subject} from 'rxjs/Subject';
5354
import {Subscription} from 'rxjs/Subscription';
5455
import {MatAutocomplete} from './autocomplete';
5556

@@ -68,20 +69,13 @@ export const AUTOCOMPLETE_PANEL_HEIGHT = 256;
6869

6970
/** Injection token that determines the scroll handling while the autocomplete panel is open. */
7071
export const MAT_AUTOCOMPLETE_SCROLL_STRATEGY =
71-
new InjectionToken<() => ScrollStrategy>('mat-autocomplete-scroll-strategy');
72-
73-
/** @docs-private */
74-
export function MAT_AUTOCOMPLETE_SCROLL_STRATEGY_PROVIDER_FACTORY(overlay: Overlay):
75-
() => ScrollStrategy {
76-
return () => overlay.scrollStrategies.reposition();
77-
}
78-
79-
/** @docs-private */
80-
export const MAT_AUTOCOMPLETE_SCROLL_STRATEGY_PROVIDER = {
81-
provide: MAT_AUTOCOMPLETE_SCROLL_STRATEGY,
82-
deps: [Overlay],
83-
useFactory: MAT_AUTOCOMPLETE_SCROLL_STRATEGY_PROVIDER_FACTORY,
84-
};
72+
new InjectionToken<() => ScrollStrategy>('mat-autocomplete-scroll-strategy', {
73+
providedIn: 'root',
74+
factory: () => {
75+
const overlay = inject(Overlay);
76+
return () => overlay.scrollStrategies.reposition();
77+
}
78+
});
8579

8680
/**
8781
* Provider that allows the autocomplete to register as a ControlValueAccessor.

src/lib/autocomplete/autocomplete.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ export interface MatAutocompleteDefaultOptions {
6161

6262
/** Injection token to be used to override the default options for `mat-autocomplete`. */
6363
export const MAT_AUTOCOMPLETE_DEFAULT_OPTIONS =
64-
new InjectionToken<MatAutocompleteDefaultOptions>('mat-autocomplete-default-options');
64+
new InjectionToken<MatAutocompleteDefaultOptions>('mat-autocomplete-default-options', {
65+
providedIn: 'root',
66+
factory: () => ({autoActiveFirstOption: false}),
67+
});
6568

6669

6770
@Component({

src/lib/core/common-behaviors/common-module.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {BidiModule} from '@angular/cdk/bidi';
1111

1212

1313
/** Injection token that configures whether the Material sanity checks are enabled. */
14-
export const MATERIAL_SANITY_CHECKS = new InjectionToken<boolean>('mat-sanity-checks');
14+
export const MATERIAL_SANITY_CHECKS = new InjectionToken<boolean>('mat-sanity-checks', {
15+
providedIn: 'root',
16+
factory: () => true,
17+
});
1518

1619

1720
/**
@@ -23,9 +26,6 @@ export const MATERIAL_SANITY_CHECKS = new InjectionToken<boolean>('mat-sanity-ch
2326
@NgModule({
2427
imports: [BidiModule],
2528
exports: [BidiModule],
26-
providers: [{
27-
provide: MATERIAL_SANITY_CHECKS, useValue: true,
28-
}],
2929
})
3030
export class MatCommonModule {
3131
/** Whether we've done the global sanity checks (e.g. a theme is loaded, there is a doctype). */

src/lib/core/datetime/date-adapter.ts

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

9-
import {InjectionToken, LOCALE_ID} from '@angular/core';
9+
import {inject, InjectionToken, LOCALE_ID} from '@angular/core';
1010
import {Observable} from 'rxjs/Observable';
1111
import {Subject} from 'rxjs/Subject';
1212

1313

1414
/** InjectionToken for datepicker that can be used to override default locale code. */
15-
export const MAT_DATE_LOCALE = new InjectionToken<string>('MAT_DATE_LOCALE');
16-
17-
/** Provider for MAT_DATE_LOCALE injection token. */
18-
export const MAT_DATE_LOCALE_PROVIDER = {provide: MAT_DATE_LOCALE, useExisting: LOCALE_ID};
15+
export const MAT_DATE_LOCALE = new InjectionToken<string>('MAT_DATE_LOCALE', {
16+
providedIn: 'root',
17+
factory: () => inject(LOCALE_ID)
18+
});
1919

2020
/** Adapts type `D` to be usable as a date by cdk-based components that work with dates. */
2121
export abstract class DateAdapter<D> {

src/lib/core/datetime/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {PlatformModule} from '@angular/cdk/platform';
1010
import {NgModule} from '@angular/core';
11-
import {DateAdapter, MAT_DATE_LOCALE_PROVIDER} from './date-adapter';
11+
import {DateAdapter} from './date-adapter';
1212
import {MAT_DATE_FORMATS} from './date-formats';
1313
import {NativeDateAdapter} from './native-date-adapter';
1414
import {MAT_NATIVE_DATE_FORMATS} from './native-date-formats';
@@ -23,7 +23,6 @@ export * from './native-date-formats';
2323
imports: [PlatformModule],
2424
providers: [
2525
{provide: DateAdapter, useClass: NativeDateAdapter},
26-
MAT_DATE_LOCALE_PROVIDER
2726
],
2827
})
2928
export class NativeDateModule {}

src/lib/core/error/error-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class ShowOnDirtyErrorStateMatcher implements ErrorStateMatcher {
1818
}
1919

2020
/** Provider that defines how form controls behave with regards to displaying error messages. */
21-
@Injectable()
21+
@Injectable({providedIn: 'root'})
2222
export class ErrorStateMatcher {
2323
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
2424
return !!(control && control.invalid && (control.touched || (form && form.submitted)));

src/lib/datepicker/datepicker-intl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {Subject} from 'rxjs/Subject';
1111

1212

1313
/** Datepicker data that requires internationalization. */
14-
@Injectable()
14+
@Injectable({providedIn: 'root'})
1515
export class MatDatepickerIntl {
1616
/**
1717
* Stream that emits whenever the labels here are changed. Use this to notify

src/lib/datepicker/datepicker-module.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ import {MatButtonModule} from '@angular/material/button';
1414
import {MatDialogModule} from '@angular/material/dialog';
1515
import {MatCalendar} from './calendar';
1616
import {MatCalendarBody} from './calendar-body';
17-
import {
18-
MAT_DATEPICKER_SCROLL_STRATEGY_PROVIDER,
19-
MatDatepicker,
20-
MatDatepickerContent,
21-
} from './datepicker';
17+
import {MatDatepicker, MatDatepickerContent} from './datepicker';
2218
import {MatDatepickerInput} from './datepicker-input';
2319
import {MatDatepickerIntl} from './datepicker-intl';
2420
import {MatDatepickerToggle, MatDatepickerToggleIcon} from './datepicker-toggle';
@@ -61,7 +57,6 @@ import {MatYearView} from './year-view';
6157
],
6258
providers: [
6359
MatDatepickerIntl,
64-
MAT_DATEPICKER_SCROLL_STRATEGY_PROVIDER,
6560
],
6661
entryComponents: [
6762
MatDatepickerContent,

src/lib/datepicker/datepicker.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {ENTER, ESCAPE, RIGHT_ARROW} from '@angular/cdk/keycodes';
2-
import {OverlayContainer, Overlay, ScrollDispatcher} from '@angular/cdk/overlay';
2+
import {Overlay, OverlayContainer, ScrollDispatcher} from '@angular/cdk/overlay';
33
import {dispatchFakeEvent, dispatchKeyboardEvent, dispatchMouseEvent} from '@angular/cdk/testing';
44
import {Component, ViewChild} from '@angular/core';
5-
import {fakeAsync, flush, ComponentFixture, inject, TestBed} from '@angular/core/testing';
5+
import {ComponentFixture, fakeAsync, flush, inject, TestBed} from '@angular/core/testing';
66
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
77
import {
88
DEC,
@@ -14,15 +14,15 @@ import {
1414
NativeDateModule,
1515
SEP,
1616
} from '@angular/material/core';
17-
import {MatFormFieldModule, MatFormField} from '@angular/material/form-field';
17+
import {MatFormField, MatFormFieldModule} from '@angular/material/form-field';
1818
import {By} from '@angular/platform-browser';
1919
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
20+
import {Subject} from 'rxjs/Subject';
2021
import {MatInputModule} from '../input/index';
2122
import {MatDatepicker} from './datepicker';
2223
import {MatDatepickerInput} from './datepicker-input';
2324
import {MatDatepickerToggle} from './datepicker-toggle';
24-
import {MatDatepickerIntl, MatDatepickerModule, MAT_DATEPICKER_SCROLL_STRATEGY} from './index';
25-
import {Subject} from 'rxjs/Subject';
25+
import {MAT_DATEPICKER_SCROLL_STRATEGY, MatDatepickerIntl, MatDatepickerModule} from './index';
2626

2727
describe('MatDatepicker', () => {
2828
const SUPPORTS_INTL = typeof Intl != 'undefined';

src/lib/datepicker/datepicker.ts

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,67 +10,60 @@ import {Directionality} from '@angular/cdk/bidi';
1010
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1111
import {ESCAPE} from '@angular/cdk/keycodes';
1212
import {
13+
FlexibleConnectedPositionStrategy,
1314
Overlay,
1415
OverlayConfig,
1516
OverlayRef,
1617
PositionStrategy,
17-
RepositionScrollStrategy,
1818
ScrollStrategy,
19-
FlexibleConnectedPositionStrategy,
2019
} from '@angular/cdk/overlay';
2120
import {ComponentPortal} from '@angular/cdk/portal';
22-
import {take} from 'rxjs/operators/take';
23-
import {filter} from 'rxjs/operators/filter';
21+
import {DOCUMENT} from '@angular/common';
2422
import {
2523
AfterContentInit,
2624
ChangeDetectionStrategy,
25+
ChangeDetectorRef,
2726
Component,
2827
ComponentRef,
2928
ElementRef,
3029
EventEmitter,
3130
Inject,
31+
inject,
3232
InjectionToken,
3333
Input,
3434
NgZone,
3535
OnDestroy,
36+
OnInit,
3637
Optional,
3738
Output,
3839
ViewChild,
3940
ViewContainerRef,
4041
ViewEncapsulation,
41-
ChangeDetectorRef,
42-
OnInit,
4342
} from '@angular/core';
4443
import {CanColor, DateAdapter, mixinColor, ThemePalette} from '@angular/material/core';
4544
import {MatDialog, MatDialogRef} from '@angular/material/dialog';
46-
import {DOCUMENT} from '@angular/common';
45+
import {merge} from 'rxjs/observable/merge';
46+
import {filter} from 'rxjs/operators/filter';
47+
import {take} from 'rxjs/operators/take';
4748
import {Subject} from 'rxjs/Subject';
4849
import {Subscription} from 'rxjs/Subscription';
49-
import {merge} from 'rxjs/observable/merge';
50-
import {createMissingDateImplError} from './datepicker-errors';
51-
import {MatDatepickerInput} from './datepicker-input';
5250
import {MatCalendar} from './calendar';
5351
import {matDatepickerAnimations} from './datepicker-animations';
52+
import {createMissingDateImplError} from './datepicker-errors';
53+
import {MatDatepickerInput} from './datepicker-input';
5454

5555
/** Used to generate a unique ID for each datepicker instance. */
5656
let datepickerUid = 0;
5757

5858
/** Injection token that determines the scroll handling while the calendar is open. */
5959
export const MAT_DATEPICKER_SCROLL_STRATEGY =
60-
new InjectionToken<() => ScrollStrategy>('mat-datepicker-scroll-strategy');
61-
62-
/** @docs-private */
63-
export function MAT_DATEPICKER_SCROLL_STRATEGY_PROVIDER_FACTORY(overlay: Overlay):
64-
() => RepositionScrollStrategy {
65-
return () => overlay.scrollStrategies.reposition();
66-
}
67-
68-
/** @docs-private */
69-
export const MAT_DATEPICKER_SCROLL_STRATEGY_PROVIDER = {
70-
provide: MAT_DATEPICKER_SCROLL_STRATEGY,
71-
deps: [Overlay],
72-
useFactory: MAT_DATEPICKER_SCROLL_STRATEGY_PROVIDER_FACTORY,
73-
};
60+
new InjectionToken<() => ScrollStrategy>('mat-datepicker-scroll-strategy', {
61+
providedIn: 'root',
62+
factory: () => {
63+
const overlay = inject(Overlay);
64+
return () => overlay.scrollStrategies.reposition();
65+
}
66+
});
7467

7568
// Boilerplate for applying mixins to MatDatepickerContent.
7669
/** @docs-private */

src/lib/dialog/dialog.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ import {Directionality} from '@angular/cdk/bidi';
1010
import {
1111
Overlay,
1212
OverlayConfig,
13+
OverlayContainer,
1314
OverlayRef,
1415
ScrollStrategy,
15-
OverlayContainer,
1616
} from '@angular/cdk/overlay';
1717
import {ComponentPortal, ComponentType, PortalInjector, TemplatePortal} from '@angular/cdk/portal';
1818
import {Location} from '@angular/common';
1919
import {
2020
ComponentRef,
2121
Inject,
22+
inject,
2223
Injectable,
2324
InjectionToken,
2425
Injector,
@@ -44,7 +45,13 @@ export const MAT_DIALOG_DEFAULT_OPTIONS =
4445

4546
/** Injection token that determines the scroll handling while the dialog is open. */
4647
export const MAT_DIALOG_SCROLL_STRATEGY =
47-
new InjectionToken<() => ScrollStrategy>('mat-dialog-scroll-strategy');
48+
new InjectionToken<() => ScrollStrategy>('mat-dialog-scroll-strategy', {
49+
providedIn: 'root',
50+
factory: () => {
51+
const overlay = inject(Overlay);
52+
return () => overlay.scrollStrategies.block();
53+
}
54+
});
4855

4956
/** @docs-private */
5057
export function MAT_DIALOG_SCROLL_STRATEGY_PROVIDER_FACTORY(overlay: Overlay):

src/lib/icon/icon-registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class SvgIconConfig {
7676
* - Registers aliases for CSS classes, for use with icon fonts.
7777
* - Loads icons from URLs and extracts individual icons from icon sets.
7878
*/
79-
@Injectable()
79+
@Injectable({providedIn: 'root'})
8080
export class MatIconRegistry {
8181
private _document: Document;
8282

0 commit comments

Comments
 (0)