Skip to content

Commit 87bcd54

Browse files
authored
Merge pull request #11812 from IgniteUI/VDyulgerov/feature-locale-14.0
V dyulgerov/feature locale 14.0
2 parents 5504653 + 3385053 commit 87bcd54

32 files changed

+379
-117
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@ All notable changes for each version of this project will be documented in this
44

55
## 14.0.0
66

7+
- `IgxDatePicker` and `IgxDateRangePicker` now expose a `weekStart` input property like the `IgxCalendar`
8+
9+
### New Features
10+
711
### General
812
- Updating dependency to Angular 14
913
- `Migrations`
1014
- Migrations now support Yarn berry (version 2+)
1115

1216
- `IgxGridEditingActions`
1317
- Added new inputs to show/hide the edit and delete buttons - `editRow`, `deleteRow`.
18+
-
19+
- - Locale settings
20+
- `IgxDatePicker` and `IgxDateRangePicker` now expose a `weekStart` input property like the `IgxCalendar`
21+
- `IColumnPipeArgs` interface now expose a `weekStart` property to control the first week of day in calendar used in the grid for editing and filtering
22+
- `locale` property of `IgxCalendar`, `IgxDatePicker`, `IgxDateRangePicker` and `IgxGrid` will now default to global Angular application locale, if not set.
23+
- `weekStart` property of `IgxCalendar`, `IgxDatePicker`, `IgxDateRangePicker` and `IgxGrid` will default to the default first day for the current component `locale`, if not set.
1424

1525
## 13.2.0
1626

projects/igniteui-angular/src/lib/calendar/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ Controls the layout of the calendar component. When vertical is set to `true`
114114
the calendar header will be rendered to the side of the calendar body.
115115
Defaults to `false`.
116116

117-
- `weekStart: number | WEEKDAYS`
117+
- `weekStart: WEEKDAYS | number`
118118

119-
Controls the starting day of the weeek for the calendar.
119+
Controls the starting day of the week for the calendar.
120120
Defaults to Sunday.
121121

122122
- `locale: string`

projects/igniteui-angular/src/lib/calendar/calendar-base.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Input, Output, EventEmitter, Directive } from '@angular/core';
1+
import { Input, Output, EventEmitter, Directive, Inject, LOCALE_ID } from '@angular/core';
22
import { WEEKDAYS, Calendar, isDateInRanges, IFormattingOptions, IFormattingViews } from './calendar';
33
import { ControlValueAccessor } from '@angular/forms';
44
import { DateRangeDescriptor } from '../core/dates';
@@ -8,7 +8,7 @@ import { IgxCalendarView } from './month-picker-base';
88
import { CurrentResourceStrings } from '../core/i18n/resources';
99
import { ICalendarResourceStrings } from '../core/i18n/calendar-resources';
1010
import { DateTimeUtil } from '../date-common/util/date-time.util';
11-
11+
import { getLocaleFirstDayOfWeek, getLocaleId } from "@angular/common";
1212

1313
/**
1414
* Sets the selection type - single, multi or range.
@@ -162,7 +162,12 @@ export class IgxCalendarBaseDirective implements ControlValueAccessor {
162162
/**
163163
* @hidden
164164
*/
165-
private _locale = 'en';
165+
private _locale: string;
166+
167+
/**
168+
* @hidden
169+
*/
170+
private _weekStart: WEEKDAYS | number;
166171

