Skip to content

fix: ad1b77dd 将Radio改为完全受控后,原来的input[type=radio]:checked的css不再生效,造成radio点击后无法显示被点击的状态。#1

Open
Starrah wants to merge 1 commit intoMuNET-OSS:mainfrom
Starrah:fix-radio

Conversation

@Starrah
Copy link
Copy Markdown

@Starrah Starrah commented Apr 13, 2026

2026-04-13.11-42-09.mp4

此bug疑似由 ad1b77d 引入,它将Radio改为完全受控、通过Vue在DOM上设置的checked attribute进行控制,而不再使用的默认行为(将元素的.checked属性设为true)。
原来的CSS选择器input[type=radio]:checked,是一种伪类选择器,它匹配的是原生radio元素上定义的元素级checked属性。这个属性受浏览器自有逻辑控制,在用户点击等场合进行更新。然而,我们将Radio改为完全受控,并使用preventDefault()阻止了浏览器的默认行为后,checked就不会被更新了,所以:checked伪类选择器不会生效。
本PR将input[type=radio]:checked改为input[type=radio][checked],完全基于vue代码层面控制的checked属性来决定显示。

Summary by Sourcery

Bug Fixes:

  • 确保当通过 checked 属性(而非原生的 :checked 伪类)进行受控时,单选按钮能够在视觉上正确显示其选中状态。
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Ensure radio buttons visually display their selected state when controlled via the checked attribute instead of the native :checked pseudo-class.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Apr 13, 2026

审阅者指南(在小型 PR 上折叠)

审阅者指南

将单选按钮样式选择器更新为依赖由 Vue 控制的 checked 属性,而不是由浏览器管理的 :checked 伪类,从而在完全受控的单选按钮中恢复正确的视觉选中状态。

在修复 CSS 选择器后,受控单选按钮点击与样式更新的时序图

sequenceDiagram
  actor User
  participant Browser
  participant VueRadioComponent
  participant DOMInput
  participant CSSEngine

  User->>Browser: Click radio input
  Browser->>VueRadioComponent: dispatch click event
  VueRadioComponent->>Browser: event.preventDefault()
  VueRadioComponent->>VueRadioComponent: update internal checked state
  VueRadioComponent->>DOMInput: set attribute checked=true
  DOMInput->>CSSEngine: notify attribute change
  CSSEngine->>DOMInput: apply styles for input[type=radio][checked]
  DOMInput->>User: show checked visual state
Loading

文件级变更

变更 详情 文件
将单选按钮选中状态的样式从针对 :checked 伪类改为针对静态 checked 属性,以适配完全受控的 Vue 单选按钮。
  • input[type=radio] 规则集中,将 SCSS 选择器从 &:checked 更新为 &[checked],使样式基于 checked 属性的存在来生效。
  • 确保选中状态的视觉效果(背景色和边框颜色)由 Vue 设置的 DOM 属性驱动,而不是由原生 input 状态驱动。
src/themes/dynamicLight/components.module.scss

提示和命令

与 Sourcery 交互

  • 触发新的审阅: 在 pull request 中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 从审阅评论生成 GitHub issue: 通过回复某条审阅评论让 Sourcery 从该评论创建一个 issue。你也可以回复审阅评论 @sourcery-ai issue 来从该评论创建 issue。
  • 生成 pull request 标题: 在 pull request 标题中任意位置写上 @sourcery-ai,即可随时生成标题。也可以在 pull request 中评论 @sourcery-ai title 来(重新)生成标题。
  • 生成 pull request 摘要: 在 pull request 正文任意位置写上 @sourcery-ai summary,即可在你想要的位置生成 PR 摘要。你也可以在 pull request 中评论 @sourcery-ai summary 来随时(重新)生成摘要。
  • 生成审阅者指南: 在 pull request 中评论 @sourcery-ai guide,即可随时(重新)生成审阅者指南。
  • 一次性解决所有 Sourcery 评论: 在 pull request 中评论 @sourcery-ai resolve,即可将所有 Sourcery 评论标记为已解决。如果你已经处理完所有评论且不想再看到它们,这会很有用。
  • 一次性忽略所有 Sourcery 审阅: 在 pull request 中评论 @sourcery-ai dismiss,即可忽略所有现有的 Sourcery 审阅。如果你想从头开始一次新的审阅,这尤其有用——别忘了再评论 @sourcery-ai review 来触发新的审阅!

自定义你的使用体验

访问你的 控制面板 以:

  • 启用或禁用审阅功能,例如 Sourcery 自动生成的 pull request 摘要、审阅者指南等。
  • 修改审阅语言。
  • 添加、删除或编辑自定义审阅说明。
  • 调整其他审阅设置。

获取帮助

Original review guide in English
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates the radio button styling selector to rely on the Vue-controlled checked attribute instead of the browser-managed :checked pseudo-class, restoring the visual checked state for fully controlled radios.

Sequence diagram for controlled radio click and styling after CSS selector fix

sequenceDiagram
  actor User
  participant Browser
  participant VueRadioComponent
  participant DOMInput
  participant CSSEngine

  User->>Browser: Click radio input
  Browser->>VueRadioComponent: dispatch click event
  VueRadioComponent->>Browser: event.preventDefault()
  VueRadioComponent->>VueRadioComponent: update internal checked state
  VueRadioComponent->>DOMInput: set attribute checked=true
  DOMInput->>CSSEngine: notify attribute change
  CSSEngine->>DOMInput: apply styles for input[type=radio][checked]
  DOMInput->>User: show checked visual state
Loading

File-Level Changes

