Skip to content

feat: ✨ tabs支持左对齐 #718

Merged
Moonofweisheng merged 1 commit intomasterfrom
feature/tabs-slidable
Nov 18, 2024
Merged

feat: ✨ tabs支持左对齐 #718
Moonofweisheng merged 1 commit intomasterfrom
feature/tabs-slidable

Conversation

@Moonofweisheng
Copy link
Owner

@Moonofweisheng Moonofweisheng commented Nov 18, 2024

Closes: #380

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

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

🔗 相关 Issue

#380

💡 需求背景和解决方案

支持tabs选项左对齐,同时在开启动画时默认不对隐藏tabs项添加display:none

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

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

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

Summary by CodeRabbit

  • 新功能

    • 更新了 wd-tabs 组件文档,新增左对齐滚动标签功能。
    • 新增 slidable 属性,支持标签在超出可用空间时滚动。
    • 新增示例代码,展示如何使用左对齐滚动标签。
  • 文档

    • 修改了 slidable-num 属性的描述,以明确其在 slidable 设置为 auto 时的有效性。
    • 文档结构保持一致,涵盖了 wd-tabs 组件的各种功能和用法示例。
  • 样式

    • 更新了 wd-tabs 组件的样式,以增强暗黑主题和默认主题的视觉一致性。

@vercel
Copy link

vercel bot commented Nov 18, 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 18, 2024 5:04am

@coderabbitai
Copy link

coderabbitai bot commented Nov 18, 2024

Walkthrough

此次变更涉及对wd-tabs组件的文档和实现的多项更新。文档中新增了关于左对齐滚动标签的功能说明,并对相关属性进行了更新。组件实现方面,移除了lazy-render属性,新增了一个示例展示左对齐滚动标签的功能。此外,多个文件中引入了新的计算属性和状态管理,增强了组件的可维护性和功能性。

Changes

文件路径 变更摘要
docs/component/tabs.md 更新文档,新增左对齐滚动标签功能说明,修改slidable-num属性描述。
src/pages/tabs/Index.vue 移除lazy-render属性,新增示例块以展示slidable属性设置为"always"的滚动标签功能,新增tab9变量。
src/uni_modules/wot-design-uni/components/wd-tab/wd-tab.vue 引入计算属性tabBodyStyle以管理标签主体样式,更新导入语句以包含新工具函数。
src/uni_modules/wot-design-uni/components/wd-tabs/index.scss 添加和修改样式以支持深色主题和默认主题,增强视觉一致性和响应性。
src/uni_modules/wot-design-uni/components/wd-tabs/types.ts 更新TabsProvideTabsExpose类型,新增slidable属性及相关类型定义,增强状态管理能力。
src/uni_modules/wot-design-uni/components/wd-tabs/wd-tabs.vue 修改类绑定,引入计算属性innerSlidable,新增状态属性useInnerLine,优化setActive函数逻辑。

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.

@netlify
Copy link

netlify bot commented Nov 18, 2024

Deploy Preview for wot-design-uni ready!

Name Link
🔨 Latest commit 0d2d220
🔍 Latest deploy log https://app.netlify.com/sites/wot-design-uni/deploys/673acaed1d7a720008550b50
😎 Deploy Preview https://deploy-preview-718--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: 1

🧹 Outside diff range and nitpick comments (13)
src/uni_modules/wot-design-uni/components/wd-tabs/types.ts (3)

16-16: 建议添加类型说明文档

为了提高代码可读性和可维护性,建议为 TabsSlidable 类型添加 JSDoc 注释,说明每个可选值的用途。

建议添加如下注释:

+/**
+ * 滚动导航类型
+ * @property 'auto' - 超过阈值时自动开启滚动
+ * @property 'always' - 始终开启滚动
+ */
export type TabsSlidable = 'auto' | 'always'

70-75: 建议完善属性文档

属性文档已经包含了基本说明,但建议添加使用示例,以便开发者更好地理解如何使用该属性。

建议扩展注释如下:

  /**
   * 是否开启滚动导航
   * 可选值:'auto' | 'always'
   * @default auto
+  * @example
+  * // 始终开启滚动导航
+  * <wd-tabs slidable="always">
+  * // 自动判断是否开启滚动导航
+  * <wd-tabs slidable="auto">
   */

78-84: 建议增强方法参数说明

虽然方法注释清晰,但建议为参数添加更详细的类型说明和用途说明。

