Skip to content

Commit e289b14

Browse files
ChronosSFkdinev
andauthored
fix(timepicker): allowing better scroll on touchpads #6292 - 20.0 (#15875)
* fix(timepicker): allowing better scroll on touchpads #6292 * fix(pickers): adding logic for date editor/picker, slowing scroll down further * test(date-picker): changing test to accommodate wheel accumulator --------- Co-authored-by: Konstantin Dinev <[email protected]>
1 parent 1ccf222 commit e289b14

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,13 @@ describe('IgxDateTimeEditor', () => {
11231123
inputElement.triggerEventHandler('focus', {});
11241124
fixture.detectChanges();
11251125
dateTimeEditorDirective.nativeElement.setSelectionRange(1, 1);
1126-
inputElement.triggerEventHandler('wheel', new WheelEvent('wheel', { deltaY: 1 }));
1126+
// typical wheel scrolls are 120px and the date-editor employs touchpad-friendly implementation
1127+
// that accumulates to 50 before incrementing/decrementing
1128+
// we'll test the behavior by doing two scrolls with the first one not expected to trigger a change
1129+
inputElement.triggerEventHandler('wheel', new WheelEvent('wheel', { deltaY: 20 }));
1130+
fixture.detectChanges();
1131+
expect(dateTimeEditorDirective.value.getDate()).toEqual(today.getDate());
1132+
inputElement.triggerEventHandler('wheel', new WheelEvent('wheel', { deltaY: 40 }));
11271133
fixture.detectChanges();
11281134
expect(dateTimeEditorDirective.value.getDate()).toEqual(today.getDate() - 1);
11291135
}));

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,10 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnCh
228228
@Output()
229229
public validationFailed = new EventEmitter<IgxDateTimeEditorEventArgs>();
230230

231+
232+
private readonly SCROLL_THRESHOLD = 50;
231233
private _inputFormat: string;
234+
private _scrollAccumulator = 0;
232235
private _displayFormat: string;
233236
private _oldValue: Date;
234237
private _dateValue: Date;
@@ -313,10 +316,14 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnCh
313316
}
314317
event.preventDefault();
315318
event.stopPropagation();
316-
if (event.deltaY > 0) {
317-
this.decrement();
318-
} else {
319-
this.increment();
319+
this._scrollAccumulator += event.deltaY;
320+
if (Math.abs(this._scrollAccumulator) > this.SCROLL_THRESHOLD) {
321+
if (this._scrollAccumulator > 0) {
322+
this.decrement();
323+
} else {
324+
this.increment();
325+
}
326+
this._scrollAccumulator = 0;
320327
}
321328
}
322329

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ export class IgxItemListDirective implements OnInit, OnDestroy {
3434

3535
public isActive: boolean;
3636

37+
private readonly SCROLL_THRESHOLD = 50;
38+
private readonly PAN_THRESHOLD = 10;
39+
40+
/**
41+
* accumulates wheel scrolls and triggers a change action above SCROLL_THRESHOLD
42+
*/
43+
private scrollAccumulator = 0;
44+
3745
constructor(
3846
@Inject(IGX_TIME_PICKER_COMPONENT) public timePicker: IgxTimePickerBase,
3947
private elementRef: ElementRef,
@@ -170,24 +178,35 @@ export class IgxItemListDirective implements OnInit, OnDestroy {
170178
event.preventDefault();
171179
event.stopPropagation();
172180

173-
const delta = event.deltaY;
174-
if (delta !== 0) {
175-
this.nextItem(delta);
181+
this.scrollAccumulator += event.deltaY;
182+
if (Math.abs(this.scrollAccumulator) > this.SCROLL_THRESHOLD) {
183+
this.nextItem(this.scrollAccumulator);
184+
this.scrollAccumulator = 0;
176185
}
177186
}
178187

179188
/**
180189
* @hidden @internal
181190
*/
182191
public ngOnInit() {
183-
const hammerOptions: HammerOptions = { recognizers: [[HammerGesturesManager.Hammer?.Pan, { direction: HammerGesturesManager.Hammer?.DIRECTION_VERTICAL, threshold: 10 }]] };
192+
const hammerOptions: HammerOptions = {
193+
recognizers: [
194+
[
195+
HammerGesturesManager.Hammer?.Pan,
196+
{
197+
direction: HammerGesturesManager.Hammer?.DIRECTION_VERTICAL,
198+
threshold: this.PAN_THRESHOLD
199+
}
200+
]
201+
]
202+
};
184203
this.touchManager.addEventListener(this.elementRef.nativeElement, 'pan', this.onPanMove, hammerOptions);
185204
}
186205

187206
/**
188207
* @hidden @internal
189208
*/
190-
public ngOnDestroy() {
209+
public ngOnDestroy() {
191210
this.touchManager.destroy();
192211
}
193212

@@ -355,7 +374,7 @@ export class IgxTimeItemDirective {
355374
private getHourPart(date: Date): string {
356375
const inputDateParts = DateTimeUtil.parseDateTimeFormat(this.timePicker.appliedFormat);
357376
const hourPart = inputDateParts.find(element => element.type === 'hours');
358-
const ampmPart = inputDateParts.find(element =>element.format.indexOf('a') !== -1 || element.format === 'tt');
377+
const ampmPart = inputDateParts.find(element => element.format.indexOf('a') !== -1 || element.format === 'tt');
359378
const hour = DateTimeUtil.getPartValue(date, hourPart, hourPart.format.length);
360379
if (ampmPart) {
361380
const ampm = DateTimeUtil.getPartValue(date, ampmPart, ampmPart.format.length);

0 commit comments

Comments
 (0)