Skip to content

refactor(color-picker): ensure mode switches based on color value #3503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Apr 28, 2025

Conversation

RylanBot
Copy link
Collaborator

@RylanBot RylanBot commented Apr 12, 2025

🤔 这个 PR 的性质是?

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

🔗 相关 Issue

💡 需求背景和解决方案

Tip

以下出现的双栏截图和视频,左边均是有问题的版本,右边是预期合理的版本

1. PC 端统一修复➕重构

序号 描述 类型
1.1 「渐变」切回「单色」模式 ① CSS 中的色值仍保留渐变时的格式,未按预期转换为 rgba image
② 新增最近使用颜色仍为渐变色,未更新为单色
Tencent/tdesign-vue#3566
1.2 自动根据当前数值切换模式 ① 🐛 Trigger Input 【视频 1-2-1】 / Tencent/tdesign-vue-next#3486 / Tencent/tdesign-vue-next#2821
② ♻️ 最近使用颜色、预设颜色 【视频 1-2-2】
1.3 透明通道 ① 🐛 Trigger Input 无法同步透明度值 【视频 1-3-1】
② ♻️ 优化格式化返回值,例如 RGB 转为 RGBA,HSL 转为 HSLA 等
1.4 colorModes = {['linear-gradient']} 当用户仅使用渐变模式时,预设颜色中过滤非渐变的色值
1.5 Trigger Input ① 由于之前的逻辑是先检查用户输入色值是否有效,再决定是否更新 → 导致无法直接删除某个字母,只能整体粘贴替换,现在移除验证
② 移除 onBlur 事件,避免与 onChange 重复触发
③ onChange 回调传入 context: { color: ColorObject; trigger: 'input' },符合文档标注的 TS 类型,避免返回给用户一个 undefined 或合成事件

2. React 特有 Bug 修复

序号 类型 描述
2.1 渐变模式 除第一个点外,其他渐变点的位置和颜色无法正常更新 https://github.com/user-attachments/assets/015e8580-a706-4afe-979e-d16232a9f464
2.2 Panel 移除未使用的 Props,调整函数名称与其他 PC 端组件库同步
2.3 RadioGroup PR #3495 的遗留问题
- 在实现 1.2 的功能时, 发现 Radio 经常无法触发高亮
- common 的 obeserve() 内部已经主动 unobserve 了,所以我觉得暂时不需要外部再进行 disconnect,性能目前看来影响不大,主动销毁反而造成一些极端情况下的计算失败

5. 解决方案总结

细节比较多,不方便全部一一说明。部分问题均是 Color 实例内部的 isGradiant 没有同步导致的,可以参考这里的注释 Tencent/tdesign-common@1124592

📝 更新日志

  • refactor(ColorPicker): 自动根据「触发器 / 最近颜色 / 预设颜色」的色值进行切换单色和渐变模式;只开启渐变模式时,过滤「预设颜色 / 当前颜色」中的非渐变色值;新增 format HEX8,移除 HSB;使用渐变模式的业务请注意此变更 ⚠️

  • feat(ColorPicker): 新增 enableMultipleGradient API,默认开启

  • fix(ColorPicker): 修复渐变点无法正常更新颜色和位置;开启透明通道时的返回值格式化异常

  • 本条 PR 不需要纳入 Changelog

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

⚠️ 请自检并全部勾选全部选项⚠️

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

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

Copy link

pkg-pr-new bot commented Apr 12, 2025

Open in StackBlitz

npm i https://pkg.pr.new/tdesign-react@3503

commit: 76f3cda

Copy link
Contributor

github-actions bot commented Apr 12, 2025

完成

@RylanBot RylanBot changed the title fix(color-picker): ensure mode switches based on the input color value refactor(color-picker): ensure mode switches based on color value Apr 16, 2025
@RylanBot RylanBot requested a review from Copilot April 20, 2025 15:23
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors several components of the ColorPicker and related components to ensure the color mode switches correctly based on the color value while cleaning up unused props and improving event handling. Key changes include:

  • Removal of unused props and cleanup functions in the RadioGroup and ColorPicker interfaces.
  • Refactoring of change handling in the trigger component to use direct prop values and improved callback parameters.
  • Updates in the panel component to auto-detect color modes based on the input value, adjust format conversion for alpha channels, and streamline UI updates.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/components/radio/RadioGroup.tsx Removed redundant observer cleanup logic; ensure proper observer disconnection.
packages/components/color-picker/interface.ts Removed unused props for cleaner interface.
packages/components/color-picker/components/trigger.tsx Refactored controlled input handling and event callbacks.
packages/components/color-picker/components/panel/index.tsx Revised mode detection, format conversion logic, and event handlers.
packages/components/color-picker/components/panel/header.tsx Removed unused togglePopup prop from header.
packages/components/color-picker/tests/color-picker.test.tsx Updated tests to reflect changes in gradient and pure-color mode handling.
Comments suppressed due to low confidence (1)

packages/components/radio/RadioGroup.tsx:87

  • The removal of the clearObserver logic in the conditional branch may leave stale observers attached when barStyle.width is non-zero. Consider verifying that observer cleanup is handled appropriately in all cases.
if (barStyle && barStyle.width !== '0px') {

@RylanBot RylanBot removed the WIP work in porgess label Apr 20, 2025
@RylanBot RylanBot requested a review from Copilot April 20, 2025 16:25
@RylanBot RylanBot force-pushed the fix/color-picker branch 2 times, most recently from 9c940f6 to d5301ad Compare April 23, 2025 10:41
@RylanBot
Copy link
Collaborator Author

RylanBot commented Apr 23, 2025

Note

需要等 common 仓库 PR Tencent/tdesign-common#2110 合并,针对 commit 1ddc014 变动的测试才能通过,且还需要同步对应的公共方法

@liweijie0812

This comment was marked as outdated.

1 similar comment
@liweijie0812
Copy link
Collaborator

/update-common

@uyarn uyarn merged commit e7a8b48 into develop Apr 28, 2025
9 checks passed
@uyarn uyarn deleted the fix/color-picker branch April 28, 2025 12:27
@github-actions github-actions bot mentioned this pull request Apr 28, 2025
16 tasks
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.

4 participants