Skip to content
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

refactor: ♻️ 优化 wd-sort-button 组件排序图标 #652

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

LengGeng
Copy link
Contributor

@LengGeng LengGeng commented Oct 11, 2024

优化排序图标显示,更符合直觉
添加排序图标的插槽(sort-icon),方便用户扩展

🤔 这个 PR 的性质是?(至少选择一个)

  • 日常 bug 修复
  • 新特性提交
  • 站点、文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • TypeScript 定义更新
  • CI/CD 改进
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 代码重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

🔗 相关 Issue

💡 需求背景和解决方案

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

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

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

Summary by CodeRabbit

  • 新功能
    • 引入了新的 CSS 变量以增强排序按钮的样式定制选项。
    • 允许用户自定义排序图标,提供默认图标作为备选。
  • 样式
    • 更新了排序按钮的样式规则,增加了活动状态的样式。
    • 确保排序图标在活动状态下的一致外观。

优化排序图标显示,更符合直觉
添加排序图标的插槽(sort-icon),方便用户扩展
Copy link

vercel bot commented Oct 11, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
wot-design-uni ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 14, 2024 9:43am

Copy link

coderabbitai bot commented Oct 11, 2024

Walkthrough

此次更改涉及三个文件,主要更新了与排序按钮组件相关的样式和逻辑。在 variable.scss 文件中,新增了两个 CSS 变量 $-sort-button-icon-color$-sort-button-icon-active-color,用于定义排序按钮图标的颜色。在 index.scss 文件中,调整了排序按钮的样式规则,引入了新的状态选择器,并根据活动状态更新了图标颜色。最后,在 wd-sort-button.vue 组件中,重构了模板结构,采用动态类绑定来渲染排序图标,并引入了插槽以支持自定义图标。

Changes

文件路径 更改摘要
src/uni_modules/wot-design-uni/components/common/abstracts/variable.scss 新增变量 $-sort-button-icon-color$-sort-button-icon-active-color,用于定义排序按钮图标的颜色。
src/uni_modules/wot-design-uni/components/wd-sort-button/index.scss 修改了样式规则,新增活动状态选择器,并更新图标颜色为新变量 $-sort-button-icon-active-color
src/uni_modules/wot-design-uni/components/wd-sort-button/wd-sort-button.vue 重构模板结构,使用动态类绑定替代条件渲染,新增插槽以支持自定义排序图标。

Possibly related PRs

Suggested reviewers

  • Moonofweisheng

Poem

在排序按钮上跳跃,
新颜色闪闪发光,
动态绑定乐无穷,
自定义图标随心放。
小兔子欢快歌唱,
变化带来新希望! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Oct 11, 2024

Deploy Preview for wot-design-uni ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 0327f16
🔍 Latest deploy log https://app.netlify.com/sites/wot-design-uni/deploys/6735c5e4c68248000888ccc3
😎 Deploy Preview https://deploy-preview-652--wot-design-uni.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

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

🧹 Outside diff range and nitpick comments (5)
src/uni_modules/wot-design-uni/components/wd-sort-button/wd-sort-button.vue (2)

7-18: 优化了排序按钮的实现,增加了自定义图标的灵活性。

这些更改显著提高了组件的可定制性和可维护性:

  1. 使用动态类绑定简化了不同排序状态的显示逻辑。
  2. 新增的插槽允许轻松自定义排序图标。
  3. 如果没有使用自定义图标,仍然提供了默认图标。

这是一个很好的改进。

建议考虑使用更具描述性的图标名称。例如,可以将 "arrow-up" 和 "arrow-down" 改为 "sort-ascending" 和 "sort-descending",以更好地表达它们的用途:

-          <wd-icon name="arrow-up" custom-class="wd-sort-button__icon ascending wd-sort-button__icon-up" />
-          <wd-icon name="arrow-down" custom-class="wd-sort-button__icon descending wd-sort-button__icon-down" />
+          <wd-icon name="sort-ascending" custom-class="wd-sort-button__icon ascending wd-sort-button__icon-up" />
+          <wd-icon name="sort-descending" custom-class="wd-sort-button__icon descending wd-sort-button__icon-down" />

这样可以使代码更加自解释,提高可读性。


Line range hint 22-78: 脚本部分可以进一步优化

虽然模板部分的更改很好,但脚本部分还有改进的空间:

  1. handleClick 函数较长,可以考虑重构以提高可读性:
function getNextSortValue(currentValue: number, allowReset: boolean, descFirst: boolean): number {
  if (descFirst) {
    return currentValue === 0 ? -1 : currentValue === -1 ? 1 : allowReset ? 0 : -1;
  } else {
    return currentValue === 0 ? 1 : currentValue === 1 ? -1 : allowReset ? 0 : 1;
  }
}

function handleClick() {
  const nextValue = getNextSortValue(props.modelValue, props.allowReset, props.descFirst);
  emit('update:modelValue', nextValue);
  emit('change', { value: nextValue });
}
  1. 考虑使用更具描述性的名称替代 modelValue,例如 sortOrder
- const props = defineProps(sortButtonProps)
+ const props = defineProps({
+   ...sortButtonProps,
+   sortOrder: {
+     type: Number,
+     default: 0
+   }
+ })

