Skip to content

Commit fe3a767

Browse files
authored
Merge pull request #7690 from IgniteUI/nalipiev/date-range-picker-coverage
test(date-range-picker): improve coverage and refactor #5732 (#7647)
2 parents ae149ed + ebebca0 commit fe3a767

8 files changed

+686
-404
lines changed

projects/igniteui-angular/src/lib/date-picker/date-picker.utils.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isIE } from '../core/utils';
22
import { DatePart, DatePartInfo } from '../directives/date-time-editor/date-time-editor.common';
3+
import { formatDate, FormatWidth, getLocaleDateFormat } from '@angular/common';
34

45
/**
56
* This enum is used to keep the date validation result.
@@ -48,7 +49,7 @@ export abstract class DatePickerUtil {
4849

4950

5051
/**
51-
* TODO: Unit tests for all public methods.
52+
* TODO: (in issue #6483) Unit tests and docs for all public methods.
5253
*/
5354

5455

@@ -147,6 +148,38 @@ export abstract class DatePickerUtil {
147148
return DatePickerUtil.getMask(parts);
148149
}
149150

151+
public static formatDate(value: number | Date, format: string, locale: string, timezone?: string): string {
152+
let formattedDate: string;
153+
try {
154+
formattedDate = formatDate(value, format, locale, timezone);
155+
} catch {
156+
this.logMissingLocaleSettings(locale);
157+
const formatter = new Intl.DateTimeFormat(locale);
158+
formattedDate = formatter.format(value);
159+
}
160+
161+
return formattedDate;
162+
}
163+
164+
public static getLocaleDateFormat(locale: string, displayFormat?: string): string {
165+
const formatKeys = Object.keys(FormatWidth) as (keyof FormatWidth)[];
166+
const targetKey = formatKeys.find(k => k.toLowerCase() === displayFormat?.toLowerCase().replace('date', ''));
167+
if (!targetKey) {
168+
// if displayFormat is not shortDate, longDate, etc.
169+
// or if it is not set by the user
170+
return displayFormat;
171+
}
172+
let format: string;
173+
try {
174+
format = getLocaleDateFormat(locale, FormatWidth[targetKey]);
175+
} catch {
176+
this.logMissingLocaleSettings(locale);
177+
format = DatePickerUtil.getDefaultInputFormat(locale);
178+
}
179+
180+
return format;
181+
}
182+
150183
public static isDateOrTimeChar(char: string): boolean {
151184
return DATE_CHARS.indexOf(char) !== -1 || TIME_CHARS.indexOf(char) !== -1;
152185
}
@@ -303,6 +336,11 @@ export abstract class DatePickerUtil {
303336
return _value.getTime() < _minValue.getTime();
304337
}
305338

339+
private static logMissingLocaleSettings(locale: string): void {
340+
console.warn(`Missing locale data for the locale ${locale}. Please refer to https://angular.io/guide/i18n#i18n-pipes`);
341+
console.warn('Using default browser locale settings.');
342+
}
343+
306344
private static ensureLeadingZero(part: DatePartInfo) {
307345
switch (part.type) {
308346
case DatePart.Date:

projects/igniteui-angular/src/lib/date-range-picker/date-range-picker-inputs.common.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Component, ContentChild, Pipe, PipeTransform, Output, EventEmitter, HostListener, Directive } from '@angular/core';
2-
import { formatDate } from '@angular/common';
32
import { NgControl } from '@angular/forms';
43
import { IgxInputDirective, IgxInputState } from '../input-group/public_api';
54
import { IgxInputGroupComponent } from '../input-group/input-group.component';
65
import { IgxInputGroupBase } from '../input-group/input-group.common';
6+
import { DatePickerUtil } from '../date-picker/date-picker.utils';
77
import { IgxDateTimeEditorDirective } from '../directives/date-time-editor/public_api';
88

99
/**
@@ -17,14 +17,17 @@ export interface DateRange {
1717
/** @hidden @internal */
1818
@Pipe({ name: 'dateRange' })
1919
export class DateRangePickerFormatPipe implements PipeTransform {
20-
public transform(values: DateRange, inputFormat?: string, locale?: string): string {
21-
if (!values) {
20+
public transform(values: DateRange, appliedFormat?: string,
21+
locale?: string, formatter?: (_: DateRange) => string): string {
22+
if (!values || !values.start && !values.end) {
2223
return '';
2324
}
25+
if (formatter) {
26+
return formatter(values);
27+
}
2428
const { start, end } = values;
25-
// TODO: move default locale from IgxDateTimeEditorDirective to its commons file/use displayFormat
26-
const startDate = inputFormat ? formatDate(start, inputFormat, locale || 'en') : start?.toLocaleDateString();
27-
const endDate = inputFormat ? formatDate(end, inputFormat, locale || 'en') : end?.toLocaleDateString();
29+
const startDate = appliedFormat ? DatePickerUtil.formatDate(start, appliedFormat, locale || 'en') : start?.toLocaleDateString();
30+
const endDate = appliedFormat ? DatePickerUtil.formatDate(end, appliedFormat, locale || 'en') : end?.toLocaleDateString();
2831
let formatted;
2932
if (start) {
3033
formatted = `${startDate} - `;
@@ -33,7 +36,6 @@ export class DateRangePickerFormatPipe implements PipeTransform {
3336
}
3437
}
3538

36-
// TODO: no need to set format twice
3739
return formatted ? formatted : '';
3840
}
3941
}

projects/igniteui-angular/src/lib/date-range-picker/date-range-picker.component.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@
4343
<ng-template #defTemplate>
4444
<igx-input-group (click)="open()">
4545
<input #singleInput igxInput type="text" readonly
46-
[placeholder]="this.value ? '' : appliedFormat"
46+
[placeholder]="this.value ? '' : singleInputFormat"
4747
role="combobox"
4848
aria-haspopup="grid"
4949
[attr.aria-expanded]="!toggle.collapsed"
5050
[attr.aria-labelledby]="this.label?.id"
51-
[value]="this.value | dateRange: this.inputFormat : this.locale"
51+
[value]="this.value | dateRange: this.appliedFormat : this.locale : this.formatter"
5252
/>
5353

5454
<igx-prefix *ngIf="!this.toggleComponents.length">

0 commit comments

Comments
 (0)