• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IgniteUI / igniteui-angular / 16751757744

05 Aug 2025 01:40PM UTC coverage: 91.436% (-0.004%) from 91.44%
16751757744

Pull #16115

github

web-flow
Merge ba9ac869e into 6d3657091
Pull Request #16115: Clear icon enhancements for IgxDateRangePicker

13463 of 15805 branches covered (85.18%)

23 of 24 new or added lines in 2 files covered. (95.83%)

2 existing lines in 1 file now uncovered.

27184 of 29730 relevant lines covered (91.44%)

34495.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.83
/projects/igniteui-angular/src/lib/date-range-picker/date-range-picker.component.ts
1
import {
2
    AfterViewInit, booleanAttribute, ChangeDetectorRef, Component, ContentChild, ContentChildren, ElementRef,
3
    EventEmitter, HostBinding, HostListener, Inject, Injector, Input, LOCALE_ID,
4
    OnChanges, OnDestroy, OnInit, Optional, Output, QueryList,
5
    SimpleChanges, TemplateRef, ViewChild, ViewContainerRef
6
} from '@angular/core';
7
import { NgTemplateOutlet, getLocaleFirstDayOfWeek } from '@angular/common';
8
import {
9
    AbstractControl, ControlValueAccessor, NgControl,
10
    NG_VALIDATORS, NG_VALUE_ACCESSOR, ValidationErrors, Validator
11
} from '@angular/forms';
12

13
import { fromEvent, merge, MonoTypeOperatorFunction, noop, Subscription } from 'rxjs';
14
import { filter, takeUntil } from 'rxjs/operators';
15

16
import { CalendarSelection, IgxCalendarComponent } from '../calendar/public_api';
17
import { DateRangeType } from '../core/dates';
18
import { DateRangePickerResourceStringsEN, IDateRangePickerResourceStrings } from '../core/i18n/date-range-picker-resources';
19
import { IBaseCancelableBrowserEventArgs, isDate, parseDate, PlatformUtil } from '../core/utils';
20
import { IgxCalendarContainerComponent } from '../date-common/calendar-container/calendar-container.component';
21
import { PickerBaseDirective } from '../date-common/picker-base.directive';
22
import { IgxPickerActionsDirective, IgxPickerClearComponent } from '../date-common/picker-icons.common';
23
import { DateTimeUtil } from '../date-common/util/date-time.util';
24
import { IgxOverlayOutletDirective } from '../directives/toggle/toggle.directive';
25
import {
26
    IgxInputDirective, IgxInputGroupComponent, IgxInputGroupType, IgxInputState,
27
    IgxLabelDirective, IGX_INPUT_GROUP_TYPE, IgxSuffixDirective
28
} from '../input-group/public_api';
29
import {
30
    AutoPositionStrategy, IgxOverlayService, OverlayCancelableEventArgs, OverlayEventArgs,
31
    OverlaySettings, PositionSettings
32
} from '../services/public_api';
33
import { DateRange, IgxDateRangeEndComponent, IgxDateRangeInputsBaseComponent, IgxDateRangeSeparatorDirective, IgxDateRangeStartComponent, DateRangePickerFormatPipe } from './date-range-picker-inputs.common';
34
import { IgxPrefixDirective } from '../directives/prefix/prefix.directive';
35
import { IgxIconComponent } from '../icon/icon.component';
36
import { getCurrentResourceStrings } from '../core/i18n/resources';
37
import { fadeIn, fadeOut } from 'igniteui-angular/animations';
38

39
const SingleInputDatesConcatenationString = ' - ';
3✔
40

41
/**
42
 * Provides the ability to select a range of dates from a calendar UI or editable inputs.
43
 *
44
 * @igxModule IgxDateRangeModule
45
 *
46
 * @igxTheme igx-input-group-theme, igx-calendar-theme, igx-date-range-picker-theme
47
 *
48
 * @igxKeywords date, range, date range, date picker
49
 *
50
 * @igxGroup scheduling
51
 *
52
 * @remarks
53
 * It displays the range selection in a single or two input fields.
54
 * The default template displays a single *readonly* input field
55
 * while projecting `igx-date-range-start` and `igx-date-range-end`
56
 * displays two *editable* input fields.
57
 *
58
 * @example
59
 * ```html
60
 * <igx-date-range-picker mode="dropdown"></igx-date-range-picker>
61
 * ```
62
 */
