From 3c8bd21f9fdb3eef6793b22738b9e228afdd919d Mon Sep 17 00:00:00 2001 From: Serge Date: Tue, 23 Jul 2024 14:33:41 +0200 Subject: [PATCH] fix(ngx-date-picker): use inject --- .../datepicker-buttons.component.ts | 13 ++++++------ .../datepicker-with-time.component.ts | 20 +++++++++---------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/projects/date-picker/src/datepicker-buttons/datepicker-buttons.component.ts b/projects/date-picker/src/datepicker-buttons/datepicker-buttons.component.ts index 5783feb7..48d16518 100644 --- a/projects/date-picker/src/datepicker-buttons/datepicker-buttons.component.ts +++ b/projects/date-picker/src/datepicker-buttons/datepicker-buttons.component.ts @@ -1,16 +1,16 @@ import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion'; import { NgIf } from '@angular/common'; -import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output, ViewEncapsulation } from '@angular/core'; +import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, inject, Input, OnInit, Output, ViewEncapsulation } from '@angular/core'; import { AbstractControl } from '@angular/forms'; import { MatButtonModule } from '@angular/material/button'; import { DateAdapter } from '@angular/material/core'; -import { MatDateRangeInput, MatDateRangePicker, MatDatepicker } from '@angular/material/datepicker'; +import { MatDatepicker, MatDateRangeInput, MatDateRangePicker } from '@angular/material/datepicker'; import { MatFormFieldControl } from '@angular/material/form-field'; import { MatIconModule } from '@angular/material/icon'; import { MatTooltipModule } from '@angular/material/tooltip'; import { NgxDestroy } from '@hug/ngx-core'; import { set } from 'date-fns'; -import { ReplaySubject, filter, switchMap, takeUntil, tap } from 'rxjs'; +import { filter, ReplaySubject, switchMap, takeUntil, tap } from 'rxjs'; @Component({ selector: 'ngx-datepicker-buttons', @@ -32,6 +32,9 @@ export class NgxDatepickerButtonsComponent extends NgxDestroy implements OnInit @Input() public forInput!: MatFormFieldControl; @Input() public forPicker!: MatDatepicker | MatDateRangePicker; + protected changeDetectorRef = inject(ChangeDetectorRef); + protected dateAdater = inject>(DateAdapter); + private _hideToday = false; private _hideClear = false; private setToday$ = new ReplaySubject(1); @@ -59,10 +62,6 @@ export class NgxDatepickerButtonsComponent extends NgxDestroy implements OnInit return this._hideClear; } - public constructor(private changeDetectorRef: ChangeDetectorRef, private dateAdater: DateAdapter) { - super(); - } - public ngOnInit(): void { this.setToday$.pipe( filter(setToday => setToday && !!this.forPicker), diff --git a/projects/date-picker/src/datepicker-with-time/datepicker-with-time.component.ts b/projects/date-picker/src/datepicker-with-time/datepicker-with-time.component.ts index c9fc4410..1a834649 100644 --- a/projects/date-picker/src/datepicker-with-time/datepicker-with-time.component.ts +++ b/projects/date-picker/src/datepicker-with-time/datepicker-with-time.component.ts @@ -1,8 +1,8 @@ import { TemplatePortal } from '@angular/cdk/portal'; -import { AfterViewInit, ChangeDetectionStrategy, Component, ElementRef, Inject, OnDestroy, Optional, TemplateRef, ViewChild, ViewContainerRef, ViewEncapsulation } from '@angular/core'; +import { AfterViewInit, ChangeDetectionStrategy, Component, ElementRef, inject, OnDestroy, TemplateRef, ViewChild, ViewContainerRef, ViewEncapsulation } from '@angular/core'; import { MatButtonModule } from '@angular/material/button'; import { DateAdapter } from '@angular/material/core'; -import { MatDateSelectionModel, MatDatepicker, MatDatepickerInput } from '@angular/material/datepicker'; +import { MatDatepicker, MatDatepickerInput, MatDateSelectionModel } from '@angular/material/datepicker'; import { NgxDestroy } from '@hug/ngx-core'; import { DateOrDuration, TimePickerComponent } from '@hug/ngx-time-picker'; import { cloneDeep } from 'lodash-es'; @@ -32,19 +32,19 @@ export class NgxDatepickerWithTimeComponent extends NgxDestroy implements AfterV protected time?: DateOrDuration; + protected datepicker = inject>(MatDatepicker); + protected viewContainerRef = inject(ViewContainerRef); + protected globalModel = inject>(MatDateSelectionModel); + protected dateAdapter? = inject & DateAdapter>(DATE_TIME_ADAPTER, { optional: true }); + private portal?: TemplatePortal; - public constructor( - private datepicker: MatDatepicker, - private viewContainerRef: ViewContainerRef, - private globalModel: MatDateSelectionModel, - @Optional() @Inject(DATE_TIME_ADAPTER) private dateAdapter: DateTimeAdapter & DateAdapter - ) { + public constructor() { super(); - datepicker.openedStream.pipe( + this.datepicker.openedStream.pipe( tap(() => { - const datePickerInput = datepicker.datepickerInput as MatDatepickerInput; + const datePickerInput = this.datepicker.datepickerInput as MatDatepickerInput; this.time = datePickerInput.value ?? new Date(); }), delay(1),