Skip to content

Commit 448e967

Browse files
refactor(date-editor): update events, display format for IE, spin methods
1 parent e3d44e4 commit 448e967

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ export abstract class DatePickerUtil {
134134
}
135135

136136
public static getDefaultInputFormat(locale: string): string {
137+
if (!Intl || !Intl.DateTimeFormat || !Intl.DateTimeFormat.prototype.formatToParts) {
138+
// TODO: fallback with Intl.format for IE?
139+
return DatePickerUtil.SHORT_DATE_MASK;
140+
}
137141
const parts = DatePickerUtil.getDefaultLocaleMask(locale);
138142
parts.forEach(p => {
139143
if (p.type !== DatePart.Year && p.type !== DatePickerUtil.SEPARATOR) {
@@ -148,7 +152,7 @@ export abstract class DatePickerUtil {
148152
return DATE_CHARS.indexOf(char) !== -1 || TIME_CHARS.indexOf(char) !== -1;
149153
}
150154

151-
public static spinDate(delta: number, newDate: Date, isSpinLoop: boolean): Date {
155+
public static spinDate(delta: number, newDate: Date, isSpinLoop: boolean): void {
152156
const maxDate = DatePickerUtil.daysInMonth(newDate.getFullYear(), newDate.getMonth());
153157
let date = newDate.getDate() + delta;
154158
if (date > maxDate) {
@@ -158,10 +162,9 @@ export abstract class DatePickerUtil {
158162
}
159163

160164
newDate.setDate(date);
161-
return newDate;
162165
}
163166

164-
public static spinMonth(delta: number, newDate: Date, isSpinLoop: boolean): Date {
167+
public static spinMonth(delta: number, newDate: Date, isSpinLoop: boolean): void {
165168
const maxDate = DatePickerUtil.daysInMonth(newDate.getFullYear(), newDate.getMonth() + delta);
166169
if (newDate.getDate() > maxDate) {
167170
newDate.setDate(maxDate);
@@ -177,19 +180,17 @@ export abstract class DatePickerUtil {
177180
}
178181

179182
newDate.setMonth(month);
180-
return newDate;
181183
}
182184

183-
public static spinYear(delta: number, newDate: Date): Date {
185+
public static spinYear(delta: number, newDate: Date): void {
184186
const maxDate = DatePickerUtil.daysInMonth(newDate.getFullYear() + delta, newDate.getMonth());
185187
if (newDate.getDate() > maxDate) {
186188
newDate.setDate(maxDate);
187189
}
188190
newDate.setFullYear(newDate.getFullYear() + delta);
189-
return newDate;
190191
}
191192

192-
public static spinHours(delta: number, newDate: Date, isSpinLoop: boolean): Date {
193+
public static spinHours(delta: number, newDate: Date, isSpinLoop: boolean): void {
193194
const maxHour = 23;
194195
const minHour = 0;
195196
let hours = newDate.getHours() + delta;
@@ -200,10 +201,9 @@ export abstract class DatePickerUtil {
200201
}
201202

202203
newDate.setHours(hours);
203-
return newDate;
204204
}
205205

206-
public static spinMinutes(delta: number, newDate: Date, isSpinLoop: boolean): Date {
206+
public static spinMinutes(delta: number, newDate: Date, isSpinLoop: boolean): void {
207207
const maxMinutes = 59;
208208
const minMinutes = 0;
209209
let minutes = newDate.getMinutes() + delta;
@@ -214,10 +214,9 @@ export abstract class DatePickerUtil {
214214
}
215215

216216
newDate.setMinutes(minutes);
217-
return newDate;
218217
}
219218

220-
public static spinSeconds(delta: number, newDate: Date, isSpinLoop: boolean): Date {
219+
public static spinSeconds(delta: number, newDate: Date, isSpinLoop: boolean): void {
221220
const maxSeconds = 59;
222221
const minSeconds = 0;
223222
let seconds = newDate.getSeconds() + delta;
@@ -228,10 +227,9 @@ export abstract class DatePickerUtil {
228227
}
229228

230229
newDate.setSeconds(seconds);
231-
return newDate;
232230
}
233231

234-
public static spinAmPm(newDate: Date, currentDate: Date, amPmFromMask: string) {
232+
public static spinAmPm(newDate: Date, currentDate: Date, amPmFromMask: string): Date {
235233
switch (amPmFromMask) {
236234
case 'AM':
237235
newDate = new Date(newDate.setHours(newDate.getHours() + 12));

projects/igniteui-angular/src/lib/directives/date-time-editor/date-time-editor.directive.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnCh
6464
/**
6565
* Set the minimum possible value the editor will allow.
6666
*
67-
* If a `string` value is passed in, it must be in the defined input format; if no input format is defined
68-
* then the value's format must match the format based on the current locale.
67+
* If a `string` value is passed in, it must be in the defined input format.
6968
*
7069
* @example
7170
* ```html
@@ -85,8 +84,7 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnCh
8584
/**
8685
* Set the maximum possible value the editor will allow.
8786
*
88-
* If a `string` value is passed in, it must be in the defined input format; if no input format is defined
89-
* then the value's format must match the format based on the current locale.
87+
* If a `string` value is passed in, it must be in the defined input format.
9088
* @example
9189
* ```html
9290
* <input igxDateTimeEditor [maxValue]="maxDate">
@@ -164,11 +162,11 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnCh
164162
* Emitted when the editor's value has changed.
165163
* @example
166164
* ```html
167-
* <input igxDateTimeEditor (valueChanged)="onValueChanged($event)"/>
165+
* <input igxDateTimeEditor (valueChange)="onValueChanged($event)"/>
168166
* ```
169167
*/
170168
@Output()
171-
public valueChange = new EventEmitter<IgxDateTimeEditorEventArgs>();
169+
public valueChange = new EventEmitter<Date>();
172170

173171
/**
174172
* Emitted when the editor is not within a specified range or when the editor's value is in an invalid state.
@@ -386,7 +384,12 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnCh
386384
this.setSelectionRange(cursor);
387385
} else {
388386
const format = this.displayFormat || this.inputFormat;
389-
this.inputValue = formatDate(this.value, format.replace('tt', 'aa'), this.locale);
387+
if (format) {
388+
this.inputValue = formatDate(this.value, format.replace('tt', 'aa'), this.locale);
389+
} else {
390+
// TODO: formatter function?
391+
this.inputValue = this.value.toLocaleString();
392+
}
390393
}
391394
}
392395

@@ -412,22 +415,29 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnCh
412415
const newDate = new Date(this.value.getTime());
413416
switch (datePart) {
414417
case DatePart.Date:
415-
return DatePickerUtil.spinDate(delta, newDate, this.isSpinLoop);
418+
DatePickerUtil.spinDate(delta, newDate, this.isSpinLoop);
419+
break;
416420
case DatePart.Month:
417-
return DatePickerUtil.spinMonth(delta, newDate, this.isSpinLoop);
421+
DatePickerUtil.spinMonth(delta, newDate, this.isSpinLoop);
422+
break;
418423
case DatePart.Year:
419-
return DatePickerUtil.spinYear(delta, newDate);
424+
DatePickerUtil.spinYear(delta, newDate);
425+
break;
420426
case DatePart.Hours:
421-
return DatePickerUtil.spinHours(delta, newDate, this.isSpinLoop);
427+
DatePickerUtil.spinHours(delta, newDate, this.isSpinLoop);
428+
break;
422429
case DatePart.Minutes:
423-
return DatePickerUtil.spinMinutes(delta, newDate, this.isSpinLoop);
430+
DatePickerUtil.spinMinutes(delta, newDate, this.isSpinLoop);
431+
break;
424432
case DatePart.Seconds:
425-
return DatePickerUtil.spinSeconds(delta, newDate, this.isSpinLoop);
433+
DatePickerUtil.spinSeconds(delta, newDate, this.isSpinLoop);
434+
break;
426435
case DatePart.AmPm:
427436
const formatPart = this._inputDateParts.find(dp => dp.type === DatePart.AmPm);
428437
const amPmFromMask = this.inputValue.substring(formatPart.start, formatPart.end);
429438
return DatePickerUtil.spinAmPm(newDate, this.value, amPmFromMask);
430439
}
440+
return newDate;
431441
}
432442

433443
private updateValue(newDate: Date): void {
@@ -438,7 +448,7 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnCh
438448
this.validationFailed.emit({ oldValue: this._oldValue, newValue: this.value, userInput: this.inputValue });
439449
}
440450
if (this.inputIsComplete() || this.inputValue === this.emptyMask) {
441-
this.valueChange.emit({ oldValue: this._oldValue, newValue: this.value, userInput: this.inputValue });
451+
this.valueChange.emit(this.value);
442452
}
443453
}
444454

0 commit comments

Comments
 (0)