Skip to content

Commit 677eb48

Browse files
committed
refactor(time-picker): remove spinDelta #6482
1 parent e9794ec commit 677eb48

File tree

2 files changed

+40
-47
lines changed

2 files changed

+40
-47
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!-- [preventSpinOnWheel]="!this.isDropdown" -->
22
<igx-input-group [suppressInputAutofocus]="this.isDropdown" (click)="!this.isDropdown && this.open()">
33
<input igxInput [igxDateTimeEditor]="this.inputFormat" type="text" [readonly]="!this.isDropdown"
4-
[spinDelta]="this.spinDelta" [disabled]="this.disabled" [displayFormat]="this.displayFormat"
4+
[spinDelta]="this.itemsDelta" [disabled]="this.disabled" [displayFormat]="this.displayFormat"
55
[placeholder]="this.placeholder" role="combobox" aria-haspopup="dialog"
66
[attr.aria-expanded]="!this.toggleDirective.collapsed" [attr.aria-labelledby]="this.label?.id" />
77

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

+39-46
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import { IgxDateTimeEditorModule, IgxDateTimeEditorDirective } from '../directiv
4444
import { IgxToggleModule, IgxToggleDirective } from '../directives/toggle/toggle.directive';
4545
import { ITimePickerResourceStrings } from '../core/i18n/time-picker-resources';
4646
import { CurrentResourceStrings } from '../core/i18n/resources';
47-
import { KEYS, IBaseEventArgs, isEqual } from '../core/utils';
47+
import { KEYS, IBaseEventArgs, isEqual, isDate } from '../core/utils';
4848
import { PickerInteractionMode } from '../date-common/types';
4949
import { IgxTextSelectionModule } from '../directives/text-selection/text-selection.directive';
5050
import { IgxLabelDirective } from '../directives/label/label.directive';
@@ -54,6 +54,7 @@ import { DatePart, DatePartDeltas } from '../directives/date-time-editor/public_
5454
import { PickerHeaderOrientation } from '../date-common/types';
5555
import { IgxPickerToggleComponent } from '../date-common/picker-icons.common';
5656
import { TimeFormatPipe } from './time-picker.pipes';
57+
import { defaultCipherList } from 'constants';
5758

5859

5960
let NEXT_ID = 0;
@@ -144,7 +145,7 @@ export class IgxTimePickerComponent extends PickerBaseDirective
144145
* ```
145146
*/
146147
@Input()
147-
public inputFormat : string = DateTimeUtil.DEFAULT_TIME_INPUT_FORMAT;
148+
public inputFormat: string = DateTimeUtil.DEFAULT_TIME_INPUT_FORMAT;
148149

149150
/**
150151
* Gets/Sets the interaction mode - dialog or drop down.
@@ -222,18 +223,6 @@ export class IgxTimePickerComponent extends PickerBaseDirective
222223
@Input()
223224
public headerOrientation: PickerHeaderOrientation = PickerHeaderOrientation.Horizontal;
224225

225-
/**
226-
* Delta values used to increment or decrement each editor time part on spin actions.
227-
* All values default to `1`.
228-
*
229-
* @example
230-
* ```html
231-
* <igx-time-picker [spinDelta]="{ hour: 2, minute: 20 }"></igx-time-picker>
232-
* ```
233-
*/
234-
@Input()
235-
public spinDelta: Pick<DatePartDeltas, 'hour' | 'minute' | 'second'>;
236-
237226
/**
238227
* Emitted after a selection has been done.
239228
*
@@ -257,7 +246,7 @@ export class IgxTimePickerComponent extends PickerBaseDirective
257246
* ```
258247
*/
259248
@Output()
260-
public valueChange = new EventEmitter<Date>();
249+
public valueChange = new EventEmitter<Date | string>();
261250

