Skip to content

Conversation

@IKEYCY
Copy link
Contributor

@IKEYCY IKEYCY commented Dec 4, 2025

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • New Features
    • Switch component now includes a new width prop, accepting both numeric values and string values for flexible width configuration.
    • New documentation and interactive demonstrations showcase how to configure the switch width using different value types.
    • Comprehensive end-to-end tests validate proper CSS width rendering for numeric and string-based width configurations across all scenarios.

✏️ Tip: You can customize this high-level summary in your review settings.

@github-actions github-actions bot added the enhancement New feature or request (功能增强) label Dec 4, 2025
@coderabbitai
Copy link

coderabbitai bot commented Dec 4, 2025

Walkthrough

A new width prop is added to the TinySwitch component across the renderless and Vue layers, enabling customization of switch width via number or string values. The implementation includes computed properties for styling, demo components, tests, and API documentation updates.

Changes

Cohort / File(s) Summary
Core Component Implementation
packages/vue/src/switch/src/index.ts, packages/vue/src/switch/src/pc.vue, packages/renderless/src/switch/vue.ts
Added width prop (type: [Number, String]) to switchProps; introduced switchStyle and displayOnlyStyle computed properties in renderless layer to handle width styling; applied style bindings to root and display-only elements
API & Documentation
examples/sites/demos/apis/switch.js, examples/sites/demos/pc/app/switch/webdoc/switch.js
Added width prop entry to API demonstration with type, localized descriptions, and metadata; added corresponding demo documentation entry with zh-CN/en-US localization
Demo Components
examples/sites/demos/pc/app/switch/width.vue, examples/sites/demos/pc/app/switch/width-composition-api.vue
Created new Vue components demonstrating width prop usage with both numeric and string width values
Tests
examples/sites/demos/pc/app/switch/width.spec.ts
Added Playwright E2E test validating that switches render with correct CSS width (150px) for both number and string type values

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Consistent pattern applied across files (prop definition → style computation → binding)
  • Low logic complexity; primarily configuration and styling concerns
  • Verify that switchStyle and displayOnlyStyle compute px values correctly for number types and pass-through strings as-is
  • Confirm test assertions match the demo component's expected width values

Poem

🐰 wiggles nose
A switch grows wider, oh what cheer,
Width control is finally here,
Numbers or strings, your choice to make,
Styled with ease for goodness sake,
Hopping through each test we take! 🎉

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically summarizes the main feature: adding a width property to the Switch component to enable custom width support.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
packages/renderless/src/switch/vue.ts (1)

50-67: Extract duplicate width transformation logic to reduce code duplication.

The width transformation logic (typeof props.width === 'number' ? \${props.width}px` : props.width) is duplicated in both switchStyleanddisplayOnlyStyle`. Consider extracting this to a helper computed property for better maintainability.

Apply this refactor:

    }),
+   // 计算 width 值
+   computedWidth: computed(() => {
+     if (!props.width) return null
+     return typeof props.width === 'number' ? `${props.width}px` : props.width
+   }),
    // 添加 switchStyle 计算属性
    switchStyle: computed(() => {
-     if (props.width) {
-       return {
-         width: typeof props.width === 'number' ? `${props.width}px` : props.width
-       }
-     }
-     return {}
+     return state.computedWidth ? { width: state.computedWidth } : {}
    }),
    // 添加 displayOnlyStyle 计算属性
    displayOnlyStyle: computed(() => {
-     if (props.width) {
-       return {
-         width: typeof props.width === 'number' ? `${props.width}px` : props.width,
-         display: 'inline-block'
-       }
-     }
-     return {}
+     return state.computedWidth 
+       ? { width: state.computedWidth, display: 'inline-block' } 
+       : {}
    })
examples/sites/demos/pc/app/switch/width-composition-api.vue (1)

10-10: Consider using lowercase "px" for consistency with CSS conventions.

