Skip to content

fix(calendar): compute currentMonth when switchMode is none (issue #4718) - #4720

Open
lnuxe wants to merge 2 commits into
Tencent:developfrom
lnuxe:fix/calendar-current-month-none-mode
Open

lnuxe wants to merge 2 commits into
Tencent:developfrom
lnuxe:fix/calendar-current-month-none-mode

Conversation

@lnuxe

@lnuxe lnuxe commented Nov 11, 2026 •

Copy link
Copy Markdown

🤔 这个 PR 的性质是?

  • 日常 bug 修复
  • 新特性提交
  • 文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • CI/CD 改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他

🔗 相关 Issue

close #4718

💡 需求背景和解决方案

问题

issue #4718:区间模式下,minDate 与 maxDate 跨度很大时(例:2016-11 ~ 2026-12,共 122 个月),打开面板无法定位到选中的日期区间。

复现参数:

value: [new Date(2022, 11, 5).getTime(), new Date(2022, 11, 10).getTime()],
minDate: new Date(2016, 10, 1).getTime(),
maxDate: new Date(2026, 12, 1).getTime(),
type: 'range',

根因

calcMonths 在 switchMode === 'none'(默认值)时提前返回,只设置 months,不设置 currentMonth:

calcMonths(newValue?: TCalendarValue) {
  const months = this.base.getMonths();

  if (this.data.switchMode === 'none') {
    this.setData({ months });
    return;                        // ← currentMonth 从未被赋值
  }
  ...
}

而 currentMonth 的初始值是空对象 {}(calendar.ts 的 data)。面板定位、区间高亮都依赖它,于是:

months.length   = 122                  ← 月份算对了
scrollIntoView  = year_2022_month_11   ← 滚动目标也算对了
currentMonth    = {}                   ← 一直是空的

跨度小时不容易察觉,因为默认落在相邻月份;跨度大时就会明显停在错误位置。

修改

switchMode === 'none' 分支同样推导 currentMonth,只是不动月份切换按钮(updateActionButton 保持不调用,因为该模式下按钮不展示):

if (this.data.switchMode === 'none') {
  // switchMode 为 none 时虽然不展示月份切换按钮,但面板仍需要定位到选中的月份。
  const { currentMonth } = this.getCurrentMonth(newValue, months);
  this.setData({ months, currentMonth });
  return;
}

验证

新增 packages/components/calendar/__test__/scroll-into-view.test.js:用 issue 给的实际参数(跨度 122 个月),visible 从 false 切到 true,断言:

  • currentMonth[0] 是 { year: 2022, month: 11 };
  • scrollIntoView 是 year_2022_month_11;
  • 区间标记正确:start.day === 5、end.day === 10。

反向验证:还原 calendar.ts 后跑:

× positions on the selected month when opened (issue #4718)
  Expected: ObjectContaining {"month": 11, "year": 2022}
  Received: undefined
1 failed | 15 passed

应用修复后 calendar 全量 16 passed(3 个测试文件)。

fix #4718

📝 更新日志

  • 本条 PR 不需要纳入 Changelog

tdesign-miniprogram

☑️ 请求合并前的自查清单

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充
  • Changelog 已提供或无须提供

};
},

onInput(event) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

与本pr目标修复无关的其他修复,单独提 pr

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已移除。本 PR 现在只包含 packages/components/calendar/calendar.ts 与 packages/components/calendar/__test__/index.test.js 两个文件,textarea 的改动已从分支中剔除,不再出现在 diff 里。

@@ -0,0 +1,49 @@
import path from 'path';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议 scrollIntoView 场景的测试合到组件自己的测试文件中,不新建文件。(只保留父组件、子组件、工具函数单的单元测试文件)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已按建议合并。scrollIntoView 场景的用例已并入 packages/components/calendar/__test__/index.test.js,scroll-into-view.test.js 已删除(见 8a1689c)。

@lnuxe
lnuxe force-pushed the fix/calendar-current-month-none-mode branch from 30a39aa to 278043b Compare November 13, 2026 01:50
@lnuxe

lnuxe commented Nov 13, 2026

Copy link
Copy Markdown
Author

已按 review 意见调整:

  1. 移除了与本 PR 无关的改动。之前这个分支上混了 textarea 和 tree-select 的提交,现在只剩 calendar 的 calendar.ts 改动。textarea 的部分在 fix(textarea): emit the truncated value from change (issue #4705) #4717,tree-select 的部分在 fix(tree-select): rebuild columns when the root column changes (issue #4664) #4719。

  2. 测试文件合并到组件自己的测试文件。删掉了 __test__/scroll-into-view.test.js,用例移入 __test__/index.test.js 的 describe('scrollIntoView') 块。

现在改动范围:

packages/components/calendar/__test__/index.test.js  | 45 ++++++++++++++++++++++
packages/components/calendar/calendar.ts             |  6 ++-

本地验证:

npx jest --testPathPattern "calendar"
Test Suites: 2 passed, 2 total
Tests:       16 passed, 16 total
Snapshots:   9 passed, 9 total

calcMonths returned early for switchMode 'none' after setting months only, so
currentMonth stayed at its initial empty object. Opening the panel could not
locate the selected range whenever minDate/maxDate spanned a long period.

Derive currentMonth in that branch too, keeping the existing behaviour of not
touching the month switch buttons.

fix Tencent#4718
Review feedback: keep one unit test file per component instead of adding a
new file per scenario. The scrollIntoView case now lives in index.test.js
under its own describe block, and scroll-into-view.test.js is gone.

No behaviour change.
@lnuxe
lnuxe force-pushed the fix/calendar-current-month-none-mode branch from 278043b to 8a1689c Compare November 17, 2026 02:04
lnuxe added a commit to lnuxe/tdesign-miniprogram that referenced this pull request Nov 17, 2026
Keep a single unit test file per component, as requested in the review of
Tencent#4720: only parent, child and utility modules get their own files.

Refs Tencent#4732.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[TCalendar ] 小程序端无法定位到选中的日期

2 participants