建议修改如下:

 export type TabsExpose = {
-  // 修改选中的tab Index
+  /**
+   * 修改选中的tab Index
+   * @param value - 目标tab的索引或值
+   * @param init - 是否为初始化调用
+   * @param setScroll - 是否需要滚动到选中位置
+   */
   setActive: (value: number | string, init: boolean, setScroll: boolean) => void
-  // scroll-view滑动到active的tab_nav
+  /**
+   * scroll-view滑动到active的tab_nav
+   */
   scrollIntoView: () => void
-  // 更新底部条样式
+  /**
+   * 更新底部条样式
+   * @param animation - 是否启用动画效果
+   */
   updateLineStyle: (animation: boolean) => void
 }
src/uni_modules/wot-design-uni/components/wd-tab/wd-tab.vue (2)

19-20: 建议:优化导入语句的组织

建议将导入语句按照以下顺序组织:

  1. Vue 核心模块
  2. 工具函数
  3. 组件相关的类型和常量

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

-import { getCurrentInstance, ref, watch, type CSSProperties } from 'vue'
-import { isDef, isNumber, isString, objToStyle } from '../common/util'
+// Vue 核心模块
+import { getCurrentInstance, ref, watch, computed, type CSSProperties } from 'vue'
+
+// 工具函数
+import { isDef, isNumber, isString, objToStyle } from '../common/util'

38-44: 建议:增强计算属性的类型安全性和可读性

当前实现可以通过以下几点来改进:

  1. 添加注释说明计算属性的用途
  2. 增强对 tabs 对象的空值检查
  3. 明确返回类型定义