While CSS units are case-insensitive and "150PX" will work, the standard convention is lowercase ("150px"). Consider updating for consistency with typical CSS practices.

-      <tiny-switch width="150PX"></tiny-switch>
+      <tiny-switch width="150px"></tiny-switch>
examples/sites/demos/pc/app/switch/width.spec.ts (1)

1-17: LGTM! Basic width functionality is properly tested.

The test correctly validates that both numeric and string width values render as expected. The pageerror check is a good practice.

Consider adding edge case tests for more comprehensive coverage (can be deferred to a future PR):

  • Different CSS units (em, rem, %, vw)
  • Very small/large numeric values
  • Invalid string values (to verify graceful handling)
examples/sites/demos/pc/app/switch/width.vue (1)

10-10: Consider using lowercase "px" for consistency with CSS conventions.

Similar to the composition API demo, consider using lowercase "px" instead of uppercase "PX" to align with standard CSS notation practices.

-      <tiny-switch width="150PX"></tiny-switch>
+      <tiny-switch width="150px"></tiny-switch>
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dba45e3 and 352ada7.

📒 Files selected for processing (8)
  • examples/sites/demos/apis/switch.js (1 hunks)
  • examples/sites/demos/pc/app/switch/webdoc/switch.js (1 hunks)
  • examples/sites/demos/pc/app/switch/width-composition-api.vue (1 hunks)
  • examples/sites/demos/pc/app/switch/width.spec.ts (1 hunks)
  • examples/sites/demos/pc/app/switch/width.vue (1 hunks)
  • packages/renderless/src/switch/vue.ts (1 hunks)
  • packages/vue/src/switch/src/index.ts (1 hunks)
  • packages/vue/src/switch/src/pc.vue (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: PR E2E Test (pnpm test:e2e3)
🔇 Additional comments (9)
examples/sites/demos/pc/app/switch/width-composition-api.vue (1)

1-29: LGTM! Clear demonstration of width prop usage.

The demo effectively shows both numeric and string width types. The composition API pattern is correctly implemented.

examples/sites/demos/pc/app/switch/width.vue (1)

1-35: LGTM! Options API demo is correctly implemented.

The demo clearly demonstrates width prop usage with both numeric and string types using the Options API pattern.

packages/vue/src/switch/src/pc.vue (3)

17-17: LGTM! Style binding correctly applies width customization.

The style binding to state.switchStyle properly integrates the new width prop with the existing switch styling.


40-40: LGTM! Display-only mode style binding is correctly implemented.

The style binding to state.displayOnlyStyle appropriately handles width customization for display-only mode.


66-67: LGTM! Width prop declaration follows component conventions.

The width prop is correctly added to the props array, maintaining consistency with the existing prop declaration pattern.

examples/sites/demos/pc/app/switch/webdoc/switch.js (1)

31-42: LGTM! Width demo documentation is properly structured.

The demo entry is well-documented with bilingual descriptions and follows the established pattern for demo documentation. The placement between mini-mode and loading demos is logical.

packages/vue/src/switch/src/index.ts (1)

72-73: LGTM! Width prop correctly added to public API.

The width prop is properly declared with the correct type signature [Number, String], allowing both numeric pixel values and string values with units. The syntax change to the loading prop's closing brace is correct.

examples/sites/demos/apis/switch.js (1)

114-128: Width prop API documentation is complete and well-structured.

The API documentation properly describes the width prop with correct type information (number | string), bilingual descriptions, appropriate demo references, and version metadata. However, verify that 3.28.0 is the correct version for this feature release—if this PR targets a different version, update the meta.stable value accordingly.

packages/renderless/src/switch/vue.ts (1)

59-67: Verify necessity of display: 'inline-block' for display-only mode.

The displayOnlyStyle adds display: 'inline-block' when width is specified. Please confirm this is necessary and won't conflict with existing display styles for the display-only span. If the span already has appropriate display styling, this explicit override may be unnecessary.

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

Labels

enhancement New feature or request (功能增强)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant