Skip to content

Commit b759cd3

Browse files
refactor(date-editor): fix failing tests, promptChars
1 parent 1d3f6cc commit b759cd3

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

Diff for: projects/igniteui-angular/src/lib/date-picker/date-picker.pipes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class DatePickerDisplayValuePipe implements PipeTransform {
1616
return '';
1717
}
1818
this._datePicker.rawDateString = value;
19-
return DatePickerUtil.trimUnderlines(value);
19+
return DatePickerUtil.trimEmptyPlaceholders(value);
2020
}
2121
return '';
2222
}

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ export abstract class DatePickerUtil {
5555
* @param inputData masked value to parse
5656
* @param dateTimeParts Date parts array for the mask
5757
*/
58-
public static parseValueFromMask(inputData: string, dateTimeParts: DatePartInfo[]): Date | null {
58+
public static parseValueFromMask(inputData: string, dateTimeParts: DatePartInfo[], promptChar?: string): Date | null {
5959
const parts: { [key in DatePart]: number } = {} as any;
6060
dateTimeParts.forEach(dp => {
61-
let value = parseInt(this.getCleanVal(inputData, dp), 10);
61+
let value = parseInt(this.getCleanVal(inputData, dp, promptChar), 10);
6262
if (!value) {
6363
value = dp.type === DatePart.Date || dp.type === DatePart.Month ? 1 : 0;
6464
}
@@ -256,8 +256,8 @@ export abstract class DatePickerUtil {
256256
return newDate;
257257
}
258258

259-
private static getCleanVal(inputData: string, datePart: DatePartInfo): string {
260-
return DatePickerUtil.trimUnderlines(inputData.substring(datePart.start, datePart.end));
259+
private static getCleanVal(inputData: string, datePart: DatePartInfo, promptChar?: string): string {
260+
return DatePickerUtil.trimEmptyPlaceholders(inputData.substring(datePart.start, datePart.end), promptChar);
261261
}
262262

263263
private static determineDatePart(char: string): DatePart {
@@ -460,8 +460,8 @@ export abstract class DatePickerUtil {
460460
* This method replaces prompt chars with empty string.
461461
* @param value
462462
*/
463-
public static trimUnderlines(value: string): string {
464-
const result = value.replace(/_/g, '');
463+
public static trimEmptyPlaceholders(value: string, promptChar?: string): string {
464+
const result = value.replace(new RegExp(promptChar || '_', 'g'), '');
465465
return result;
466466
}
467467

@@ -654,7 +654,7 @@ export abstract class DatePickerUtil {
654654
break;
655655
}
656656
case DateParts.Year: {
657-
dateStruct[i].formatType = formatterOptions.month;
657+
dateStruct[i].formatType = formatterOptions.year;
658658
break;
659659
}
660660
}
@@ -711,7 +711,7 @@ export abstract class DatePickerUtil {
711711
private static getDateValueFromInput(dateFormatParts: any[], type: DateParts, inputValue: string, trim: boolean = true): string {
712712
const partPosition = DatePickerUtil.getDateFormatPart(dateFormatParts, type).position;
713713
const result = inputValue.substring(partPosition[0], partPosition[1]);
714-
return (trim) ? DatePickerUtil.trimUnderlines(result) : result;
714+
return (trim) ? DatePickerUtil.trimEmptyPlaceholders(result) : result;
715715
}
716716

717717
private static getDayValueFromInput(dateFormatParts: any[], inputValue: string, trim: boolean = true): string {

Diff for: projects/igniteui-angular/src/lib/directives/date-time-editor/date-time-editor.directive.spec.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('IgxDateTimeEditor', () => {
4444
dateTimeEditor.clear();
4545
expect(dateTimeEditor.value).toBeNull();
4646
expect(dateTimeEditor.valueChange.emit).toHaveBeenCalledTimes(1);
47-
expect(dateTimeEditor.valueChange.emit).toHaveBeenCalledWith({ oldValue: date, newValue: null, userInput: inputDate });
47+
expect(dateTimeEditor.valueChange.emit).toHaveBeenCalledWith(null);
4848
});
4949

5050
it('should update mask according to the input format', () => {
@@ -632,7 +632,7 @@ describe('IgxDateTimeEditor', () => {
632632
date.setMonth(3);
633633
const resultDate =
634634
date.toLocaleString('en-GB', { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' })
635-
.replace(/,/g, '');
635+
.replace(/,/g, '');
636636
result = `${resultDate} ${formatShortTime(date)}`;
637637
expect(inputElement.nativeElement.value).toEqual(result);
638638
}));
@@ -695,7 +695,7 @@ describe('IgxDateTimeEditor', () => {
695695
});
696696
it('should revert to empty mask on clear()', fakeAsync(() => {
697697
const date = new Date(2003, 4, 5);
698-
fixture.componentInstance.date = new Date(2003, 3, 5);
698+
fixture.componentInstance.date = new Date(2003, 3, 5);
699699
fixture.detectChanges();
700700
tick();
701701
const result = formatFullDateTime(date);
@@ -706,7 +706,7 @@ describe('IgxDateTimeEditor', () => {
706706
}));
707707
it('should move the caret to the start/end of the portion with CTRL + arrow left/right keys.', fakeAsync(() => {
708708
const date = new Date(2003, 4, 5);
709-
fixture.componentInstance.date = new Date(2003, 3, 5);
709+
fixture.componentInstance.date = new Date(2003, 3, 5);
710710
fixture.detectChanges();
711711
tick();
712712
const result = formatFullDateTime(date);
@@ -833,10 +833,9 @@ describe('IgxDateTimeEditor', () => {
833833

834834
const year = (newDate.getFullYear().toString()).slice(-2);
835835
const result = [newDate.getDate(), newDate.getMonth() + 1, year].join('/');
836-
const args = { oldValue: undefined, newValue: newDate, userInput: result };
837836
expect(inputElement.nativeElement.value).toEqual(result);
838837
expect(dateTimeEditorDirective.valueChange.emit).toHaveBeenCalledTimes(1);
839-
expect(dateTimeEditorDirective.valueChange.emit).toHaveBeenCalledWith(args);
838+
expect(dateTimeEditorDirective.valueChange.emit).toHaveBeenCalledWith(newDate);
840839
});
841840
it('should fire validationFailed when input date is outside date range.', () => {
842841
fixture.componentInstance.dateTimeFormat = 'dd-MM-yyyy';

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnCh
191191
private onValidatorChange = (...args: any[]) => { };
192192

193193
private get emptyMask(): string {
194-
return this.maskParser.applyMask(this.inputFormat, this.maskOptions);
194+
return this.maskParser.applyMask(null, this.maskOptions);
195195
}
196196

197197
private get targetDatePart(): DatePart {
@@ -524,7 +524,7 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnCh
524524

525525
private parseDate(val: string): Date | null {
526526
if (!val) { return null; }
527-
return DatePickerUtil.parseValueFromMask(val, this._inputDateParts);
527+
return DatePickerUtil.parseValueFromMask(val, this._inputDateParts, this.promptChar);
528528
}
529529

530530
private moveCursor(event: KeyboardEvent): void {

0 commit comments

Comments
 (0)