+/**
+ * 计算标签页主体的样式
+ * @returns {CSSProperties} 样式对象
+ */
-const tabBodyStyle = computed(() => {
+const tabBodyStyle = computed((): CSSProperties => {
   const style: CSSProperties = {}
-  if (!isShow.value && (!isDef(tabs) || !tabs.props.animated)) {
+  if (!isShow.value && (!isDef(tabs) || !tabs?.props?.animated)) {
     style.display = 'none'
   }
   return objToStyle(style)
 })
src/pages/tabs/Index.vue (1)

86-94: 新增左对齐滚动功能实现正确

新增的左对齐滚动演示区块实现合理,但建议:

  1. 考虑添加更多的标签来更好地展示滚动效果
  2. 为了更好的用户体验,可以考虑添加滚动指示器

建议调整示例代码如下:

-        <block v-for="item in 5" :key="item">
+        <block v-for="item in 8" :key="item">
           <wd-tab :title="`超大标签${item}`">
             <view class="content">内容{{ tab9 + 1 }}</view>
           </wd-tab>
         </block>
docs/component/tabs.md (3)

130-143: 建议补充更多使用场景示例

建议在示例中展示更多实际应用场景,比如:

  • 标签数量较多时的滚动效果
  • 不同标签宽度混合时的对齐效果
  • 与其他属性(如 sticky)组合使用的效果

这将帮助用户更好地理解和使用该功能。


147-147: 建议优化文档结构,提高可读性

当前描述段落包含了多个重要概念(滑动阈值、导航地图、左对齐特性),建议:

  1. 将内容分成多个小段落
  2. 使用项目符号列表来分别说明不同的功能特性
  3. 为每个特性添加对应的示例链接

这样的结构将使文档更容易阅读和理解。


Line range hint 154-165: 建议完善属性文档说明

关于 slidableslidable-num 属性的说明建议补充:

  1. 两个属性之间的关系和使用场景
  2. alwaysauto 模式的具体行为差异
  3. 添加示例代码链接,方便用户快速查看使用方法

这些补充信息将帮助用户更好地理解和正确使用这些属性。

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

124-127: 指示器居中定位实现合理

使用transform进行居中定位是一个很好的实现方式:

  • 性能更好(触发合成而不是重排)
  • 确保指示器在任何宽度的标签下都能正确居中

建议添加一个注释说明这个修饰符的用途。

 @include m(inner){
+      // 确保指示器在标签内居中显示
       left: 50%; 
       transform: translateX(-50%)
     }
src/uni_modules/wot-design-uni/components/wd-tabs/wd-tabs.vue (3)

5-5: 模板绑定逻辑优化

通过引入 innerSlidableuseInnerLine 改进了滑动行为和激活标识的显示逻辑,增强了组件的可用性。建议考虑以下优化:

  1. 为内部激活线添加过渡动画,提升用户体验
  2. 考虑在 useInnerLine 为 true 时隐藏外部下划线,避免视觉干扰
  <view class="wd-tabs__line wd-tabs__line--inner" 
-   v-if="state.activeIndex === index && state.useInnerLine">
+   v-if="state.activeIndex === index && state.useInnerLine"
+   :style="{ transition: 'opacity 0.3s' }">
  </view>

Also applies to: 12-12, 23-23, 80-80, 84-84, 95-95


205-224: 更新激活项方法实现完善,建议小幅优化

updateActive 方法实现了完整的激活项更新逻辑,包括初始化、滚动行为和状态更新。建议添加以下优化:

  1. 添加参数类型验证
  2. 考虑添加错误处理机制
 const updateActive = (value: number | string = 0, init: boolean = false, setScroll: boolean = true) => {
+  // 验证参数类型
+  if (!isNumber(value) && !isString(value)) {
+    console.warn('[wot-design] warning: value should be number or string')
+    return
+  }
   
   if (items.value.length === 0) return
   
   value = getActiveIndex(value)
   if (items.value[value].disabled) return
   
+  try {
     state.activeIndex = value
     if (setScroll) {
       updateLineStyle(init === false)
       scrollIntoView()
     }
     setActiveTab()
+  } catch (error) {
+    console.error('[wot-design] error: failed to update active tab', error)
+  }
 }

Line range hint 319-344: 建议优化下划线样式更新的性能

updateLineStyle 方法的实现逻辑正确,但在频繁切换标签时可能会导致性能问题。建议考虑以下优化:

  1. 使用 requestAnimationFrame 优化动画性能
  2. 添加防抖处理避免频繁计算
-function updateLineStyle(animation: boolean = true) {
+const updateLineStyle = debounce((animation: boolean = true) => {
   if (!state.inited) return
   
   const { lineWidth, lineHeight } = props
   
   getRect($item, true, proxy).then((rects) => {
     // ... existing code ...
     if (left) {
-      lineStyle.transform = `translateX(${left}px) translateX(-50%)`
+      requestAnimationFrame(() => {
+        lineStyle.transform = `translateX(${left}px) translateX(-50%)`
+        if (animation) {
+          lineStyle.transition = 'width 300ms ease, transform 300ms ease'
+        }
+        state.useInnerLine = false
+        state.lineStyle = objToStyle(lineStyle)
+      })
     }
   })
-}
+}, 16)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8419564 and 0d2d220.

📒 Files selected for processing (6)
  • docs/component/tabs.md (2 hunks)
  • src/pages/tabs/Index.vue (3 hunks)
  • src/uni_modules/wot-design-uni/components/wd-tab/wd-tab.vue (3 hunks)
  • src/uni_modules/wot-design-uni/components/wd-tabs/index.scss (2 hunks)
  • src/uni_modules/wot-design-uni/components/wd-tabs/types.ts (2 hunks)
  • src/uni_modules/wot-design-uni/components/wd-tabs/wd-tabs.vue (10 hunks)
🔇 Additional comments (7)
src/uni_modules/wot-design-uni/components/wd-tabs/types.ts (1)

7-11: 状态属性定义清晰完整!

新增的状态属性定义合理,每个属性都有清晰的类型声明和注释说明,有助于代码的可维护性。

src/uni_modules/wot-design-uni/components/wd-tab/wd-tab.vue (1)

3-3: 代码优化:将内联样式迁移到计算属性中

将样式逻辑从模板移到计算属性中是一个很好的改进,这样可以提高代码的可维护性和可读性。

src/pages/tabs/Index.vue (2)

121-122: 新增状态变量定义合理

tab9 的定义遵循了组件中其他 tab 变量的命名和类型规范,实现正确。


Line range hint 77-85: 移除了 lazy-render 属性的影响

移除 lazy-render 属性可能会影响标签页的性能表现,特别是在标签数量较多的情况下。建议确认:

  1. 是否是有意识地移除该属性
  2. 对性能的潜在影响是否可接受

运行以下脚本检查其他文件中是否还在使用 lazy-render 属性:

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

94-94: 添加相对定位属性以支持标签对齐功能

为nav-item添加相对定位是合理的,这样可以确保子元素(如指示器)能够正确定位。

src/uni_modules/wot-design-uni/components/wd-tabs/wd-tabs.vue (2)

182-184: 计算属性实现正确

innerSlidable 计算属性的实现逻辑清晰,正确处理了 slidable 属性的 'always' 模式和基于 slidableNum 的条件判断。


295-296: 生命周期钩子实现正确

onMounted 钩子中正确初始化了 updateActiveuseInnerLine 状态,实现逻辑合理。

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.

[新功能需求] tab固定宽度在tabs中左对齐

1 participant