Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions packages/components/calendar/__test__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,49 @@ describe('calendar', () => {

expect(instance.data.currentMonth[0].months[0].suffix).toBe('updated');
});

describe('scrollIntoView', () => {
const minDate = new Date(2016, 10, 1).getTime();
const maxDate = new Date(2026, 11, 1).getTime();
const rangeValue = [new Date(2022, 11, 5).getTime(), new Date(2022, 11, 10).getTime()];

let component;
const renderRange = (props = {}) => {
component = simulate.render(calendar, {
minDate,
maxDate,
value: rangeValue,
type: 'range',
usePopup: true,
visible: false,
...props,
});
component.attach(document.createElement('parent-wrapper'));
return component;
};

afterEach(() => component?.detach());

it('positions on the selected month when opened (issue #4718)', async () => {
renderRange();
await simulate.sleep(20);

const inst = component.instance;

// 打开弹窗
inst.setData({ visible: true });
await simulate.sleep(30);

const { currentMonth, scrollIntoView } = inst.data;

// 选中区间在 2022 年 12 月,面板必须定位到该月
expect(currentMonth[0]).toEqual(expect.objectContaining({ year: 2022, month: 11 }));
expect(scrollIntoView).toBe('year_2022_month_11');

// 区间标记也要落在正确的日期上,否则定位到月份但高亮仍然错位
const days = currentMonth[0].months;
expect(days.find((d) => d.type === 'start')?.day).toBe(5);
expect(days.find((d) => d.type === 'end')?.day).toBe(10);
});
});
});
6 changes: 5 additions & 1 deletion packages/components/calendar/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,11 @@ export default class Calendar extends SuperComponent {
const months = this.base.getMonths();

if (this.data.switchMode === 'none') {
this.setData({ months });
// switchMode 为 none 时虽然不展示月份切换按钮,但面板仍需要定位到选中的月份。
// 这里之前只设置 months,currentMonth 会一直停留在初始的空对象上,
// 导致跨度过大时打开面板无法定位到选中区间(issue #4718)。
const { currentMonth } = this.getCurrentMonth(newValue, months);
this.setData({ months, currentMonth });
return;
}

Expand Down