63
@Component({
64
    selector: 'igx-date-range-picker',
65
    templateUrl: './date-range-picker.component.html',
66
    providers: [
67
        { provide: NG_VALUE_ACCESSOR, useExisting: IgxDateRangePickerComponent, multi: true },
68
        { provide: NG_VALIDATORS, useExisting: IgxDateRangePickerComponent, multi: true }
69
    ],
70
    imports: [
71
        NgTemplateOutlet,
72
        IgxIconComponent,
73
        IgxInputGroupComponent,
74
        IgxInputDirective,
75
        IgxPrefixDirective,
76
        IgxSuffixDirective,
77
        DateRangePickerFormatPipe
78
    ]
79
})
80
export class IgxDateRangePickerComponent extends PickerBaseDirective
3✔
81
    implements OnChanges, OnInit, AfterViewInit, OnDestroy, ControlValueAccessor, Validator {
82

83
    /**
84
     * The number of displayed month views.
85
     *
86
     * @remarks
87
     * Default is `2`.
88
     *
89
     * @example
90
     * ```html
91
     * <igx-date-range-picker [displayMonthsCount]="3"></igx-date-range-picker>
92
     * ```
93
     */
94
    @Input()
95
    public displayMonthsCount = 2;
90✔
96

97
    /**
98
     * Gets/Sets whether dates that are not part of the current month will be displayed.
99
     *
100
     * @remarks
101
     * Default value is `false`.
102
     *
103
     * @example
104
     * ```html
105
     * <igx-date-range-picker [hideOutsideDays]="true"></igx-date-range-picker>
106
     * ```
107
     */
108
    @Input({ transform: booleanAttribute })
109
    public hideOutsideDays: boolean;
110

111
    /**
112
     * A custom formatter function, applied on the selected or passed in date.
113
     *
114
     * @example
115
     * ```typescript
116
     * private dayFormatter = new Intl.DateTimeFormat("en", { weekday: "long" });
117
     * private monthFormatter = new Intl.DateTimeFormat("en", { month: "long" });
118
     *
119
     * public formatter(date: Date): string {
120
     *  return `${this.dayFormatter.format(date)} - ${this.monthFormatter.format(date)} - ${date.getFullYear()}`;
121
     * }
122
     * ```
123
     * ```html
124
     * <igx-date-range-picker [formatter]="formatter"></igx-date-range-picker>
125
     * ```
126
     */
127
    @Input()
128
    public formatter: (val: DateRange) => string;
129

130
    /**
131
     * Overrides the default text of the calendar dialog **Done** button.
132
     *
133
     * @remarks
134
     * Defaults to the value from resource strings, `"Done"` for the built-in EN.
135
     * The button will only show up in `dialog` mode.
136
     *
137
     * @example
138
     * ```html
139
     * <igx-date-range-picker doneButtonText="完了"></igx-date-range-picker>
140
     * ```
141
     */
142
    @Input()
143
    public set doneButtonText(value: string) {
144
        this._doneButtonText = value;
1✔
145
    }
146

147
    public get doneButtonText(): string {
148
        if (this._doneButtonText === null) {
7✔
149
            return this.resourceStrings.igx_date_range_picker_done_button;
6✔
150
        }
151
        return this._doneButtonText;
1✔
152
    }
153
    /**
154
     * Custom overlay settings that should be used to display the calendar.
155
     *
156
     * @example
157
     * ```html
158
     * <igx-date-range-picker [overlaySettings]="customOverlaySettings"></igx-date-range-picker>
159
     * ```
160
     */
161
    @Input()
162
    public override overlaySettings: OverlaySettings;
163

164
    /**
165
     * The format used when editable inputs are not focused.
166
     *
167
     * @remarks
168
     * Uses Angular's DatePipe.
169
     *
170
     * @example
171
     * ```html
172
     * <igx-date-range-picker displayFormat="EE/M/yy"></igx-date-range-picker>
173
     * ```
174
     *
175
     */
176
    @Input()
177
    public override displayFormat: string;
178

179
    /**
180
     * The expected user input format and placeholder.
181
     *
182
     * @example
183
     * ```html
184
     * <igx-date-range-picker inputFormat="dd/MM/yy"></igx-date-range-picker>
185
     * ```
186
     */
187
    @Input()
188
    public override inputFormat: string;
189

190
    /**
191
     * The minimum value in a valid range.
192
     *
193
     * @example
194
     * <igx-date-range-picker [minValue]="minDate"></igx-date-range-picker>
195
     */
196
    @Input()
197
    public set minValue(value: Date | string) {
198
        this._minValue = value;
33✔
199
        this.onValidatorChange();
33✔
200
    }
201

202
    public get minValue(): Date | string {
203
        return this._minValue;
103✔
204
    }
205

206
    /**
207
     * The maximum value in a valid range.
208
     *
209
     * @example
210
     * <igx-date-range-picker [maxValue]="maxDate"></igx-date-range-picker>
211
     */
212
    @Input()
213
    public set maxValue(value: Date | string) {
214
        this._maxValue = value;
33✔
215
        this.onValidatorChange();
33✔
216
    }
217

218
    public get maxValue(): Date | string {
219
        return this._maxValue;
103✔
220
    }
221

222
    /**
223
     * An accessor that sets the resource strings.
224
     * By default it uses EN resources.
225
     */
226
    @Input()
227
    public set resourceStrings(value: IDateRangePickerResourceStrings) {
228
        this._resourceStrings = Object.assign({}, this._resourceStrings, value);
×
229
    }
230

231
    /**
232
     * An accessor that returns the resource strings.
233
     */
234
    public get resourceStrings(): IDateRangePickerResourceStrings {
235
        return this._resourceStrings;
74✔
236
    }
237

238
    /**
239
     * Sets the `placeholder` for single-input `IgxDateRangePickerComponent`.
240
     *
241
     *   @example
242
     * ```html
243
     * <igx-date-range-picker [placeholder]="'Choose your dates'"></igx-date-range-picker>
244
     * ```
245
     */
246
    @Input()
247
    public override placeholder = '';
90✔
248

249
    /**
250
     * Gets/Sets the container used for the popup element.
251
     *
252
     * @remarks
253
     *  `outlet` is an instance of `IgxOverlayOutletDirective` or an `ElementRef`.
254
     * @example
255
     * ```html
256
     * <div igxOverlayOutlet #outlet="overlay-outlet"></div>
257
     * //..
258
     * <igx-date-range-picker [outlet]="outlet"></igx-date-range-picker>
259
     * //..
260
     * ```
261
     */
262
    @Input()
263
    public override outlet: IgxOverlayOutletDirective | ElementRef<any>;
264

265
    /**
266
     * Show/hide week numbers
267
     *
268
     * @remarks
269
     * Default is `false`.
270
     *
271
     * @example
272
     * ```html
273
     * <igx-date-range-picker [showWeekNumbers]="true"></igx-date-range-picker>
274
     * ``
275
     */
276
    @Input({ transform: booleanAttribute })
277
    public showWeekNumbers = false;
90✔
278

279
    /**
280
     * Emitted when the picker's value changes. Used for two-way binding.
281
     *
282
     * @example
283
     * ```html
284
     * <igx-date-range-picker [(value)]="date"></igx-date-range-picker>
285
     * ```
286
     */
287
    @Output()
288
    public valueChange = new EventEmitter<DateRange>();
90✔
289

290
    /** @hidden @internal */
291
    @HostBinding('class.igx-date-range-picker')
292
    public cssClass = 'igx-date-range-picker';
90✔
293

294
    @ViewChild(IgxInputGroupComponent, { read: ViewContainerRef })
295
    private viewContainerRef: ViewContainerRef;
296

297
    /** @hidden @internal */
298
    @ViewChild(IgxInputDirective)
299
    public inputDirective: IgxInputDirective;
300

301
    /** @hidden @internal */
302
    @ContentChildren(IgxDateRangeInputsBaseComponent)
303
    public projectedInputs: QueryList<IgxDateRangeInputsBaseComponent>;
304

305
    @ContentChild(IgxLabelDirective)
306
    public label: IgxLabelDirective;
307

308
    @ContentChild(IgxPickerActionsDirective)
309
    public pickerActions: IgxPickerActionsDirective;
310

311
    /** @hidden @internal */
312
    @ContentChild(IgxDateRangeSeparatorDirective, { read: TemplateRef })
313
    public dateSeparatorTemplate: TemplateRef<any>;
314

315
    /** @hidden @internal */
316
    public get dateSeparator(): string {
317
        if (this._dateSeparator === null) {
68✔
318
            return this.resourceStrings.igx_date_range_picker_date_separator;
68✔
319
        }
320
        return this._dateSeparator;
×
321
    }
322

323
    /** @hidden @internal */
324
    public get appliedFormat(): string {
325
        return DateTimeUtil.getLocaleDateFormat(this.locale, this.displayFormat)
710✔
326
            || DateTimeUtil.DEFAULT_INPUT_FORMAT;
327
    }
328

329
    /**
330
     * @example
331
     * ```html
332
     * <igx-date-range-picker locale="jp"></igx-date-range-picker>
333
     * ```
334
     */
335
    /**
336
     * Gets the `locale` of the date-range-picker.
337
     * If not set, defaults to application's locale.
338
     */
339
    @Input()
340
    public override get locale(): string {
341
        return this._locale;
1,400✔
342
    }
343

344
    /**
345
     * Sets the `locale` of the date-picker.
346
     * Expects a valid BCP 47 language tag.
347
     */
348
    public override set locale(value: string) {
349
        this._locale = value;
274✔
350
        // if value is invalid, set it back to _localeId
351
        try {
274✔
352
            getLocaleFirstDayOfWeek(this._locale);
274✔
353
        } catch (e) {
354
            this._locale = this._localeId;
1✔
355
        }
356
        if (this.hasProjectedInputs) {
274✔
357
            this.updateInputLocale();
3✔
358
            this.updateDisplayFormat();
3✔
359
        }
360
    }
361

362
    /** @hidden @internal */
363
    public get singleInputFormat(): string {
364
        if (this.placeholder !== '') {
333✔
365
            return this.placeholder;
2✔
366
        }
367

368
        const format = this.appliedFormat;
331✔
369
        return `${format}${SingleInputDatesConcatenationString}${format}`;
331✔
370
    }
371

372
    /**
373
     * Gets calendar state.
374
     *
375
     * ```typescript
376
     * let state = this.dateRange.collapsed;
377
     * ```
378
     */
379
    public override get collapsed(): boolean {
380
        return this._collapsed;
535✔
381
    }
382

383
    /**
384
     * The currently selected value / range from the calendar
385
     *
386
     * @remarks
387
     * The current value is of type `DateRange`
388
     *
389
     * @example
390
     * ```typescript
391
     * const newValue: DateRange = { start: new Date("2/2/2012"), end: new Date("3/3/2013")};
392
     * this.dateRangePicker.value = newValue;
393
     * ```
394
     */
395
    public get value(): DateRange | null {
396
        return this._value;
1,519✔
397
    }
398

399
    @Input()
400
    public set value(value: DateRange | null) {
401
        this.updateValue(value);
83✔
402
        this.onChangeCallback(value);
83✔
403
        this.valueChange.emit(value);
83✔
404
    }
405

406
    /** @hidden @internal */
407
    public get hasProjectedInputs(): boolean {
408
        return this.projectedInputs?.length > 0;
1,953✔
409
    }
410

411
    /** @hidden @internal */
412
    public get separatorClass(): string {
413
        return 'igx-date-range-picker__label';
302✔
414
    }
415

416
    protected override get toggleContainer(): HTMLElement | undefined {
417
        return this._calendarContainer;
7✔
418
    }
419

420
    private get required(): boolean {
421
        if (this._ngControl && this._ngControl.control && this._ngControl.control.validator) {
164✔
422
            const error = this._ngControl.control.validator({} as AbstractControl);
111✔
423
            return (error && error.required) ? true : false;
111!
424
        }
425

426
        return false;
53✔
427
    }
428

429
    private get calendar(): IgxCalendarComponent {
430
        return this._calendar;
656✔
431
    }
432

433
    private get dropdownOverlaySettings(): OverlaySettings {
434
        return Object.assign({}, this._dropDownOverlaySettings, this.overlaySettings);
28✔
435
    }
436

437
    private get dialogOverlaySettings(): OverlaySettings {
438
        return Object.assign({}, this._dialogOverlaySettings, this.overlaySettings);
7✔
439
    }
440

441
    private _resourceStrings = getCurrentResourceStrings(DateRangePickerResourceStringsEN);
90✔
442
    private _doneButtonText = null;
90✔
443
    private _dateSeparator = null;
90✔
444
    private _value: DateRange | null;
445
    private _overlayId: string;
446
    private _ngControl: NgControl;
447
    private _statusChanges$: Subscription;
448
    private _calendar: IgxCalendarComponent;
449
    private _calendarContainer?: HTMLElement;
450
    private _positionSettings: PositionSettings;
451
    private _focusedInput: IgxDateRangeInputsBaseComponent;
452
    private _overlaySubFilter:
90✔
453
        [MonoTypeOperatorFunction<OverlayEventArgs>, MonoTypeOperatorFunction<OverlayEventArgs | OverlayCancelableEventArgs>] = [
454
            filter(x => x.id === this._overlayId),
112✔
455
            takeUntil(merge(this._destroy$, this.closed))
456
        ];
457
    private _dialogOverlaySettings: OverlaySettings = {
90✔
458
        closeOnOutsideClick: true,
459
        modal: true,
460
        closeOnEscape: true
461
    };
462
    private _dropDownOverlaySettings: OverlaySettings = {
90✔
463
        closeOnOutsideClick: true,
464
        modal: false,
465
        closeOnEscape: true
466
    };
467
    private onChangeCallback: (dateRange: DateRange) => void = noop;
90✔
468
    private onTouchCallback: () => void = noop;
90✔
469
    private onValidatorChange: () => void = noop;
90✔
470

471
    constructor(element: ElementRef,
472
        @Inject(LOCALE_ID) _localeId: string,
473
        protected platform: PlatformUtil,
90✔
474
        private _injector: Injector,
90✔
475
        private _cdr: ChangeDetectorRef,
90✔
476
        @Inject(IgxOverlayService) private _overlayService: IgxOverlayService,
90✔
477
        @Optional() @Inject(IGX_INPUT_GROUP_TYPE) _inputGroupType?: IgxInputGroupType) {
478
        super(element, _localeId, _inputGroupType);
90✔
479
        this.locale = this.locale || this._localeId;
90!
480
    }
481

482
    /** @hidden @internal */
483
    @HostListener('keydown', ['$event'])
484
    /** @hidden @internal */
485
    public onKeyDown(event: KeyboardEvent): void {
486
        switch (event.key) {
5!
487
            case this.platform.KEYMAP.ARROW_UP:
488
                if (event.altKey) {
×
489
                    this.close();
×
490
                }
491
                break;
×
492
            case this.platform.KEYMAP.ARROW_DOWN:
493
                if (event.altKey) {
5✔
494
                    this.open();
5✔
495
                }
496
                break;
5✔
497
        }
498
    }
499

500
    /**
501
     * Opens the date range picker's dropdown or dialog.
502
     *
503
     * @example
504
     * ```html
505
     * <igx-date-range-picker #dateRange></igx-date-range-picker>
506
     *
507
     * <button type="button" igxButton (click)="dateRange.open()">Open Dialog</button
508
     * ```
509
     */
510
    public open(overlaySettings?: OverlaySettings): void {
511
        if (!this.collapsed || this.disabled) {
39✔
512
            return;
4✔
513
        }
514

515
        const settings = Object.assign({}, this.isDropdown
35✔
516
            ? this.dropdownOverlaySettings
517
            : this.dialogOverlaySettings
518
            , overlaySettings);
519

520
        this._overlayId = this._overlayService
35✔
521
            .attach(IgxCalendarContainerComponent, this.viewContainerRef, settings);
522
        this.subscribeToOverlayEvents();
35✔
523
        this._overlayService.show(this._overlayId);
35✔
524
    }
525

526
    /**
527
     * Closes the date range picker's dropdown or dialog.
528
     *
529
     * @example
530
     * ```html
531
     * <igx-date-range-picker #dateRange></igx-date-range-picker>
532
     *
533
     * <button type="button" igxButton (click)="dateRange.close()">Close Dialog</button>
534
     * ```
535
     */
536
    public close(): void {
537
        if (!this.collapsed) {
48✔
538
            this._overlayService.hide(this._overlayId);
31✔
539
        }
540
    }
541

542
    /**
543
     * Toggles the date range picker's dropdown or dialog
544
     *
545
     * @example
546
     * ```html
547
     * <igx-date-range-picker #dateRange></igx-date-range-picker>
548
     *
549
     * <button type="button" igxButton (click)="dateRange.toggle()">Toggle Dialog</button>
550
     * ```
551
     */
552
    public toggle(overlaySettings?: OverlaySettings): void {
553
        if (!this.collapsed) {
12✔
554
            this.close();
2✔
555
        } else {
556
            this.open(overlaySettings);
10✔
557
        }
558
    }
559

560
    /**
561
     * Selects a range of dates. If no `endDate` is passed, range is 1 day (only `startDate`)
562
     *
563
     * @example
564
     * ```typescript
565
     * public selectFiveDayRange() {
566
     *  const today = new Date();
567
     *  const inFiveDays = new Date(new Date().setDate(today.getDate() + 5));
568
     *  this.dateRange.select(today, inFiveDays);
569
     * }
570
     * ```
571
     */
572
    public select(startDate: Date, endDate?: Date): void {
573
        endDate = endDate ?? startDate;
19✔
574
        const dateRange = [startDate, endDate];
19✔
575
        this.handleSelection(dateRange);
19✔
576
    }
577

578
    /**
579
     * Clears the input field(s) and the picker's value.
580
     *
581
     * @example
582
     * ```typescript
583
     * this.dateRangePicker.clear();
584
     * ```
585
     */
586
    public clear(): void {
587
        if (this.disabled) {
4!
NEW
588
            return;
×
589
        }
590

591
        this.value = null;
4✔
592
        this._calendar?.deselectDate();
4✔
593
        if (this.hasProjectedInputs) {
4✔
594
            this.projectedInputs.forEach((i) => {
2✔
595
                i.inputDirective.clear();
4✔
596
            });
597
        } else {
598
            this.inputDirective.clear();
2✔
599
        }
600
    }
601

602
    /** @hidden @internal */
603
    public writeValue(value: DateRange): void {
604
        this.updateValue(value);
62✔
605
    }
606

607
    /** @hidden @internal */
608
    public registerOnChange(fn: any): void {
609
        this.onChangeCallback = fn;
41✔
610
    }
611

612
    /** @hidden @internal */
613
    public registerOnTouched(fn: any): void {
614
        this.onTouchCallback = fn;
40✔
615
    }
616

617
    /** @hidden @internal */
618
    public validate(control: AbstractControl): ValidationErrors | null {
619
        const value: DateRange = control.value;
207✔
620
        const errors = {};
207✔
621
        if (value) {
207✔
622
            if (this.hasProjectedInputs) {
44✔
623
                const startInput = this.projectedInputs.find(i => i instanceof IgxDateRangeStartComponent) as IgxDateRangeStartComponent;
41✔
624
                const endInput = this.projectedInputs.find(i => i instanceof IgxDateRangeEndComponent) as IgxDateRangeEndComponent;
82✔
625
                if (!startInput.dateTimeEditor.value) {
41!
626
                    Object.assign(errors, { startValue: true });
×
627
                }
628
                if (!endInput.dateTimeEditor.value) {
41✔
629
                    Object.assign(errors, { endValue: true });
1✔
630
                }
631
            }
632

633
            const min = parseDate(this.minValue);
44✔
634
            const max = parseDate(this.maxValue);
44✔
635
            const start = parseDate(value.start);
44✔
636
            const end = parseDate(value.end);
44✔
637
            if ((min && start && DateTimeUtil.lessThanMinValue(start, min, false))
44✔
638
                || (min && end && DateTimeUtil.lessThanMinValue(end, min, false))) {
639
                Object.assign(errors, { minValue: true });
2✔
640
            }
641
            if ((max && start && DateTimeUtil.greaterThanMaxValue(start, max, false))
44✔
642
                || (max && end && DateTimeUtil.greaterThanMaxValue(end, max, false))) {
643
                Object.assign(errors, { maxValue: true });
1✔
644
            }
645
        }
646

647
        return Object.keys(errors).length > 0 ? errors : null;
207✔
648
    }
649

650
    /** @hidden @internal */
651
    public registerOnValidatorChange?(fn: any): void {
652
        this.onValidatorChange = fn;
40✔
653
    }
654

655
    /** @hidden @internal */
656
    public setDisabledState?(isDisabled: boolean): void {
657
        this.disabled = isDisabled;
41✔
658
    }
659

660
    /** @hidden */
661
    public ngOnInit(): void {
662
        this._ngControl = this._injector.get<NgControl>(NgControl, null);
87✔
663

664
        this.locale = this.locale || this._localeId;
87!
665
    }
666

667
    /** @hidden */
668
    public override ngAfterViewInit(): void {
669
        super.ngAfterViewInit();
84✔
670
        this.subscribeToDateEditorEvents();
84✔
671
        this.subscribeToClick();
84✔
672
        this.configPositionStrategy();
84✔
673
        this.configOverlaySettings();
84✔
674
        this.cacheFocusedInput();
84✔
675
        this.attachOnTouched();
84✔
676

677
        this.setRequiredToInputs();
84✔
678

679
        if (this._ngControl) {
84✔
680
            this._statusChanges$ = this._ngControl.statusChanges.subscribe(this.onStatusChanged.bind(this));
33✔
681
        }
682

683
        // delay invocations until the current change detection cycle has completed
684
        Promise.resolve().then(() => {
84✔
685
            this.updateDisabledState();
84✔
686
            this.initialSetValue();
84✔
687
            this.updateInputs();
84✔
688
            // B.P. 07 July 2021 - IgxDateRangePicker not showing initial disabled state with ChangeDetectionStrategy.OnPush #9776
689
            /**
690
             * if disabled is placed on the range picker element and there are projected inputs
691
             * run change detection since igxInput will initially set the projected inputs' disabled to false
692
             */
693
            if (this.hasProjectedInputs && this.disabled) {
84✔
694
                this._cdr.markForCheck();
3✔
695
            }
696
        });
697
        this.updateDisplayFormat();
84✔
698
        this.updateInputFormat();
84✔
699
    }
700

701
    /** @hidden @internal */
702
    public ngOnChanges(changes: SimpleChanges): void {
703
        if (changes['displayFormat'] && this.hasProjectedInputs) {
89✔
704
            this.updateDisplayFormat();
11✔
705
        }
706
        if (changes['inputFormat'] && this.hasProjectedInputs) {
89✔
707
            this.updateInputFormat();
4✔
708
        }
709
        if (changes['disabled']) {
89✔
710
            this.updateDisabledState();
66✔
711
        }
712
    }
713

714
    /** @hidden @internal */
715
    public override ngOnDestroy(): void {
716
        super.ngOnDestroy();
73✔
717
        if (this._statusChanges$) {
73✔
718
            this._statusChanges$.unsubscribe();
33✔
719
        }
720
        if (this._overlayId) {
73✔
721
            this._overlayService.detach(this._overlayId);
8✔
722
        }
723
    }
724

725
    /** @hidden @internal */
726
    public getEditElement(): HTMLInputElement | undefined {
727
        return this.inputDirective?.nativeElement;
86✔
728
    }
729

730
    protected onStatusChanged = () => {
90✔
731
        if (this.inputGroup) {
79✔
732
            this.setValidityState(this.inputDirective, this.inputGroup.isFocused);
7✔
733
        } else if (this.hasProjectedInputs) {
72✔
734
            this.projectedInputs
72✔
735
                .forEach((i) => {
736
                    this.setValidityState(i.inputDirective, i.isFocused);
144✔
737
                });
738
        }
739
        this.setRequiredToInputs();
79✔
740
    };
741

742
    private setValidityState(inputDirective: IgxInputDirective, isFocused: boolean) {
743
        if (this._ngControl && !this._ngControl.disabled && this.isTouchedOrDirty) {
151✔
744
            if (this.hasValidators && isFocused) {
137✔
745
                inputDirective.valid = this._ngControl.valid ? IgxInputState.VALID : IgxInputState.INVALID;
2✔
746
            } else {
747
                inputDirective.valid = this._ngControl.valid ? IgxInputState.INITIAL : IgxInputState.INVALID;
135✔
748
            }
749
        } else {
750
            inputDirective.valid = IgxInputState.INITIAL;
14✔
751
        }
752
    }
753

754
    private get isTouchedOrDirty(): boolean {
755
        return (this._ngControl.control.touched || this._ngControl.control.dirty);
141✔
756
    }
757

758
    private get hasValidators(): boolean {
759
        return (!!this._ngControl.control.validator || !!this._ngControl.control.asyncValidator);
137✔
760
    }
761

762
    private handleSelection(selectionData: Date[]): void {
763
        let newValue = this.extractRange(selectionData);
37✔
764
        if (!newValue.start && !newValue.end) {
37!
765
            newValue = null;
×
766
        }
767
        this.value = newValue;
37✔
768
        if (this.isDropdown && selectionData?.length > 1) {
37✔
769
            this.close();
26✔
770
        }
771
    }
772

773
    private handleClosing(e: IBaseCancelableBrowserEventArgs): void {
774
        const args = { owner: this, cancel: e?.cancel, event: e?.event };
34✔
775
        this.closing.emit(args);
34✔
776
        e.cancel = args.cancel;
34✔
777
        if (args.cancel) {
34✔
778
            return;
2✔
779
        }
780

781
        if (this.isDropdown && e?.event && !this.isFocused) {
32!
782
            // outside click
783
            this.updateValidityOnBlur();
×
784
        } else {
785
            this.onTouchCallback();
32✔
786
            // input click
787
            if (this.hasProjectedInputs && this._focusedInput) {
32✔
788
                this._focusedInput.setFocus();
7✔
789
            }
790
            if (this.inputDirective) {
32✔
791
                this.inputDirective.focus();
15✔
792
            }
793
        }
794
    }
795

796
    private subscribeToOverlayEvents() {
797
        this._overlayService.opening.pipe(...this._overlaySubFilter).subscribe((e) => {
35✔
798
            const overlayEvent = e as OverlayCancelableEventArgs;
35✔
799
            const args = { owner: this, cancel: overlayEvent?.cancel, event: e.event };
35✔
800
            this.opening.emit(args);
35✔
801
            if (args.cancel) {
35!
802
                this._overlayService.detach(this._overlayId);
×
803
                overlayEvent.cancel = true;
×
804
                return;
×
805
            }
806

807
            this._initializeCalendarContainer(e.componentRef.instance);
35✔
808
            this._calendarContainer = e.componentRef.location.nativeElement;
35✔
809
            this._collapsed = false;
35✔
810
            this.updateCalendar();
35✔
811
        });
812

813
        this._overlayService.opened.pipe(...this._overlaySubFilter).subscribe(() => {
35✔
814
            this.calendar.wrapper.nativeElement.focus();
27✔
815
            this.opened.emit({ owner: this });
27✔
816
        });
817

818
        this._overlayService.closing.pipe(...this._overlaySubFilter).subscribe((e) => {
35✔
819
            this.handleClosing(e as OverlayCancelableEventArgs);
34✔
820
        });
821

822
        this._overlayService.closed.pipe(...this._overlaySubFilter).subscribe(() => {
35✔
823
            this._overlayService.detach(this._overlayId);
16✔
824
            this._collapsed = true;
16✔
825
            this._overlayId = null;
16✔
826
            this._calendar = null;
16✔
827
            this._calendarContainer = undefined;
16✔
828
            this.closed.emit({ owner: this });
16✔
829
        });
830
    }
831

832
    private updateValue(value: DateRange) {
833
        this._value = value ? value : null;
145✔
834
        this.updateInputs();
145✔
835
        this.updateCalendar();
145✔
836
    }
837

838
    private updateValidityOnBlur() {
839
        this._focusedInput = null;
2✔
840
        this.onTouchCallback();
2✔
841
        if (this._ngControl) {
2✔
842
            if (this.hasProjectedInputs) {
1✔
843
                this.projectedInputs.forEach(i => {
1✔
844
                    if (!this._ngControl.valid) {
2!
845
                        i.updateInputValidity(IgxInputState.INVALID);
2✔
846
                    } else {
847
                        i.updateInputValidity(IgxInputState.INITIAL);
×
848
                    }
849
                });
850
            }
851

852
            if (this.inputDirective) {
1!
853
                if (!this._ngControl.valid) {
×
854
                    this.inputDirective.valid = IgxInputState.INVALID;
×
855
                } else {
856
                    this.inputDirective.valid = IgxInputState.INITIAL;
×
857
                }
858
            }
859
        }
860
    }
861

862
    private updateDisabledState() {
863
        if (this.hasProjectedInputs) {
150✔
864
            const start = this.projectedInputs.find(i => i instanceof IgxDateRangeStartComponent) as IgxDateRangeStartComponent;
38✔
865
            const end = this.projectedInputs.find(i => i instanceof IgxDateRangeEndComponent) as IgxDateRangeEndComponent;
76✔
866
            start.inputDirective.disabled = this.disabled;
38✔
867
            end.inputDirective.disabled = this.disabled;
38✔
868
            return;
38✔
869
        }
870
    }
871

872
    private setRequiredToInputs(): void {
873
        // workaround for igxInput setting required
874
        Promise.resolve().then(() => {
163✔
875
            const isRequired = this.required;
163✔
876
            if (this.inputGroup && this.inputGroup.isRequired !== isRequired) {
163✔
877
                this.inputGroup.isRequired = isRequired;
4✔
878
            } else if (this.hasProjectedInputs && this._ngControl) {
159✔
879
                this.projectedInputs.forEach(i => i.isRequired = isRequired);
204✔
880
            }
881
        });
882
    }
883

884
    private parseMinValue(value: string | Date): Date | null {
885
        let minValue: Date = parseDate(value);
57✔
886
        if (!minValue && this.hasProjectedInputs) {
57✔
887
            const start = this.projectedInputs.filter(i => i instanceof IgxDateRangeStartComponent)[0];
52✔
888
            if (start) {
26✔
889
                minValue = parseDate(start.dateTimeEditor.minValue);
26✔
890
            }
891
        }
892

893
        return minValue;
57✔
894
    }
895

896
    private parseMaxValue(value: string | Date): Date | null {
897
        let maxValue: Date = parseDate(value);
57✔
898
        if (!maxValue && this.projectedInputs) {
57✔
899
            const end = this.projectedInputs.filter(i => i instanceof IgxDateRangeEndComponent)[0];
55✔
900
            if (end) {
55✔
901
                maxValue = parseDate(end.dateTimeEditor.maxValue);
26✔
902
            }
903
        }
904

905
        return maxValue;
57✔
906
    }
907

908
    private updateCalendar(): void {
909
        if (!this.calendar) {
182✔
910
            return;
125✔
911
        }
912
        this.calendar.disabledDates = [];
57✔
913
        const minValue = this.parseMinValue(this.minValue);
57✔
914
        if (minValue) {
57✔
915
            this.calendar.disabledDates.push({ type: DateRangeType.Before, dateRange: [minValue] });
2✔
916
        }
917
        const maxValue = this.parseMaxValue(this.maxValue);
57✔
918
        if (maxValue) {
57✔
919
            this.calendar.disabledDates.push({ type: DateRangeType.After, dateRange: [maxValue] });
2✔
920
        }
921

922
        const range: Date[] = [];
57✔
923
        if (this.value?.start && this.value?.end) {
57✔
924
            const _value = this.toRangeOfDates(this.value);
21✔
925
            if (DateTimeUtil.greaterThanMaxValue(_value.start, _value.end)) {
21!
926
                this.swapEditorDates();
×
927
            }
928
            if (this.valueInRange(this.value, minValue, maxValue)) {
21✔
929
                range.push(_value.start, _value.end);
21✔
930
            }
931
        }
932

933
        if (range.length > 0) {
57✔
934
            this.calendar.selectDate(range);
21✔
935
        } else if (range.length === 0 && this.calendar.monthViews) {
36!
936
            this.calendar.deselectDate();
×
937
        }
938
        this.calendar.viewDate = range[0] || new Date();
57✔
939
    }
940

941
    private swapEditorDates(): void {
942
        if (this.hasProjectedInputs) {
×
943
            const start = this.projectedInputs.find(i => i instanceof IgxDateRangeStartComponent) as IgxDateRangeStartComponent;
×
944
            const end = this.projectedInputs.find(i => i instanceof IgxDateRangeEndComponent) as IgxDateRangeEndComponent;
×
945
            [start.dateTimeEditor.value, end.dateTimeEditor.value] = [end.dateTimeEditor.value, start.dateTimeEditor.value];
×
946
            [this.value.start, this.value.end] = [this.value.end, this.value.start];
×
947
        }
948
    }
949

950
    private valueInRange(value: DateRange, minValue?: Date, maxValue?: Date): boolean {
951
        const _value = this.toRangeOfDates(value);
21✔
952
        if (minValue && DateTimeUtil.lessThanMinValue(_value.start, minValue, false)) {
21!
953
            return false;
×
954
        }
955
        if (maxValue && DateTimeUtil.greaterThanMaxValue(_value.end, maxValue, false)) {
21!
956
            return false;
×
957
        }
958

959
        return true;
21✔
960
    }
961

962
    private extractRange(selection: Date[]): DateRange {
963
        return {
37✔
964
            start: selection[0] || null,
37!
965
            end: selection.length > 0 ? selection[selection.length - 1] : null
37!
966
        };
967
    }
968

969
    private toRangeOfDates(range: DateRange): { start: Date; end: Date } {
970
        let start;
971
        let end;
972
        if (!isDate(range.start)) {
88✔
973
            start = DateTimeUtil.parseIsoDate(range.start);
2✔
974
        }
975
        if (!isDate(range.end)) {
88✔
976
            end = DateTimeUtil.parseIsoDate(range.end);
3✔
977
        }
978

979
        if (start || end) {
88!
980
            return { start, end };
×
981
        }
982

983
        return { start: range.start as Date, end: range.end as Date };
88✔
984
    }
985

986
    private subscribeToClick() {
987
        const editElement = this.getEditElement();
84✔
988
        if (!editElement) {
84✔
989
            return;
33✔
990
        }
991
        fromEvent(editElement, 'click')
51✔
992
            .pipe(takeUntil(this._destroy$))
993
            .subscribe(() => {
994
                if (!this.isDropdown) {
2✔
995
                    this.toggle();
1✔
996
                }
997
            });
998
    }
999

1000
    private subscribeToDateEditorEvents(): void {
1001
        if (this.hasProjectedInputs) {
84✔
1002
            const start = this.projectedInputs.find(i => i instanceof IgxDateRangeStartComponent) as IgxDateRangeStartComponent;
33✔
1003
            const end = this.projectedInputs.find(i => i instanceof IgxDateRangeEndComponent) as IgxDateRangeEndComponent;
66✔
1004
            if (start && end) {
33✔
1005
                start.dateTimeEditor.valueChange
33✔
1006
                    .pipe(takeUntil(this._destroy$))
1007
                    .subscribe(value => {
1008
                        if (this.value) {
1!
1009
                            this.value = { start: value, end: this.value.end };
×
1010
                        } else {
1011
                            this.value = { start: value, end: null };
1✔
1012
                        }
1013
                    });
1014
                end.dateTimeEditor.valueChange
33✔
1015
                    .pipe(takeUntil(this._destroy$))
1016
                    .subscribe(value => {
1017
                        if (this.value) {
1!
1018
                            this.value = { start: this.value.start, end: value as Date };
1✔
1019
                        } else {
1020
                            this.value = { start: null, end: value as Date };
×
1021
                        }
1022
                    });
1023
            }
1024
        }
1025
    }
1026

1027
    private attachOnTouched(): void {
1028
        if (this.hasProjectedInputs) {
84✔
1029
            this.projectedInputs.forEach(i => {
33✔
1030
                fromEvent(i.dateTimeEditor.nativeElement, 'blur')
66✔
1031
                    .pipe(takeUntil(this._destroy$))
1032
                    .subscribe(() => {
1033
                        if (this.collapsed) {
9✔
1034
                            this.updateValidityOnBlur();
1✔
1035
                        }
1036
                    });
1037
            });
1038
        } else {
1039
            fromEvent(this.inputDirective.nativeElement, 'blur')
51✔
1040
                .pipe(takeUntil(this._destroy$))
1041
                .subscribe(() => {
1042
                    if (this.collapsed) {
6!
1043
                        this.updateValidityOnBlur();
×
1044
                    }
1045
                });
1046
        }
1047
    }
1048

1049
    private cacheFocusedInput(): void {
1050
        if (this.hasProjectedInputs) {
84✔
1051
            this.projectedInputs.forEach(i => {
33✔
1052
                fromEvent(i.dateTimeEditor.nativeElement, 'focus')
66✔
1053
                    .pipe(takeUntil(this._destroy$))
1054
                    .subscribe(() => this._focusedInput = i);
13✔
1055
            });
1056
        }
1057
    }
1058

1059
    private configPositionStrategy(): void {
1060
        this._positionSettings = {
84✔
1061
            openAnimation: fadeIn,
1062
            closeAnimation: fadeOut
1063
        };
1064
        this._dropDownOverlaySettings.positionStrategy = new AutoPositionStrategy(this._positionSettings);
84✔
1065
        this._dropDownOverlaySettings.target = this.element.nativeElement;
84✔
1066
    }
1067

1068
    private configOverlaySettings(): void {
1069
        if (this.overlaySettings !== null) {
84✔
1070
            this._dropDownOverlaySettings = Object.assign({}, this._dropDownOverlaySettings, this.overlaySettings);
84✔
1071
            this._dialogOverlaySettings = Object.assign({}, this._dialogOverlaySettings, this.overlaySettings);
84✔
1072
        }
1073
    }
1074

1075
    private initialSetValue() {
1076
        // if there is no value and no ngControl on the picker but we have inputs we may have value set through
1077
        // their ngModels - we should generate our initial control value
1078
        if ((!this.value || (!this.value.start && !this.value.end)) && this.hasProjectedInputs && !this._ngControl) {
84!
1079
            const start = this.projectedInputs.find(i => i instanceof IgxDateRangeStartComponent);
3✔
1080
            const end = this.projectedInputs.find(i => i instanceof IgxDateRangeEndComponent);
6✔
1081
            this._value = {
3✔
1082
                start: start.dateTimeEditor.value as Date,
1083
                end: end.dateTimeEditor.value as Date
1084
            };
1085
        }
1086
    }
1087

1088
    private updateInputs(): void {
1089
        const start = this.projectedInputs?.find(i => i instanceof IgxDateRangeStartComponent) as IgxDateRangeStartComponent;
229✔
1090
        const end = this.projectedInputs?.find(i => i instanceof IgxDateRangeEndComponent) as IgxDateRangeEndComponent;
229✔
1091
        if (start && end) {
229✔
1092
            const _value = this.value ? this.toRangeOfDates(this.value) : null;
105✔
1093
            start.updateInputValue(_value?.start || null);
105✔
1094
            end.updateInputValue(_value?.end || null);
105✔
1095
        }
1096
    }
1097

1098
    private updateDisplayFormat(): void {
1099
        this.projectedInputs.forEach(i => {
98✔
1100
            const input = i as IgxDateRangeInputsBaseComponent;
94✔
1101
            input.dateTimeEditor.displayFormat = this.displayFormat;
94✔
1102
        });
1103
    }
1104

1105
    private updateInputFormat(): void {
1106
        this.projectedInputs.forEach(i => {
88✔
1107
            const input = i as IgxDateRangeInputsBaseComponent;
74✔
1108
            if (input.dateTimeEditor.inputFormat !== this.inputFormat) {
74✔
1109
                input.dateTimeEditor.inputFormat = this.inputFormat;
74✔
1110
            }
1111
        });
1112
    }
1113

1114
    private updateInputLocale(): void {
1115
        this.projectedInputs.forEach(i => {
3✔
1116
            const input = i as IgxDateRangeInputsBaseComponent;
6✔
1117
            input.dateTimeEditor.locale = this.locale;
6✔
1118
        });
1119
    }
1120

1121
    private _initializeCalendarContainer(componentInstance: IgxCalendarContainerComponent) {
1122
        this._calendar = componentInstance.calendar;
35✔
1123
        this.calendar.hasHeader = false;
35✔
1124
        this.calendar.locale = this.locale;
35✔
1125
        this.calendar.selection = CalendarSelection.RANGE;
35✔
1126
        this.calendar.weekStart = this.weekStart;
35✔
1127
        this.calendar.hideOutsideDays = this.hideOutsideDays;
35✔
1128
        this.calendar.monthsViewNumber = this.displayMonthsCount;
35✔
1129
        this.calendar.showWeekNumbers = this.showWeekNumbers;
35✔
1130
        this.calendar.selected.pipe(takeUntil(this._destroy$)).subscribe((ev: Date[]) => this.handleSelection(ev));
35✔
1131

1132
        componentInstance.mode = this.mode;
35✔
1133
        componentInstance.closeButtonLabel = !this.isDropdown ? this.doneButtonText : null;
35✔
1134
        componentInstance.pickerActions = this.pickerActions;
35✔
1135
        componentInstance.calendarClose.pipe(takeUntil(this._destroy$)).subscribe(() => this.close());
35✔
1136
    }
1137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc