Skip to content

Commit 0a0e710

Browse files
committed
refactor(time-picker): add input/display format validation #6482
1 parent e155b2e commit 0a0e710

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

projects/igniteui-angular/src/lib/time-picker/time-picker.component.ts

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export class IgxTimePickerComponent extends PickerBaseDirective
130130
*
131131
*/
132132
@Input()
133-
public displayFormat: string;
133+
public displayFormat: string
134134

135135
/**
136136
* The expected user input format and placeholder.
@@ -140,7 +140,7 @@ export class IgxTimePickerComponent extends PickerBaseDirective
140140
*
141141
* @example
142142
* ```html
143-
* <igx-time-picker inputFormat="HH:mm tt"></igx-time-picker>
143+
* <igx-time-picker inputFormat="HH:mm"></igx-time-picker>
144144
* ```
145145
*/
146146
@Input()
@@ -246,38 +246,25 @@ export class IgxTimePickerComponent extends PickerBaseDirective
246246
public selected = new EventEmitter<Date>();
247247

248248
/**
249-
* Emitted when selection is made. The event contains the selected value. Returns {`oldValue`: `Date | string`, `newValue`: `Date |string`}.
250-
* ```typescript
251-
* @ViewChild("toast")
252-
* private toast: IgxToastComponent;
253-
* public valueChange(timepicker){
254-
* this.toast.open()
255-
* }
256-
* //...
257-
* ```
258-
* ```html
259-
* <igx-time-picker (valueChange)="valueChange($event)"></igx-time-picker>
260-
* <igx-toast #toast message="The value has been changed!"></igx-toast>
249+
* Emitted when the picker's value changes.
250+
*
251+
* @remarks
252+
* Used for `two-way` bindings.
253+
*
254+
* @example
255+
* ```html
256+
* <igx-time-picker [(value)]="date"></igx-time-picker>
261257
* ```
262258
*/
263259
@Output()
264260
public valueChange = new EventEmitter<Date>();
265261

266262
/**
267-
* It emits when the picker's value goes outside of a given min/max range. Returns {`timePicker`: `any`, `currentValue`: `Date | string`, `setThroughUI`: `boolean`}
268-
* ```typescript
269-
* public min: string = "09:00";
270-
* public max: string = "18:00";
271-
* @ViewChild("toast")
272-
* private toast: IgxToastComponent;
273-
* public validationFailed(timepicker){
274-
* this.toast.open();
275-
* }
276-
* //...
277-
* ```
278-
* ```html
279-
* <igx-time-picker [minValue]="min" [maxValue]="max" (validationFailed)="validationFailed($event)"></igx-time-picker>
280-
* <igx-toast #toast message="Value must be between 09:00 and 18:00!"></igx-toast>
263+
* Emitted when the user types/spins invalid time in the time-picker editor.
264+
*
265+
* @example
266+
* ```html
267+
* <igx-time-picker (validationFailed)="onValidationFailed($event)"></igx-time-picker>
281268
* ```
282269
*/
283270
@Output()
@@ -467,6 +454,8 @@ export class IgxTimePickerComponent extends PickerBaseDirective
467454
private _oldValue: Date | string;
468455
private _minDropdownValue: Date;
469456
private _maxDropdownValue: Date;
457+
private _inputFormat: string;
458+
private _displayFormat: string;
470459
private _resourceStrings = CurrentResourceStrings.TimePickerResStrings;
471460
private _okButtonLabel = null;
472461
private _cancelButtonLabel = null;
@@ -702,6 +691,9 @@ export class IgxTimePickerComponent extends PickerBaseDirective
702691
this.dateTimeEditor.displayFormat = this.displayFormat;
703692
}
704693
if (changes['inputFormat'] && this.dateTimeEditor) {
694+
if (this.validateFormat(this.inputFormat)) {
695+
this.inputFormat = DateTimeUtil.DEFAULT_TIME_INPUT_FORMAT;
696+
}
705697
this.dateTimeEditor.inputFormat = this.inputFormat;
706698
}
707699
}
@@ -766,13 +758,13 @@ export class IgxTimePickerComponent extends PickerBaseDirective
766758
}
767759
}
768760

769-
/**
770-
* Clears the time picker value if it is a `string` or resets the time to `00:00:00` if the value is a Date object.
771-
* @example
772-
* ```typescript
773-
* this.timePicker.clear();
774-
* ```
775-
*/
761+
/**
762+
* Clears the time picker value if it is a `string` or resets the time to `00:00:00` if the value is a Date object.
763+
* @example
764+
* ```typescript
765+
* this.timePicker.clear();
766+
* ```
767+
*/
776768
public clear(): void {
777769
if (this.disabled) {
778770
return;
@@ -1013,7 +1005,7 @@ export class IgxTimePickerComponent extends PickerBaseDirective
10131005
this.value = this._selectedDate;
10141006
}
10151007
}
1016-
1008+
10171009
/** @hidden @internal */
10181010
public hoursInView(): string[] {
10191011
return this._hourView.filter((hour) => hour !== '');
@@ -1415,6 +1407,13 @@ export class IgxTimePickerComponent extends PickerBaseDirective
14151407
return hour;
14161408
}
14171409

1410+
private validateFormat(format: string): boolean {
1411+
return (format.indexOf('d') < 0 && format.indexOf('M') < 0 &&
1412+
format.indexOf('Y') < 0 && format.indexOf('y') < 0 &&
1413+
format.indexOf('L') < 0 && format.indexOf('E') < 0 &&
1414+
format.indexOf('W') < 0 && format.indexOf('w') < 0);
1415+
}
1416+
14181417
private attachOnKeydown(): void {
14191418
fromEvent(this.getEditElement(), 'keydown')
14201419
.pipe(takeUntil(this._destroy$))

0 commit comments

Comments
 (0)