Skip to content

Commit

Permalink
fix(ngx-date-picker): use inject
Browse files Browse the repository at this point in the history
  • Loading branch information
vapkse committed Jul 23, 2024
1 parent 8b34653 commit 3c8bd21
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -32,6 +32,9 @@ export class NgxDatepickerButtonsComponent extends NgxDestroy implements OnInit
@Input() public forInput!: MatFormFieldControl<unknown>;
@Input() public forPicker!: MatDatepicker<unknown> | MatDateRangePicker<Date>;

protected changeDetectorRef = inject(ChangeDetectorRef);
protected dateAdater = inject<DateAdapter<unknown>>(DateAdapter);

private _hideToday = false;
private _hideClear = false;
private setToday$ = new ReplaySubject<boolean>(1);
Expand Down Expand Up @@ -59,10 +62,6 @@ export class NgxDatepickerButtonsComponent extends NgxDestroy implements OnInit
return this._hideClear;
}

public constructor(private changeDetectorRef: ChangeDetectorRef, private dateAdater: DateAdapter<unknown>) {
super();
}

public ngOnInit(): void {
this.setToday$.pipe(
filter(setToday => setToday && !!this.forPicker),
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -32,19 +32,19 @@ export class NgxDatepickerWithTimeComponent extends NgxDestroy implements AfterV

protected time?: DateOrDuration;

protected datepicker = inject<MatDatepicker<unknown>>(MatDatepicker);
protected viewContainerRef = inject(ViewContainerRef);
protected globalModel = inject<MatDateSelectionModel<unknown, unknown>>(MatDateSelectionModel);
protected dateAdapter? = inject<DateTimeAdapter<unknown> & DateAdapter<unknown>>(DATE_TIME_ADAPTER, { optional: true });

private portal?: TemplatePortal;

public constructor(
private datepicker: MatDatepicker<unknown>,
private viewContainerRef: ViewContainerRef,
private globalModel: MatDateSelectionModel<unknown, unknown>,
@Optional() @Inject(DATE_TIME_ADAPTER) private dateAdapter: DateTimeAdapter<unknown> & DateAdapter<unknown>
) {
public constructor() {
super();

datepicker.openedStream.pipe(
this.datepicker.openedStream.pipe(
tap(() => {
const datePickerInput = datepicker.datepickerInput as MatDatepickerInput<Date>;
const datePickerInput = this.datepicker.datepickerInput as MatDatepickerInput<Date>;
this.time = datePickerInput.value ?? new Date();
}),
delay(1),
Expand Down

0 comments on commit 3c8bd21

Please sign in to comment.