Skip to content

Commit 2db4363

Browse files
authored
fix(date-range-picker): Consistent focus on open and active day view (#1743)
This PR prevents the focus scroll after opening the dropdown of a date range picker, causing a viewport shift which leads to deteriorated UX. It will also set the active days view index to always be the first one, improving UX after selection is made.
1 parent a944b72 commit 2db4363

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

src/components/calendar/calendar.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,19 +253,21 @@ export default class IgcCalendarComponent extends EventEmitterMixin<
253253
}
254254

255255
/** @private @hidden @internal */
256-
public async [focusActiveDate]() {
256+
public async [focusActiveDate](options?: FocusOptions) {
257257
await this.updateComplete;
258258

259259
if (this._isDayView) {
260-
return this.daysViews.item(this.activeDaysViewIndex).focusActiveDate();
260+
return this.daysViews
261+
.item(this.activeDaysViewIndex)
262+
.focusActiveDate(options);
261263
}
262264

263265
if (this._isMonthView) {
264-
return this.monthsView.focusActiveDate();
266+
return this.monthsView.focusActiveDate(options);
265267
}
266268

267269
if (this._isYearView) {
268-
return this.yearsView.focusActiveDate();
270+
return this.yearsView.focusActiveDate(options);
269271
}
270272
}
271273

src/components/calendar/days-view/days-view.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ export default class IgcDaysViewComponent extends EventEmitterMixin<
147147
}
148148

149149
/** Focuses the active date. */
150-
public focusActiveDate() {
151-
this.activeDay.focus();
150+
public focusActiveDate(options?: FocusOptions) {
151+
this.activeDay.focus(options);
152152
}
153153

154154
protected handleInteraction(event: Event) {

src/components/calendar/months-view/months-view.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ export default class IgcMonthsViewComponent extends EventEmitterMixin<
9999
}
100100

101101
/** Focuses the active date. */
102-
public focusActiveDate() {
103-
this.activeMonth.focus();
102+
public focusActiveDate(options?: FocusOptions) {
103+
this.activeMonth.focus(options);
104104
}
105105

106106
protected handleInteraction(event: Event) {

src/components/calendar/years-view/years-view.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ export default class IgcYearsViewComponent extends EventEmitterMixin<
7979
this.role = 'grid';
8080
}
8181

82-
public focusActiveDate() {
83-
this.activeYear.focus();
82+
public focusActiveDate(options?: FocusOptions) {
83+
this.activeYear.focus(options);
8484
}
8585

8686
protected handleInteraction(event: Event) {

src/components/date-range-picker/date-range-picker.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -763,11 +763,10 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
763763
}
764764

765765
protected override async handleAnchorClick() {
766-
this._calendar.activeDate =
767-
this._firstDefinedInRange ?? this._calendar.activeDate;
768766
super.handleAnchorClick();
767+
this._setCalendarActiveDateAndViewIndex();
769768
await this.updateComplete;
770-
this._calendar[focusActiveDate]();
769+
this._calendar[focusActiveDate]({ preventScroll: true });
771770
}
772771

773772
protected async _handleCalendarChangeEvent(event: CustomEvent<Date>) {
@@ -806,6 +805,18 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
806805
this.value = this._oldValue;
807806
}
808807

808+
/**
809+
* Sets the active date of the calendar based on current selection, if any,
810+
* or its current active date and its active day view index to always be the first one.
811+
*/
812+
private _setCalendarActiveDateAndViewIndex() {
813+
const activeDaysViewIndex = 'activeDaysViewIndex';
814+
815+
this._calendar.activeDate =
816+
this._firstDefinedInRange ?? this._calendar.activeDate;
817+
this._calendar[activeDaysViewIndex] = 0;
818+
}
819+
809820
private _getUpdatedDateRange(
810821
input: IgcDateTimeInputComponent,
811822
newValue: Date | null

0 commit comments

Comments
 (0)