167172
/**
168173
* @hidden
@@ -227,7 +232,7 @@ export class IgxCalendarBaseDirective implements ControlValueAccessor {
227232
/**
228233
* Gets the start day of the week.
229234
* Can return a numeric or an enum representation of the week day.
230-
* Defaults to `Sunday` / `0`.
235+
* If not set, defaults to the first day of the week for the application locale.
231236
*/
232237
@Input()
233238
public get weekStart(): WEEKDAYS | number {
@@ -239,12 +244,13 @@ export class IgxCalendarBaseDirective implements ControlValueAccessor {
239244
* Can be assigned to a numeric value or to `WEEKDAYS` enum value.
240245
*/
241246
public set weekStart(value: WEEKDAYS | number) {
247+
this._weekStart = value;
242248
this.calendarModel.firstWeekDay = value;
243249
}
244250

245251
/**
246252
* Gets the `locale` of the calendar.
247-
* Default value is `"en"`.
253+
* If not set, defaults to application's locale.
248254
*/
249255
@Input()
250256
public get locale(): string {
@@ -258,6 +264,19 @@ export class IgxCalendarBaseDirective implements ControlValueAccessor {
258264
*/
259265
public set locale(value: string) {
260266
this._locale = value;
267+
268+
// if value is not a valid BCP 47 tag, set it back to _localeId
269+
try {
270+
getLocaleFirstDayOfWeek(this._locale);
271+
} catch (e) {
272+
this._locale = this._localeId;
273+
}
274+
275+
// changing locale runtime needs to update the `weekStart` too, if `weekStart` is not explicitly set
276+
if (this._weekStart === undefined) {
277+
this.calendarModel.firstWeekDay = getLocaleFirstDayOfWeek(this._locale);
278+
}
279+
261280
this.initFormatters();
262281
}
263282

@@ -436,12 +455,11 @@ export class IgxCalendarBaseDirective implements ControlValueAccessor {
436455
/**
437456
* @hidden
438457
*/
439-
constructor(protected platform: PlatformUtil) {
458+
constructor(protected platform: PlatformUtil, @Inject(LOCALE_ID) protected _localeId: string) {
440459
this.calendarModel = new Calendar();
441-
460+
this.locale = _localeId;
442461
this.viewDate = this.viewDate ? this.viewDate : new Date();
443462

444-
this.calendarModel.firstWeekDay = this.weekStart;
445463
this.initFormatters();
446464
}
447465

projects/igniteui-angular/src/lib/calendar/calendar.component.spec.ts

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, ViewChild } from '@angular/core';
1+
import { Component, LOCALE_ID, ViewChild } from '@angular/core';
22
import { TestBed, tick, fakeAsync, flush, waitForAsync } from '@angular/core/testing';
33
import { FormsModule } from '@angular/forms';
44
import { By } from '@angular/platform-browser';
@@ -347,7 +347,7 @@ describe('IgxCalendar - ', () => {
347347
expect(headerDate.nativeElement.textContent.trim()).toMatch('1 sept.');
348348
expect(bodyYear.nativeElement.textContent.trim()).toMatch('18');
349349
expect(bodyMonth.nativeElement.textContent.trim()).toMatch('sept.');
350-
expect(bodyWeekday.nativeElement.textContent.trim()).toMatch('Dim.');
350+
expect(bodyWeekday.nativeElement.textContent.trim()).toMatch('Lun.');
351351
});
352352

353353
it('Should default to today date when invalid date is passed', () => {
@@ -1964,6 +1964,7 @@ describe('IgxCalendar - ', () => {
19641964
let fixture; let dom; let calendar; let prevMonthBtn; let nextMonthBtn;
19651965

19661966
beforeEach(waitForAsync(() => {
1967+
TestBed.overrideProvider(LOCALE_ID, {useValue: 'fr'});
19671968
fixture = TestBed.createComponent(IgxCalendarSampleComponent);
19681969
fixture.detectChanges();
19691970
dom = fixture.debugElement;
@@ -2007,6 +2008,76 @@ describe('IgxCalendar - ', () => {
20072008
fixture.detectChanges();
20082009
expect(calendar.viewDate.getMonth()).toEqual(5);
20092010
}));
2011+
it('Should the weekStart property takes precedence over locale.', fakeAsync(() => {
2012+
calendar.locale = 'en';
2013+
fixture.detectChanges();
2014+
2015+
expect(calendar.weekStart).toEqual(0);
2016+
2017+
calendar.weekStart = WEEKDAYS.FRIDAY;
2018+
expect(calendar.weekStart).toEqual(5);
2019+
2020+
calendar.locale = 'fr';
2021+
fixture.detectChanges();
2022+
2023+
expect(calendar.weekStart).toEqual(5);
2024+
2025+
flush();
2026+
}));
2027+
2028+
it('Should passing invalid value for locale, then setting weekStart must be respected.', fakeAsync(() => {
2029+
const locale = 'en-US';
2030+
calendar.locale = locale;
2031+
fixture.detectChanges();
2032+
2033+
expect(calendar.locale).toEqual(locale);
2034+
expect(calendar.weekStart).toEqual(WEEKDAYS.SUNDAY)
2035+
2036+
calendar.locale = 'frrr';
2037+
calendar.weekStart = WEEKDAYS.FRIDAY;
2038+
fixture.detectChanges();
2039+
2040+
expect(calendar.locale).toEqual('fr');
2041+
expect(calendar.weekStart).toEqual(WEEKDAYS.FRIDAY);
2042+
2043+
flush();
2044+
}));
2045+
2046+
it('Should setting the global LOCALE_ID, Calendar must be displayed per current locale.', fakeAsync(() => {
2047+
// Verify locale is set respecting the globally LOCALE_ID provider
2048+
expect(calendar.locale).toEqual('fr');
2049+
2050+
// Verify Calendar is displayed per FR locale
2051+
fixture.componentInstance.viewDate = new Date(2022, 5, 23);
2052+
fixture.componentInstance.model = new Date();
2053+
fixture.detectChanges();
2054+
2055+
const defaultOptions = {
2056+
day: 'numeric',
2057+
month: 'short',
2058+
weekday: 'short',
2059+
year: 'numeric'
2060+
};
2061+
const defaultViews = { day: false, month: true, year: false };
2062+
const bodyMonth = dom.query(By.css(HelperTestFunctions.CALENDAR_DATE_CSSCLASS));
2063+
const headerYear = dom.query(By.css(HelperTestFunctions.CALENDAR_HEADER_YEAR_CSSCLASS));
2064+
const bodyYear = dom.queryAll(By.css(HelperTestFunctions.CALENDAR_DATE_CSSCLASS))[1];
2065+
const headerWeekday = dom.queryAll(By.css(`${HelperTestFunctions.CALENDAR_HEADER_DATE_CSSCLASS} span`))[0];
2066+
const headerDate = dom.queryAll(By.css(`${HelperTestFunctions.CALENDAR_HEADER_DATE_CSSCLASS} span`))[1];
2067+
2068+
calendar.selectDate(calendar.viewDate);
2069+
fixture.detectChanges();
2070+
2071+
expect(calendar.formatOptions).toEqual(jasmine.objectContaining(defaultOptions));
2072+
expect(calendar.formatViews).toEqual(jasmine.objectContaining(defaultViews));
2073+
expect(headerYear.nativeElement.textContent.trim()).toMatch('2022');
2074+
expect(headerWeekday.nativeElement.textContent.trim()).toMatch('mer');
2075+
expect(headerDate.nativeElement.textContent.trim()).toMatch('1 juin');
2076+
expect(bodyYear.nativeElement.textContent.trim()).toMatch('2022');
2077+
expect(bodyMonth.nativeElement.textContent.trim()).toMatch('juin');
2078+
2079+
flush();
2080+
}));
20102081
});
20112082
});
20122083
});

projects/igniteui-angular/src/lib/calendar/calendar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ export enum WEEKDAYS {
155155

156156
export class Calendar {
157157

158-
private _firstWeekDay: number | WEEKDAYS;
158+
private _firstWeekDay: WEEKDAYS | number;
159159

160-
constructor(firstWeekDay: number | WEEKDAYS = WEEKDAYS.SUNDAY) {
160+
constructor(firstWeekDay: WEEKDAYS = WEEKDAYS.SUNDAY) {
161161
this._firstWeekDay = firstWeekDay;
162162
}
163163

projects/igniteui-angular/src/lib/calendar/days-view/days-view.component.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
QueryList,
99
HostBinding,
1010
DoCheck,
11-
OnInit
11+
OnInit,
12+
Inject, LOCALE_ID
1213
} from '@angular/core';
1314
import { ICalendarDate, isDateInRanges } from '../../calendar/calendar';
1415
import { NG_VALUE_ACCESSOR } from '@angular/forms';
@@ -138,9 +139,9 @@ export class IgxDaysViewComponent extends IgxCalendarBaseDirective implements Do
138139
*/
139140
constructor(
140141
public daysNavService: IgxDaysViewNavigationService,
141-
protected platform: PlatformUtil
142+
protected platform: PlatformUtil, @Inject(LOCALE_ID) protected _localeId: any
142143
) {
143-
super(platform);
144+
super(platform, _localeId);
144145
}
145146

146147
/**

projects/igniteui-angular/src/lib/date-common/picker-base.directive.ts

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ import { IgxInputGroupType, IGX_INPUT_GROUP_TYPE } from '../input-group/public_a
1414
import { OverlaySettings } from '../services/overlay/utilities';
1515
import { IgxPickerToggleComponent } from './picker-icons.common';
1616
import { PickerInteractionMode } from './types';
17+
import { getLocaleFirstDayOfWeek } from "@angular/common";
18+
import { WEEKDAYS } from '../calendar/calendar';
1719

1820
@Directive()
1921
export abstract class PickerBaseDirective extends DisplayDensityBase implements IToggleView, EditorProvider, AfterViewInit, OnDestroy {
22+
protected _locale;
23+
protected _weekStart: WEEKDAYS | number;
24+
2025
/**
2126
* The editor's input mask.
2227
*
@@ -95,21 +100,51 @@ export abstract class PickerBaseDirective extends DisplayDensityBase implements
95100
public disabled = false;
96101

97102
/**
98-
* Locale settings used for value formatting and calendar or time spinner.
99-
*
100-
* @remarks
101-
* Uses Angular's `LOCALE_ID` by default. Affects both input mask and display format if those are not set.
102-
* If a `locale` is set, it must be registered via `registerLocaleData`.
103-
* Please refer to https://angular.io/guide/i18n#i18n-pipes.
104-
* If it is not registered, `Intl` will be used for formatting.
105-
*
106103
* @example
107104
* ```html
108105
* <igx-date-picker locale="jp"></igx-date-picker>
109106
* ```
110107
*/
108+
/**
109+
* Gets the `locale` of the date-picker.
110+
* If not set, defaults to applciation's locale..
111+
*/
112+
@Input()
113+
public get locale(): string {
114+
return this._locale;
115+
}
116+
117+
/**
118+
* Sets the `locale` of the date-picker.
119+
* Expects a valid BCP 47 language tag.
120+
*/
121+
public set locale(value: string) {
122+
this._locale = value;
123+
// if value is invalid, set it back to _localeId
124+
try {
125+
getLocaleFirstDayOfWeek(this._locale);
126+
} catch (e) {
127+
this._locale = this._localeId;
128+
}
129+
}
130+
131+
/**
132+
* Gets the start day of the week.
133+
* Can return a numeric or an enum representation of the week day.
134+
* If not set, defaults to the first day of the week for the application locale.
135+
*/
111136
@Input()
112-
public locale: string;
137+
public get weekStart(): WEEKDAYS | number {
138+
return this._weekStart ?? getLocaleFirstDayOfWeek(this._locale);
139+
}
140+
141+
/**
142+
* Sets the start day of the week.
143+
* Can be assigned to a numeric value or to `WEEKDAYS` enum value.
144+
*/
145+
public set weekStart(value: WEEKDAYS | number) {
146+
this._weekStart = value;
147+
}
113148

114149
/**
115150
* The container used for the pop-up element.

0 commit comments

Comments
 (0)