Skip to content

Commit 8d6b577

Browse files
Calendar: Included dates are highlighted in calendar multi-view mode (T1253076) (#28439)
1 parent ea0af4f commit 8d6b577

File tree

4 files changed

+134
-6
lines changed

4 files changed

+134
-6
lines changed

packages/devextreme/js/__internal/ui/calendar/m_calendar.range.selection.strategy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ class CalendarRangeSelectionStrategy extends CalendarSelectionStrategy {
117117

118118
const { currentDate, viewsCount } = this.calendar.option();
119119
const isAdditionalViewDate = this.calendar._isAdditionalViewDate(currentDate);
120-
const firstDateInViews = dateUtils.getFirstMonthDate(dateUtils.addDateInterval(currentDate, 'month', isAdditionalViewDate ? -2 : -1));
121-
const lastDateInViews = dateUtils.getLastMonthDate(dateUtils.addDateInterval(currentDate, 'month', isAdditionalViewDate ? 1 : viewsCount));
120+
const firstDateInViews = dateUtils.getFirstMonthDate(currentDate, isAdditionalViewDate ? -2 : -1);
121+
const lastDateInViews = dateUtils.getLastMonthDate(currentDate, isAdditionalViewDate ? 1 : viewsCount);
122122

123123
// @ts-expect-error
124124
const rangeStartDate = new Date(Math.max(firstDateInViews, startDate));

packages/devextreme/js/core/utils/date.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,14 +466,24 @@ const getShortDateFormat = function() {
466466
return 'yyyy/MM/dd';
467467
};
468468

469-
const getFirstMonthDate = function(date) {
469+
const getFirstMonthDate = function(date, offset = 0) {
470470
if(!isDefined(date)) return;
471-
return createDateWithFullYear(date.getFullYear(), date.getMonth(), 1);
471+
472+
const currentDate = new Date(date.getTime());
473+
const month = currentDate.getMonth() + offset;
474+
currentDate.setMonth(month);
475+
476+
return createDateWithFullYear(currentDate.getFullYear(), month, 1);
472477
};
473478

474-
const getLastMonthDate = function(date) {
479+
const getLastMonthDate = function(date, offset = 0) {
475480
if(!isDefined(date)) return;
476-
return createDateWithFullYear(date.getFullYear(), date.getMonth() + 1, 0);
481+
482+
const currentDate = new Date(date.getTime());
483+
const month = currentDate.getMonth() + offset;
484+
currentDate.setMonth(month);
485+
486+
return createDateWithFullYear(currentDate.getFullYear(), month + 1, 0);
477487
};
478488

479489
function getFirstWeekDate(date, firstDayOfWeek) {

packages/devextreme/testing/tests/DevExpress.core/utils.date.tests.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,106 @@ QUnit.test('getIntervalByString second', function(assert) {
107107
assert.deepEqual(this.getDateIntervalByString('second'), { seconds: 1 });
108108
});
109109

110+
QUnit.module('getFirstMonthDate', () => {
111+
QUnit.test('should return same month first date when offset is not provided', function(assert) {
112+
const newDate = dateUtils.getFirstMonthDate(new Date(2025, 11, 15));
113+
114+
assert.deepEqual(newDate, new Date(2025, 11, 1));
115+
});
116+
117+
QUnit.test('should return same month first date when offset is 0', function(assert) {
118+
const newDate = dateUtils.getFirstMonthDate(new Date(2025, 11, 15), 0);
119+
120+
assert.deepEqual(newDate, new Date(2025, 11, 1));
121+
});
122+
123+
QUnit.test('should decrease month correctly', function(assert) {
124+
const newDate = dateUtils.getFirstMonthDate(new Date(2025, 11, 31), -1);
125+
126+
assert.deepEqual(newDate, new Date(2025, 10, 1));
127+
});
128+
129+
QUnit.test('should increase month correctly', function(assert) {
130+
const newDate = dateUtils.getFirstMonthDate(new Date(2025, 8, 30), 1);
131+
132+
assert.deepEqual(newDate, new Date(2025, 9, 1));
133+
});
134+
135+
QUnit.test('should decrease month correctly when offset is less than -1', function(assert) {
136+
const newDate = dateUtils.getFirstMonthDate(new Date(2025, 11, 15), -2);
137+
138+
assert.deepEqual(newDate, new Date(2025, 9, 1));
139+
});
140+
141+
QUnit.test('should increase month correctly when offset is greater than 1', function(assert) {
142+
const newDate = dateUtils.getFirstMonthDate(new Date(2025, 8, 15), 2);
143+
144+
assert.deepEqual(newDate, new Date(2025, 10, 1));
145+
});
146+
147+
QUnit.test('should assign correct new date when current year is increased', function(assert) {
148+
const newDate = dateUtils.getFirstMonthDate(new Date(2025, 11, 15), 1);
149+
150+
assert.deepEqual(newDate, new Date(2026, 0, 1));
151+
});
152+
153+
QUnit.test('should assign correct new date when current year is decreased', function(assert) {
154+
const newDate = dateUtils.getFirstMonthDate(new Date(2025, 0, 1), -1);
155+
156+
assert.deepEqual(newDate, new Date(2024, 11, 1));
157+
});
158+
});
159+
160+
QUnit.module('getLastMonthDate', () => {
161+
QUnit.test('should return same month last date when offset is not provided', function(assert) {
162+
const newDate = dateUtils.getLastMonthDate(new Date(2025, 11, 15));
163+
164+
assert.deepEqual(newDate, new Date(2025, 11, 31));
165+
});
166+
167+
QUnit.test('should return same month last date when offset is 0', function(assert) {
168+
const newDate = dateUtils.getLastMonthDate(new Date(2025, 11, 15), 0);
169+
170+
assert.deepEqual(newDate, new Date(2025, 11, 31));
171+
});
172+
173+
QUnit.test('should decrease month correctly', function(assert) {
174+
const newDate = dateUtils.getLastMonthDate(new Date(2025, 11, 31), -1);
175+
176+
assert.deepEqual(newDate, new Date(2025, 10, 30));
177+
});
178+
179+
QUnit.test('should increase month correctly', function(assert) {
180+
const newDate = dateUtils.getLastMonthDate(new Date(2025, 8, 30), 1);
181+
182+
assert.deepEqual(newDate, new Date(2025, 9, 31));
183+
});
184+
185+
QUnit.test('should decrease month correctly when offset is less than -1', function(assert) {
186+
const newDate = dateUtils.getLastMonthDate(new Date(2025, 11, 15), -2);
187+
188+
assert.deepEqual(newDate, new Date(2025, 9, 31));
189+
});
190+
191+
QUnit.test('should increase month correctly when offset is greater than 1', function(assert) {
192+
const newDate = dateUtils.getLastMonthDate(new Date(2025, 8, 15), 2);
193+
194+
assert.deepEqual(newDate, new Date(2025, 10, 30));
195+
});
196+
197+
QUnit.test('should assign correct new date when current year is increased', function(assert) {
198+
const newDate = dateUtils.getLastMonthDate(new Date(2025, 11, 15), 1);
199+
200+
assert.deepEqual(newDate, new Date(2026, 0, 31));
201+
});
202+
203+
QUnit.test('should assign correct new date when current year is decreased', function(assert) {
204+
const newDate = dateUtils.getLastMonthDate(new Date(2025, 0, 1), -1);
205+
206+
assert.deepEqual(newDate, new Date(2024, 11, 31));
207+
});
208+
});
209+
110210
QUnit.test('add negative Interval number', function(assert) {
111211
// arrange, act
112212
const newNumber = dateUtils.addInterval(11, 5, true);

packages/devextreme/testing/tests/DevExpress.ui.widgets.editors/calendar.tests.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,6 +2151,24 @@ QUnit.module('Options', {
21512151
assert.ok($cell.hasClass(CALENDAR_CELL_IN_RANGE_CLASS));
21522152
});
21532153

2154+
2155+
QUnit.test(`Cells between startDate and endDate should have ${CALENDAR_CELL_IN_RANGE_CLASS} class even after currentDate runtime change (T1253076)`, function(assert) {
2156+
this.reinit({
2157+
value: ['2025/01/01', '2025/12/31'],
2158+
selectionMode: 'range',
2159+
viewsCount: 2,
2160+
});
2161+
2162+
this.calendar.option('currentDate', new Date('2025-12-31'));
2163+
2164+
const $prevButton = $(this.$element.find(toSelector(CALENDAR_NAVIGATOR_PREVIOUS_VIEW_CLASS)));
2165+
$prevButton.trigger('dxclick');
2166+
2167+
const $cell = $(getCurrentViewInstance(this.calendar).$element().find('*[data-value="2025/11/01"]'));
2168+
2169+
assert.ok($cell.hasClass(CALENDAR_CELL_IN_RANGE_CLASS), 'cell is highlighted');
2170+
});
2171+
21542172
QUnit.test('Should reselect startDate and clear endDate on click when both value are defined', function(assert) {
21552173
const expectedValue = [new Date('2023/01/11'), null];
21562174
const $cell = $(getCurrentViewInstance(this.calendar).$element().find('*[data-value="2023/01/11"]'));

0 commit comments

Comments
 (0)