Skip to content

Commit e6014cb

Browse files
committed
Run ng generate @angular/core:inject to migrate from constructor injection to inject()
1 parent f45f391 commit e6014cb

13 files changed

+75
-73
lines changed

projects/ng2-date-picker/src/lib/date-picker/date-picker.component.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ import {
3333
Renderer2,
3434
SimpleChanges,
3535
ViewChild,
36-
ViewEncapsulation
36+
ViewEncapsulation,
37+
inject
3738
} from '@angular/core';
3839
import {
3940
ControlValueAccessor,
@@ -83,6 +84,11 @@ export class DatePickerComponent implements OnChanges,
8384
ControlValueAccessor,
8485
Validator,
8586
OnDestroy {
87+
private readonly dayPickerService = inject(DatePickerService);
88+
private readonly renderer = inject(Renderer2);
89+
private readonly utilsService = inject(UtilsService);
90+
readonly cd = inject(ChangeDetectorRef);
91+
8692
isInitialized: boolean = false;
8793
@Input() config: IDatePickerConfig;
8894
@Input() mode: CalendarMode = 'day';
@@ -128,13 +134,6 @@ export class DatePickerComponent implements OnChanges,
128134
origin: ElementRef | HTMLElement;
129135
private onOpenDelayTimeoutHandler;
130136

131-
constructor(private readonly dayPickerService: DatePickerService,
132-
private readonly elemRef: ElementRef,
133-
private readonly renderer: Renderer2,
134-
private readonly utilsService: UtilsService,
135-
public readonly cd: ChangeDetectorRef) {
136-
}
137-
138137
get openOnFocus(): boolean {
139138
return this.componentConfig.openOnFocus;
140139
}

projects/ng2-date-picker/src/lib/date-picker/date-picker.directive.spec.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {UtilsService} from '../common/services/utils/utils.service';
22
import {DatePickerDirective} from './date-picker.directive';
3-
import {Component} from '@angular/core';
3+
import {Component, ComponentFactoryResolver, ElementRef, ViewContainerRef} from '@angular/core';
44
import {inject, TestBed} from '@angular/core/testing';
5+
import {NgControl} from '@angular/forms';
56

67
@Component({
78
template: '',
@@ -11,14 +12,23 @@ class TestComponent {
1112
}
1213

1314
describe('Directive: DpDayPicker', () => {
15+
let directive: DatePickerDirective;
16+
1417
beforeEach(() => {
1518
TestBed.configureTestingModule({
1619
declarations: [TestComponent],
17-
providers: [UtilsService]
20+
providers: [
21+
DatePickerDirective,
22+
UtilsService,
23+
{ provide: ViewContainerRef, useValue: null },
24+
{ provide: ElementRef, useValue: null },
25+
{ provide: ComponentFactoryResolver, useValue: null },
26+
{ provide: NgControl, useValue: null },
27+
]
1828
});
19-
});
2029

21-
const directive = new DatePickerDirective(null, null, null, null, null);
30+
directive = TestBed.inject(DatePickerDirective)
31+
});
2232

2333
it('should create an instance', () => {
2434
expect(directive).toBeTruthy();

projects/ng2-date-picker/src/lib/date-picker/date-picker.directive.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import {
1010
HostListener,
1111
Input,
1212
OnInit,
13-
Optional,
1413
Output,
15-
ViewContainerRef
14+
ViewContainerRef,
15+
inject
1616
} from '@angular/core';
1717
import {NgControl} from '@angular/forms';
1818
import {INavEvent} from '../common/models/navigation-event.model';
@@ -28,6 +28,12 @@ import {Dayjs} from 'dayjs';
2828
standalone: false
2929
})
3030
export class DatePickerDirective implements OnInit {
31+
readonly viewContainerRef = inject(ViewContainerRef);
32+
readonly elemRef = inject(ElementRef);
33+
readonly componentFactoryResolver = inject(ComponentFactoryResolver);
34+
readonly formControl: NgControl | undefined = inject(NgControl, { optional: true });
35+
readonly utilsService = inject(UtilsService);
36+
3137

3238
@Output() open = new EventEmitter<void>();
3339
@Output() close = new EventEmitter<void>();
@@ -39,13 +45,6 @@ export class DatePickerDirective implements OnInit {
3945
datePicker: DatePickerComponent;
4046
api: IDpDayPickerApi;
4147

42-
constructor(public readonly viewContainerRef: ViewContainerRef,
43-
public readonly elemRef: ElementRef,
44-
public readonly componentFactoryResolver: ComponentFactoryResolver,
45-
@Optional() public readonly formControl: NgControl,
46-
public readonly utilsService: UtilsService) {
47-
}
48-
4948
private _config: IDatePickerDirectiveConfig;
5049

5150
get config(): IDatePickerDirectiveConfig {

projects/ng2-date-picker/src/lib/date-picker/date-picker.service.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {EventEmitter, Injectable} from '@angular/core';
1+
import { EventEmitter, Injectable, inject } from '@angular/core';
22
import {IDatePickerConfig, IDatePickerConfigInternal} from './date-picker-config.model';
33

44
import {UtilsService} from '../common/services/utils/utils.service';
@@ -15,6 +15,10 @@ import {ConnectionPositionPair} from '@angular/cdk/overlay';
1515
providedIn: 'root'
1616
})
1717
export class DatePickerService {
18+
private readonly utilsService = inject(UtilsService);
19+
private readonly timeSelectService = inject(TimeSelectService);
20+
private readonly daytimeCalendarService = inject(DayTimeCalendarService);
21+
1822
readonly onPickerClosed: EventEmitter<null> = new EventEmitter();
1923
private defaultConfig: IDatePickerConfigInternal = {
2024
closeOnSelect: true,
@@ -32,11 +36,6 @@ export class DatePickerService {
3236
hideOnOutsideClick: true,
3337
};
3438

35-
constructor(private readonly utilsService: UtilsService,
36-
private readonly timeSelectService: TimeSelectService,
37-
private readonly daytimeCalendarService: DayTimeCalendarService) {
38-
}
39-
4039
// todo:: add unit tests
4140
getConfig(config: IDatePickerConfig, mode: CalendarMode = 'daytime'): IDatePickerConfigInternal {
4241
const _config = <IDatePickerConfigInternal>{

projects/ng2-date-picker/src/lib/day-calendar/day-calendar.component.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
Output,
1515
SimpleChange,
1616
SimpleChanges,
17-
ViewEncapsulation
17+
ViewEncapsulation,
18+
inject
1819
} from '@angular/core';
1920
import {DayCalendarService} from './day-calendar.service';
2021

@@ -59,6 +60,10 @@ import {dayjsRef} from '../common/dayjs/dayjs.ref';
5960
standalone: false
6061
})
6162
export class DayCalendarComponent implements OnInit, OnChanges, ControlValueAccessor, Validator {
63+
readonly dayCalendarService = inject(DayCalendarService);
64+
readonly utilsService = inject(UtilsService);
65+
readonly cd = inject(ChangeDetectorRef);
66+
6267

6368
@Input() config: IDayCalendarConfig;
6469
@Input() displayDate: SingleCalendarValue;
@@ -91,11 +96,6 @@ export class DayCalendarComponent implements OnInit, OnChanges, ControlValueAcce
9196
toggleCalendarMode: this.toggleCalendarMode.bind(this)
9297
};
9398

94-
constructor(public readonly dayCalendarService: DayCalendarService,
95-
public readonly utilsService: UtilsService,
96-
public readonly cd: ChangeDetectorRef) {
97-
}
98-
9999
_selected: Dayjs[];
100100

101101
get selected(): Dayjs[] {

projects/ng2-date-picker/src/lib/day-calendar/day-calendar.service.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Injectable} from '@angular/core';
1+
import { Injectable, inject } from '@angular/core';
22

33
import {WeekDays} from '../common/types/week-days.type';
44
import {UtilsService} from '../common/services/utils/utils.service';
@@ -12,6 +12,8 @@ import {dayjsRef} from '../common/dayjs/dayjs.ref';
1212
providedIn: 'root'
1313
})
1414
export class DayCalendarService {
15+
private utilsService = inject(UtilsService);
16+
1517
readonly DEFAULT_CONFIG: IDayCalendarConfig = {
1618
showNearMonthDays: true,
1719
showWeekNumbers: false,
@@ -26,9 +28,6 @@ export class DayCalendarService {
2628
};
2729
private readonly DAYS = ['su', 'mo', 'tu', 'we', 'th', 'fr', 'sa'];
2830

29-
constructor(private utilsService: UtilsService) {
30-
}
31-
3231
getConfig(config: IDayCalendarConfig): IDayCalendarConfigInternal {
3332
const _config = {
3433
...this.DEFAULT_CONFIG,

projects/ng2-date-picker/src/lib/day-time-calendar/day-time-calendar.component.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
Output,
1414
SimpleChanges,
1515
ViewChild,
16-
ViewEncapsulation
16+
ViewEncapsulation,
17+
inject
1718
} from '@angular/core';
1819
import {
1920
ControlValueAccessor,
@@ -59,6 +60,10 @@ import {INavEvent} from '../common/models/navigation-event.model';
5960
standalone: false
6061
})
6162
export class DayTimeCalendarComponent implements OnInit, OnChanges, ControlValueAccessor, Validator {
63+
dayTimeCalendarService = inject(DayTimeCalendarService);
64+
utilsService = inject(UtilsService);
65+
cd = inject(ChangeDetectorRef);
66+
6267

6368
@Input() config: IDayTimeCalendarConfig;
6469
@Input() displayDate: SingleCalendarValue;
@@ -79,11 +84,6 @@ export class DayTimeCalendarComponent implements OnInit, OnChanges, ControlValue
7984
moveCalendarTo: this.moveCalendarTo.bind(this)
8085
};
8186

82-
constructor(public dayTimeCalendarService: DayTimeCalendarService,
83-
public utilsService: UtilsService,
84-
public cd: ChangeDetectorRef) {
85-
}
86-
8787
_selected: Dayjs;
8888

8989
get selected(): Dayjs {

projects/ng2-date-picker/src/lib/day-time-calendar/day-time-calendar.service.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Injectable} from '@angular/core';
1+
import { Injectable, inject } from '@angular/core';
22

33
import {UtilsService} from '../common/services/utils/utils.service';
44
import {DayCalendarService} from '../day-calendar/day-calendar.service';
@@ -16,12 +16,11 @@ const COMBINED_FORMAT = DAY_FORMAT + TIME_FORMAT;
1616
providedIn: 'root'
1717
})
1818
export class DayTimeCalendarService {
19-
readonly DEFAULT_CONFIG: IDayTimeCalendarConfig = {};
19+
private utilsService = inject(UtilsService);
20+
private dayCalendarService = inject(DayCalendarService);
21+
private timeSelectService = inject(TimeSelectService);
2022

21-
constructor(private utilsService: UtilsService,
22-
private dayCalendarService: DayCalendarService,
23-
private timeSelectService: TimeSelectService) {
24-
}
23+
readonly DEFAULT_CONFIG: IDayTimeCalendarConfig = {};
2524

2625
getConfig(config: IDayTimeCalendarConfig): IDayTimeCalendarConfigInternal {
2726
const _config = {

projects/ng2-date-picker/src/lib/month-calendar/month-calendar.component.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
Output,
1313
SimpleChange,
1414
SimpleChanges,
15-
ViewEncapsulation
15+
ViewEncapsulation,
16+
inject
1617
} from '@angular/core';
1718
import {IMonth} from './month.model';
1819
import {MonthCalendarService} from './month-calendar.service';
@@ -56,6 +57,10 @@ import {dayjsRef} from '../common/dayjs/dayjs.ref';
5657
standalone: false
5758
})
5859
export class MonthCalendarComponent implements OnInit, OnChanges, ControlValueAccessor, Validator {
60+
readonly monthCalendarService = inject(MonthCalendarService);
61+
readonly utilsService = inject(UtilsService);
62+
readonly cd = inject(ChangeDetectorRef);
63+
5964

6065
@Input() config: IMonthCalendarConfig;
6166
@Input() displayDate: Dayjs | string;
@@ -86,11 +91,6 @@ export class MonthCalendarComponent implements OnInit, OnChanges, ControlValueAc
8691
moveCalendarTo: this.moveCalendarTo.bind(this)
8792
};
8893

89-
constructor(public readonly monthCalendarService: MonthCalendarService,
90-
public readonly utilsService: UtilsService,
91-
public readonly cd: ChangeDetectorRef) {
92-
}
93-
9494
_selected: Dayjs[];
9595

9696
get selected(): Dayjs[] {

projects/ng2-date-picker/src/lib/month-calendar/month-calendar.service.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Injectable} from '@angular/core';
1+
import { Injectable, inject } from '@angular/core';
22

33
import {UtilsService} from '../common/services/utils/utils.service';
44
import {IMonth} from './month.model';
@@ -12,6 +12,8 @@ import {dayjsRef} from '../common/dayjs/dayjs.ref';
1212
providedIn: 'root'
1313
})
1414
export class MonthCalendarService {
15+
private utilsService = inject(UtilsService);
16+
1517
readonly DEFAULT_CONFIG: IMonthCalendarConfigInternal = {
1618
allowMultiSelect: false,
1719
yearFormat: 'YYYY',
@@ -24,9 +26,6 @@ export class MonthCalendarService {
2426
numOfMonthRows: 3
2527
};
2628

27-
constructor(private utilsService: UtilsService) {
28-
}
29-
3029
getConfig(config: IMonthCalendarConfig): IMonthCalendarConfigInternal {
3130
const _config = <IMonthCalendarConfigInternal>{
3231
...this.DEFAULT_CONFIG,

0 commit comments

Comments
 (0)