Skip to content

fix(Dialog): treat an empty cancelBtn as no button - #4739

Open
lnuxe wants to merge 1 commit into
Tencent:developfrom
lnuxe:fix/dialog-alert-cancel-button
Open

lnuxe wants to merge 1 commit into
Tencent:developfrom
lnuxe:fix/dialog-alert-cancel-button

Conversation

@lnuxe

@lnuxe lnuxe commented Nov 23, 2026

Copy link
Copy Markdown

🤔 这个 PR 的性质是?

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

🔗 相关 Issue

Fixes #4738

💡 需求背景和解决方案

问题:cancelBtn: '' 想表达「不显示取消按钮」,实际会渲染出一个空的、可点击的按钮。

dialog.ts 的 observer 用 typeof btn === 'string' 判断「有文案」,而 typeof '' === 'string' 同样为真,于是空字符串走进了渲染分支:

if (typeof btn === 'string') {
  rect[`_${key}`] = { ...base, content: btn }; // content: '',照样渲染
}

wx:if="{{_cancel}}" 拿到对象,恒为真。

解决方案:把「为空」的判断提到最前面,空字符串 / null / undefined 一律不渲染。

// 空字符串表示「不渲染这个按钮」,不能当成空文案去渲染一个空按钮:
// `cancelBtn: ''` 是 alert 以及用户隐藏取消按钮的写法。
if (!btn) {
  rect[`_${key}`] = null;
} else if (typeof btn === 'string') {
  rect[`_${key}`] = { ...base, content: btn };
} else {
  rect[`_${key}`] = { ...base, ...btn };
}

顺带修掉 Dialog.alert() 里 cancelBtn: '' 的展开顺序:它写在 ...mergedOptions 之前,会被实例上的 cancelBtn 覆盖,导致复用一个写过 cancel-btn 的实例时,alert 会带出取消按钮。

instance.setData({
  ...mergedOptions,
  // alert 只保留确定按钮。这里必须放在展开之后:mergedOptions 里带着实例上的
  // cancelBtn(页面写了 `cancel-btn` 就会有值),放在前面会被它覆盖回去。
  cancelBtn: '',
  visible: true,
});

回归测试(packages/components/dialog/__test__/alert-plugin.test.js)

用例 修复前 修复后
Dialog.alert() + 实例上有 cancelBtn="cancel" → 取消按钮数量 1 ✗ 0
<t-dialog cancelBtn="" /> → 取消按钮数量 1 ✗ 0
<t-dialog cancelBtn="cancel" /> → 取消按钮数量 / 文案(守卫) 1 / cancel 1 / cancel
# 未修复
Expected: 0
Received: 1
Expected: 0
Received: 1

# 修复后
✓ :hides the cancel button even when the instance declares one
✓ :renders no cancel button for an empty cancelBtn
✓ :renders the cancel button for a labelled cancelBtn

dialog 全量 3 个测试套件通过,无快照变更;eslint 干净。

📝 更新日志

  • fix(Dialog): cancelBtn 为空字符串时不再渲染空按钮,Dialog.alert() 不再带出取消按钮

  • 本条 PR 不需要纳入 Changelog

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

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

`typeof '' === 'string'` is true, so an empty string fell into the branch that
renders a button and produced an empty, still clickable one. `wx:if={{_cancel}}`
received an object and stayed true.

Dialog.alert() relies on that empty string to drop the cancel button, so it also
leaked whatever cancelBtn the reused instance declared. Move cancelBtn after the
spread so the intent survives.

Fixes Tencent#4738
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.

[Dialog] cancelBtn 传空字符串会渲染空按钮,Dialog.alert() 会带出取消按钮

1 participant