262251
/**
263252
* Emitted when the user types/spins invalid time in the time-picker editor.
@@ -515,14 +504,14 @@ export class IgxTimePickerComponent extends PickerBaseDirective
515504
*/
516505
@Input()
517506
public set value(value: Date | string) {
518-
const oldValue = this._dateValue;
507+
const oldValue = this._value;
519508
this._value = value;
520509
this._dateValue = this.parseToDate(value);
521-
if (this.dateTimeEditor.value !== value) {
522-
this.dateTimeEditor.value = value;
510+
if (this.dateTimeEditor.value !== this._dateValue) {
511+
this.dateTimeEditor.value = this._dateValue;
523512
}
524-
this.emitValueChange(oldValue, this._dateValue);
525-
this._onChangeCallback(this._dateValue);
513+
this.emitValueChange(oldValue, this._value);
514+
this._onChangeCallback(this._value);
526515
}
527516

528517
/**
@@ -691,9 +680,7 @@ export class IgxTimePickerComponent extends PickerBaseDirective
691680
this.dateTimeEditor.displayFormat = this.displayFormat;
692681
}
693682
if (changes['inputFormat'] && this.dateTimeEditor) {
694-
if (this.validateFormat(this.inputFormat)) {
695-
this.inputFormat = DateTimeUtil.DEFAULT_TIME_INPUT_FORMAT;
696-
}
683+
this.inputFormat = this.hasDateParts() ? DateTimeUtil.DEFAULT_TIME_INPUT_FORMAT : this.inputFormat;
697684
this.dateTimeEditor.inputFormat = this.inputFormat;
698685
}
699686
}
@@ -774,11 +761,13 @@ export class IgxTimePickerComponent extends PickerBaseDirective
774761
this.close();
775762
}
776763

777-
const value = this._dateValue;
778-
value.setHours(0, 0, 0);
779-
this.value = value;
780-
this.dateTimeEditor.value = new Date(this.value);
781-
this.setSelectedValue();
764+
if (isDate(this.value)) {
765+
this.value.setHours(0, 0, 0);
766+
this.dateTimeEditor.value = new Date(this.value);
767+
this.setSelectedValue();
768+
} else {
769+
this.value = null;
770+
}
782771
}
783772

784773
/**
@@ -868,7 +857,6 @@ export class IgxTimePickerComponent extends PickerBaseDirective
868857
this.updateSelectedAmpm(previousDate);
869858
this.updateSelectedMinutes();
870859
this.updateSelectedSeconds();
871-
this.value = this._selectedDate;
872860
}
873861
break;
874862
case 'minuteList': {
@@ -878,7 +866,6 @@ export class IgxTimePickerComponent extends PickerBaseDirective
878866
this._minuteView = this.scrollListItem(minutes, this._minuteItems, DatePart.Minutes);
879867
this._selectedDate = date;
880868
this.updateSelectedSeconds();
881-
this.value = this._selectedDate;
882869
break;
883870
}
884871
case 'secondsList': {
@@ -887,7 +874,6 @@ export class IgxTimePickerComponent extends PickerBaseDirective
887874
if (this.valueInRange(date, this._minDropdownValue, this._maxDropdownValue)) {
888875
this._secondsView = this.scrollListItem(seconds, this._secondsItems, DatePart.Seconds);
889876
this._selectedDate = date;
890-
this.value = this._selectedDate;
891877
}
892878
break;
893879
}
@@ -902,10 +888,10 @@ export class IgxTimePickerComponent extends PickerBaseDirective
902888
this._selectedDate = date;
903889
this.updateSelectedMinutes();
904890
this.updateSelectedSeconds();
905-
this.value = this._selectedDate;
906891
break;
907892
}
908893
}
894+
this.value = isDate(this.value) ? this._selectedDate : this.toISOString(this._selectedDate);
909895
}
910896

911897
/** @hidden @internal */
@@ -934,7 +920,7 @@ export class IgxTimePickerComponent extends PickerBaseDirective
934920
this.updateSelectedAmpm(previousDate);
935921

936922
this._selectedDate = new Date(this._selectedDate);
937-
this.value = this._selectedDate;
923+
this.value = isDate(this.value) ? this._selectedDate : this.toISOString(this._selectedDate);
938924
}
939925

940926
/** @hidden @internal */
@@ -957,7 +943,7 @@ export class IgxTimePickerComponent extends PickerBaseDirective
957943