Change Details Files
Change radio checked-state styling to target the static checked attribute instead of the :checked pseudo-class to work with fully controlled Vue radios.
  • Update the SCSS selector from &:checked to &[checked] within the input[type=radio] ruleset so styles apply based on the presence of the checked attribute.
  • Ensure the checked visual state (background and border color) is driven by Vue’s DOM attributes rather than native input state.
src/themes/dynamicLight/components.module.scss

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@Starrah Starrah changed the title fix: ad1b77dd将Radio改为完全受控后,原来的input[type=radio]:checked的css不再生效,造成radio点击后无法显示被点击的状态。 fix: ad1b77dd 将Radio改为完全受控后,原来的input[type=radio]:checked的css不再生效,造成radio点击后无法显示被点击的状态。 Apr 13, 2026
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 1 file

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request modifies the radio input styling in the dynamicLight theme by replacing the :checked pseudo-class with the [checked] attribute selector. Feedback indicates that this change may break compatibility with native radio inputs and Vue-managed components, as the attribute selector does not track state changes that only update the DOM property. It is recommended to use both selectors to ensure consistent behavior across different environments.

border-color: oklch(0.68 0.17 var(--hue));

&:checked {
&[checked] {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

:checked 替换为 [checked] 可能会引入以下问题:

  1. 破坏原生 radio 兼容性:对于非受控的原生 <input type="radio">,浏览器在用户交互时仅更新其 checked 属性(property),而不会同步更新 HTML 属性(attribute)。这会导致 [checked] 选择器无法匹配原生组件的选中状态。
  2. Vue 动态绑定机制:在 Vue 3 中,checked 绑定通常作为 property 处理。如果 Vue 没有将更改同步到 DOM attribute 上(通常不会同步),[checked] 选择器在动态切换时将失效。建议检查为什么 :checked 在受控模式下失效,通常只要 el.checked 属性被正确更新,该伪类就应当生效。

建议保留 :checked 并将 [checked] 作为补充以确保兼容性。此外,请检查项目中其他主题文件(如 src/themes/aquadx/components.module.scss)是否也存在类似的选择器需要同步修复。

    &:checked,
    &[checked] {

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.

确实漏了aquadx主题,force-push修了

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - 我在这里提供了一些整体性的反馈:

  • 如果应用中仍然存在任何未受控或原生的单选框(radio),请考虑在选择器中组合使用 &:checked&[checked],以避免回归问题(例如 input[type=radio]:checked, input[type=radio][checked]),而不是完全用属性选择器替换伪类。
  • 请确认在所有交互路径中(点击、键盘、以及通过代码进行的变更),Vue 都能一致地将响应式的 checked 状态同步到 DOM 属性上,因为现在这段 CSS 依赖的是属性而不是伪类。
给 AI Agent 的提示词
Please address the comments from this code review:

## Overall Comments
- If there are still any uncontrolled or native radios in the app, consider combining `&:checked` and `&[checked]` in the selector to avoid regressions (e.g., `input[type=radio]:checked, input[type=radio][checked]`) instead of fully replacing the pseudo-class.
- Confirm that Vue consistently reflects the reactive `checked` state onto the DOM attribute in all interaction paths (click, keyboard, programmatic changes), since this CSS now depends on the attribute rather than the pseudo-class.

Sourcery 对开源项目是免费的——如果你觉得我们的 Review 有帮助,欢迎帮忙分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈来改进之后的 Review。
Original comment in English

Hey - I've left some high level feedback:

  • If there are still any uncontrolled or native radios in the app, consider combining &:checked and &[checked] in the selector to avoid regressions (e.g., input[type=radio]:checked, input[type=radio][checked]) instead of fully replacing the pseudo-class.
  • Confirm that Vue consistently reflects the reactive checked state onto the DOM attribute in all interaction paths (click, keyboard, programmatic changes), since this CSS now depends on the attribute rather than the pseudo-class.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- If there are still any uncontrolled or native radios in the app, consider combining `&:checked` and `&[checked]` in the selector to avoid regressions (e.g., `input[type=radio]:checked, input[type=radio][checked]`) instead of fully replacing the pseudo-class.
- Confirm that Vue consistently reflects the reactive `checked` state onto the DOM attribute in all interaction paths (click, keyboard, programmatic changes), since this CSS now depends on the attribute rather than the pseudo-class.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@Starrah
Copy link
Copy Markdown
Author

Starrah commented Apr 13, 2026

以及另一种改法是

diff --git a/src/components/Radio/index.tsx b/src/components/Radio/index.tsx
--- a/src/components/Radio/index.tsx	(revision b4675c5786c14fc2f67f02a00bd51e558b2a8003)
+++ b/src/components/Radio/index.tsx	(date 1776053180733)
@@ -18,8 +18,10 @@
         checked={props.value === props.k}
         disabled={disabled.value}
         onClick={(e: Event) => {
-          e.preventDefault();
-          if (disabled.value) return;
+          if (disabled.value) {
+            e.preventDefault();
+            return;
+          }
           emit('update:value', props.k);
           props.onClick?.();
         }}

只在disabled的时候才preventDefault(),正常流不要prevent,这样点击之后可以确保checked属性也同步过去。
哪种改法好看您判断了

@Starrah
Copy link
Copy Markdown
Author

Starrah commented Apr 13, 2026

至于两个AI建议的 &:checked, &[checked]并列都写上,我个人认为没必要,因为这里的CSS控制的是我们自己定义的Radio对象,被选中时显示的主题色实心圆圈,如果不是我们自己定义的Radio(虽然项目里根本没有)压根也不应该走这段逻辑?
然后只要用的是我们自己的,就一定会落在受控的[checked]属性之内?

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.

1 participant