- emit('update:modelValue', nextValue)
+ emit('update:sortOrder', nextValue)

这些更改可以提高代码的可读性和可维护性。

您觉得这些建议如何?是否需要我为这些更改创建一个新的 GitHub issue?

src/uni_modules/wot-design-uni/components/wd-sort-button/index.scss (2)

71-73: 优化了排序按钮的活动状态样式

这些更改很好地提高了选择器的特异性,并使用变量来设置活动图标的颜色。这提高了代码的可维护性和灵活性。

建议:考虑为上升和下降状态使用单独的变量,以便将来可能需要不同的颜色。

&.ascending :deep(.wd-sort-button__icon.ascending) {
  color: $-sort-button-icon-active-ascending-color;
}
&.descending :deep(.wd-sort-button__icon.descending) {
  color: $-sort-button-icon-active-descending-color;
}

89-89: 下降图标颜色与上升图标保持一致

使用相同的 $-sort-button-icon-color 变量来设置下降图标的颜色是正确的,这确保了上升和下降图标的颜色一致性。

建议:如果将来需要区分上升和下降图标的颜色,可以考虑使用两个不同的变量:

@include edeep(icon-up) {
  color: $-sort-button-icon-up-color;
}

@include edeep(icon-down) {
  color: $-sort-button-icon-down-color;
}

这样可以在保持当前一致性的同时,为未来的定制提供更大的灵活性。

src/uni_modules/wot-design-uni/components/common/abstracts/variable.scss (1)

566-567: 新增的排序按钮图标颜色变量看起来不错!

这些新增的变量很好地符合了文件中的命名约定,并且使用了CSS自定义属性语法,这有利于主题定制。默认值的选择也很合理。

为了保持一致性,建议考虑添加一个注释来解释这些变量的用途,类似于文件中其他变量的做法。例如:

// 排序按钮图标颜色
$-sort-button-icon-color: var(--wot-sort-button-icon-color, rgba(0, 0, 0, 0.45)) !default;
$-sort-button-icon-active-color: var(--wot-sort-button-icon-active-color, $-color-theme) !default;

这样可以提高代码的可读性和可维护性。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 20148a7 and 0ae6e51.

📒 Files selected for processing (3)
  • src/uni_modules/wot-design-uni/components/common/abstracts/variable.scss (1 hunks)
  • src/uni_modules/wot-design-uni/components/wd-sort-button/index.scss (2 hunks)
  • src/uni_modules/wot-design-uni/components/wd-sort-button/wd-sort-button.vue (1 hunks)
🧰 Additional context used
🔇 Additional comments (2)
src/uni_modules/wot-design-uni/components/wd-sort-button/wd-sort-button.vue (1)

Line range hint 1-82: 总体评估:改进了组件的灵活性和可用性

这次更改主要集中在模板部分,通过引入动态类绑定和自定义图标插槽,显著提高了组件的灵活性和可定制性。这些更改是积极的,使得组件更易于使用和扩展。

虽然脚本部分没有直接更改,但我们提出了一些建议来进一步优化代码结构和可读性。这些建议包括重构 handleClick 函数和使用更具描述性的prop名称。

样式部分没有变化,这表明现有的样式仍然适用于更新后的组件结构。

总的来说,这些更改改进了 wd-sort-button 组件,使其更加灵活和易于使用。建议的进一步优化可以在未来的迭代中考虑实施。

src/uni_modules/wot-design-uni/components/wd-sort-button/index.scss (1)

82-82: 图标颜色使用变量

使用 $-sort-button-icon-color 变量来设置上升图标的颜色是一个很好的做法。这提高了样式的一致性和可维护性。

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

🧹 Outside diff range and nitpick comments (2)
src/uni_modules/wot-design-uni/components/wd-sort-button/wd-sort-button.vue (2)

7-14: 优化:使用动态类绑定提高了代码可维护性

通过使用动态类绑定来控制排序按钮的状态,代码变得更加清晰和易于维护。:class 对象语法的使用使得状态与样式的对应关系一目了然。

建议考虑将类名常量提取到组件的常量配置中,以便于后续维护:

+const STATE_CLASSES = {
+  'is-active': 'modelValue !== 0',
+  ascending: 'modelValue === 1',
+  descending: 'modelValue === -1'
+}

15-18: 新功能:新增排序图标插槽,提升组件灵活性

通过添加 sort-icon 插槽并传入当前排序状态,使得组件更具扩展性。默认图标的实现也符合用户体验最佳实践。

建议在组件文档中添加以下使用示例:

<!-- 自定义排序图标 -->
<wd-sort-button v-model="value">
  <template #sort-icon="{ sort }">
    <view class="custom-icon">{{ sort === 1 ? '' : '' }}</view>
  </template>
</wd-sort-button>
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 0ae6e51 and 0327f16.

📒 Files selected for processing (2)
  • src/uni_modules/wot-design-uni/components/common/abstracts/variable.scss (1 hunks)
  • src/uni_modules/wot-design-uni/components/wd-sort-button/wd-sort-button.vue (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/uni_modules/wot-design-uni/components/common/abstracts/variable.scss

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