958944
this._minuteView = this.scrollListItem(minutes, this._minuteItems, DatePart.Minutes);
959945
this._selectedDate = new Date(this._selectedDate);
960-
this.value = this._selectedDate;
946+
this.value = isDate(this.value) ? this._selectedDate : this.toISOString(this._selectedDate);
961947
}
962948

963949
/** @hidden @internal */
@@ -978,7 +964,7 @@ export class IgxTimePickerComponent extends PickerBaseDirective
978964

979965
this._secondsView = this.scrollListItem(seconds, this._secondsItems, DatePart.Seconds);
980966
this._selectedDate = new Date(this._selectedDate);
981-
this.value = this._selectedDate;
967+
this.value = isDate(this.value) ? this._selectedDate : this.toISOString(this._selectedDate);
982968
}
983969

984970
/** @hidden @internal */
@@ -1002,7 +988,7 @@ export class IgxTimePickerComponent extends PickerBaseDirective
1002988
this._ampmView = this.scrollListItem(ampm, this._ampmItems, DatePart.AmPm);
1003989

1004990
this._selectedDate = new Date(this._selectedDate);
1005-
this.value = this._selectedDate;
991+
this.value = isDate(this.value) ? this._selectedDate : this.toISOString(this._selectedDate);
1006992
}
1007993
}
1008994

@@ -1207,7 +1193,7 @@ export class IgxTimePickerComponent extends PickerBaseDirective
12071193
}
12081194

12091195
private initializeContainer() {
1210-
this.value = this._selectedDate;
1196+
this.value = isDate(this.value) ? this._selectedDate : this.toISOString(this._selectedDate);
12111197
this._onTouchedCallback();
12121198

12131199
if (this.showHoursList) {
@@ -1309,7 +1295,7 @@ export class IgxTimePickerComponent extends PickerBaseDirective
13091295
return spinDelta;
13101296
}
13111297

1312-
private emitValueChange(oldValue: Date, newValue: Date) {
1298+
private emitValueChange(oldValue: Date | string, newValue: Date | string) {
13131299
if (!isEqual(oldValue, newValue)) {
13141300
this.valueChange.emit(newValue);
13151301
}
@@ -1383,6 +1369,14 @@ export class IgxTimePickerComponent extends PickerBaseDirective
13831369
return DateTimeUtil.isValidDate(value) ? value : DateTimeUtil.parseIsoDate(value);
13841370
}
13851371

1372+
private toISOString(value: Date): string {
1373+
return value.toLocaleTimeString("en-GB", {
1374+
hour: "2-digit",
1375+
minute: "2-digit",
1376+
second: "2-digit",
1377+
});
1378+
}
1379+
13861380
private isTimePart(datePart: DatePart): boolean {
13871381
return (datePart === DatePart.Hours || datePart === DatePart.Minutes || datePart === DatePart.Seconds || datePart === DatePart.AmPm);
13881382
}
@@ -1407,11 +1401,12 @@ export class IgxTimePickerComponent extends PickerBaseDirective
14071401
return hour;
14081402
}
14091403

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);
1404+
private hasDateParts(): boolean {
1405+
const inputDateParts = DateTimeUtil.parseDateTimeFormat(this.inputFormat);
1406+
return inputDateParts.some(
1407+
p => p.type === DatePart.Date
1408+
|| p.type === DatePart.Month
1409+
|| p.type === DatePart.Year);
14151410
}
14161411

14171412
private attachOnKeydown(): void {
@@ -1442,9 +1437,7 @@ export class IgxTimePickerComponent extends PickerBaseDirective
14421437

14431438
this.dateTimeEditor.valueChange.pipe(
14441439
takeUntil(this._destroy$)).subscribe(date => {
1445-
if (!isEqual(date, this._dateValue)) {
1446-
this.value = date;
1447-
}
1440+
this.value = isDate(this.value) ? this.parseToDate(date) : isDate(date) ? this.toISOString(date) : date;
14481441
this.setSelectedValue();
14491442
});
14501443

0 commit comments

Comments
 (0)