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

feat(tabbar): add prop before-switch #3060

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

Conversation

eiinu
Copy link
Member

@eiinu eiinu commented May 7, 2024

这个 PR 做了什么? (简要描述所做更改)

这个 PR 是什么类型? (至少选择一个)

  • feat: 新特性提交
  • fix: bug 修复
  • docs: 文档改进
  • style: 组件样式/交互改进
  • type: 类型定义更新
  • perf: 性能、包体积优化
  • refactor: 代码重构、代码风格优化
  • test: 测试用例
  • chore(deps): 依赖升级
  • chore(demo): 演示代码改进
  • chore(locale): 国际化改进
  • chore: 其他改动(是关于什么的改动?)

这个 PR 涉及以下平台:

  • NutUI H5 @nutui/nutui
  • NutUI Taro @nutui/nutui-taro

这个 PR 是否已自测:

Summary by CodeRabbit

  • 新功能

    • 在应用中新增了 Tabbar 组件,支持在切换标签前执行自定义逻辑。
  • 文档

    • 更新了 Tabbar 组件的文档,新增了 before-switch 回调函数的说明。
    • 修改了 v-model 参数类型,从 number 改为 string \| number
  • 测试

    • 增加了关于 beforeSwitch 功能的测试用例。
    • 更新了测试描述,添加了 "Tabbar:" 前缀以提高清晰度。
  • 重构

    • 对 Tabbar 组件和 TabbarItem 组件进行了重构,优化了代码结构并加入了异步处理逻辑。

@eiinu eiinu linked an issue May 7, 2024 that may be closed by this pull request
Copy link

coderabbitai bot commented May 16, 2024

Walkthrough

这次更新主要涉及为Vue插件添加新的ESLint规则,以及在多个Vue组件和文档中引入新的beforeSwitch功能。这个功能允许在切换标签之前执行异步检查逻辑,增强了标签切换的灵活性和可控性。

Changes

文件路径 修改摘要
packages/nutui-eslint-config/index.js 添加了新的ESLint规则'no-empty'
.../nutui-taro-demo/src/nav/pages/tabbar/before-switch.vue
.../__VUE/tabbar/demo/before-switch.vue
.../__VUE/tabbar/demo/index.vue
新增了before-switch.vue组件,包含标签切换逻辑。
src/packages/__VUE/tabbar/__tests__/index.spec.tsx 更新了测试描述,添加了props.beforeSwitch功能的测试。
src/packages/__VUE/tabbar/doc.en-US.md
src/packages/__VUE/tabbar/doc.md
src/packages/__VUE/tabbar/doc.taro.md
更新了文档,添加了before-switch回调函数的描述,并修改了v-model参数类型。
src/packages/__VUE/tabbar/tabbar.taro.vue
src/packages/__VUE/tabbar/tabbar.vue
引入了beforeSwitch属性,更新了changeIndex函数以处理异步beforeSwitch检查。
src/packages/__VUE/tabbaritem/tabbar-item.taro.vue
src/packages/__VUE/tabbaritem/tabbar-item.vue
重构了change函数为异步函数,并添加了错误处理逻辑。

🐰✨

代码如诗,变化如梦,
标签切换,灵活掌控。
异步检查,前路通畅,
ESLint新规,代码更强。
Vue中绽放,创新无疆,
代码世界,因你而亮。

— CodeRabbit


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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

codecov bot commented May 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.98%. Comparing base (87f044d) to head (3863c1e).

Additional details and impacted files
@@            Coverage Diff             @@
##               v4    #3060      +/-   ##
==========================================
- Coverage   83.99%   83.98%   -0.02%     
==========================================
  Files         226      226              
  Lines       22939    22941       +2     
  Branches     2476     2482       +6     
==========================================
- Hits        19268    19267       -1     
- Misses       3653     3656       +3     
  Partials       18       18              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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: 2

Out of diff range and nitpick comments (4)
src/packages/__VUE/tabbar/doc.taro.md (1)

Line range hint 3-3: 标题级别应逐级递增。

- ### 介绍
+ ## 介绍
src/packages/__VUE/tabbar/doc.md (1)

Line range hint 3-3: 标题级别应逐级递增。

- ### 介绍
+ ## 介绍
src/packages/__VUE/tabbar/doc.en-US.md (2)

Line range hint 3-3: 标题级别应逐级递增。

- ### Intro
+ ## Intro

Line range hint 38-40: 建议在句子中添加连字符和逗号以提高清晰度和正确性。

- After setting the dot property, a small red dot appears in the upper right corner of the icon; When the value property is set, the corresponding logo is displayed in the upper right corner of the icon. The bardge logo component is used internally and its properties can be applied directly to the TabbarItem props.
+ After setting the dot property, a small red dot appears in the upper-right corner of the icon; When the value property is set, the corresponding logo is displayed in the upper-right corner of the icon. The bardge logo component is used internally, and its properties can be applied directly to the TabbarItem props.
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 87f044d and 3863c1e.
Files ignored due to path filters (1)
  • src/packages/__VUE/tabbar/__tests__/__snapshots__/index.spec.tsx.snap is excluded by !**/*.snap
Files selected for processing (13)
  • packages/nutui-eslint-config/index.js (1 hunks)
  • packages/nutui-taro-demo/src/nav/pages/tabbar/before-switch.vue (1 hunks)
  • packages/nutui-taro-demo/src/nav/pages/tabbar/index.vue (4 hunks)
  • src/packages/__VUE/tabbar/tests/index.spec.tsx (11 hunks)
  • src/packages/__VUE/tabbar/demo/before-switch.vue (1 hunks)
  • src/packages/__VUE/tabbar/demo/index.vue (4 hunks)
  • src/packages/__VUE/tabbar/doc.en-US.md (3 hunks)
  • src/packages/__VUE/tabbar/doc.md (3 hunks)
  • src/packages/__VUE/tabbar/doc.taro.md (3 hunks)
  • src/packages/__VUE/tabbar/tabbar.taro.vue (5 hunks)
  • src/packages/__VUE/tabbar/tabbar.vue (3 hunks)
  • src/packages/__VUE/tabbaritem/tabbar-item.taro.vue (3 hunks)
  • src/packages/__VUE/tabbaritem/tabbar-item.vue (3 hunks)
Additional Context Used
GitHub Check Runs (1)
CodeFactor success (1)

src/packages/__VUE/tabbar/tests/index.spec.tsx: [warning] 196-196: src/packages/__VUE/tabbar/tests/index.spec.tsx#L196
Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)

Additional comments not posted (30)
src/packages/__VUE/tabbar/demo/before-switch.vue (2)

1-29: 模板部分看起来很好,遵循了最佳实践。


31-41: 脚本设置部分看起来很好,beforeSwitch 函数实现简单且正确。

packages/nutui-taro-demo/src/nav/pages/tabbar/before-switch.vue (2)

1-29: 模板部分看起来很好,遵循了最佳实践。


31-41: 脚本设置部分看起来很好,beforeSwitch 函数实现简单且正确。

src/packages/__VUE/tabbar/demo/index.vue (2)

Line range hint 1-26: 模板部分看起来很好,遵循了最佳实践。


Line range hint 42-64: 脚本设置部分看起来很好,遵循了最佳实践。

packages/nutui-taro-demo/src/nav/pages/tabbar/index.vue (2)

Line range hint 1-26: 模板部分看起来很好,遵循了最佳实践。


Line range hint 42-64: 脚本设置部分看起来很好,遵循了最佳实践。

packages/nutui-eslint-config/index.js (1)

20-22: 新规则添加正确,符合最佳实践。

src/packages/__VUE/tabbar/tabbar.vue (2)

25-25: beforeSwitch 属性添加正确,符合最佳实践。

Also applies to: 35-35


55-58: changeIndex 函数修改正确,符合最佳实践。

src/packages/__VUE/tabbar/tabbar.taro.vue (2)

28-28: beforeSwitch 属性添加正确,符合最佳实践。

Also applies to: 38-38


50-53: changeIndex 函数修改正确,符合最佳实践。

src/packages/__VUE/tabbaritem/tabbar-item.vue (1)

62-80: change 函数修改正确,符合最佳实践。

src/packages/__VUE/tabbar/doc.taro.md (2)

52-55: 关于 before-switch 属性的文档描述正确。


Line range hint 80-80: 请确保链接片段有效。

Also applies to: 111-111

src/packages/__VUE/tabbar/doc.md (2)

52-55: 关于 before-switch 属性的文档描述正确。


Line range hint 80-80: 请确保链接片段有效。

Also applies to: 113-113

src/packages/__VUE/tabbar/doc.en-US.md (2)

52-55: 关于 before-switch 属性的文档描述正确。


Line range hint 113-113: 请确保链接片段有效。

src/packages/__VUE/tabbar/__tests__/index.spec.tsx (10)

13-13: 测试看起来不错,验证了默认情况下 Tabbar 的渲染。


29-29: 测试看起来不错,验证了使用 img 属性时自定义图片的渲染。


66-66: 测试看起来不错,验证了使用 prop 时自定义颜色和徽章的渲染。


84-84: 测试看起来不错,验证了使用 bottom 属性时固定元素的渲染。


93-93: 测试看起来不错,验证了通过点击匹配活动的 Tabbar。


116-116: 测试看起来不错,验证了点击时 tabSwitch 事件的触发。


133-133: 测试看起来不错,验证了使用 placeholder 和 bottom 属性时占位符元素的渲染。


145-145: 测试看起来不错,验证了存在路由器并使用 to 属性时的重定向。


161-161: 测试看起来不错,验证了在没有路由器存在并使用 to 属性时调用 location.replace。


179-179: 测试看起来不错,验证了使用 href 属性时设置 window.location.href。

Comment on lines +194 to +217
test('Tabbar: props.beforeSwitch', async () => {
const val = ref(0)
const beforeSwitch = (_: any, index: string | number) => {
return Number(index) % 2 === 0
}
const wrapper = mount(() => {
return (
<Tabbar v-model={val.value} beforeSwitch={beforeSwitch}>
<TabbarItem tab-title="首页" icon={h(Home)}></TabbarItem>
<TabbarItem tab-title="分类" icon={h(Category)}></TabbarItem>
<TabbarItem tab-title="发现" icon={h(Find)}></TabbarItem>
</Tabbar>
)
})
const items = wrapper.findAll('.nut-tabbar-item')
expect(items.length).toBe(3)
items[2].trigger('click')
await nextTick()
expect(val.value).toBe(2)

items[1].trigger('click')
await nextTick()
expect(val.value).toBe(2)
})
Copy link

Choose a reason for hiding this comment

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

避免使用 any 类型,建议指定一个更具体的类型。

- const beforeSwitch = (_: any, index: string | number) => {
+ const beforeSwitch = (_: unknown, index: string | number) => {

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
test('Tabbar: props.beforeSwitch', async () => {
const val = ref(0)
const beforeSwitch = (_: any, index: string | number) => {
return Number(index) % 2 === 0
}
const wrapper = mount(() => {
return (
<Tabbar v-model={val.value} beforeSwitch={beforeSwitch}>
<TabbarItem tab-title="首页" icon={h(Home)}></TabbarItem>
<TabbarItem tab-title="分类" icon={h(Category)}></TabbarItem>
<TabbarItem tab-title="发现" icon={h(Find)}></TabbarItem>
</Tabbar>
)
})
const items = wrapper.findAll('.nut-tabbar-item')
expect(items.length).toBe(3)
items[2].trigger('click')
await nextTick()
expect(val.value).toBe(2)
items[1].trigger('click')
await nextTick()
expect(val.value).toBe(2)
})
test('Tabbar: props.beforeSwitch', async () => {
const val = ref(0)
const beforeSwitch = (_: unknown, index: string | number) => {
return Number(index) % 2 === 0
}
const wrapper = mount(() => {
return (
<Tabbar v-model={val.value} beforeSwitch={beforeSwitch}>
<TabbarItem tab-title="首页" icon={h(Home)}></TabbarItem>
<TabbarItem tab-title="分类" icon={h(Category)}></TabbarItem>
<TabbarItem tab-title="发现" icon={h(Find)}></TabbarItem>
</Tabbar>
)
})
const items = wrapper.findAll('.nut-tabbar-item')
expect(items.length).toBe(3)
items[2].trigger('click')
await nextTick()
expect(val.value).toBe(2)
items[1].trigger('click')
await nextTick()
expect(val.value).toBe(2)
})

Comment on lines +62 to +80
const change = async () => {
try {
const key = props.name ?? index.value
await parent.changeIndex(index.value, key)

if (parent.children[index.value]?.href) {
window.location.href = parent.children[index.value].href
return
}
if (parent.children[index.value]?.href) {
window.location.href = parent.children[index.value].href
return
}

if (parent.children[index.value]?.to) {
const to = parent.children[index.value].to
if (to && router) {
router.push(to)
} else {
location.replace(to)
if (parent.children[index.value]?.to) {
const to = parent.children[index.value].to
if (to && router) {
router.push(to)
} else {
location.replace(to)
}
}
}
} catch (err) { }
Copy link

Choose a reason for hiding this comment

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

建议在错误处理块中记录错误信息,以便于调试和排查问题。

-  } catch (err) { }
+  } catch (err) {
+    console.error('Error in change function:', err);
+  }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
const change = async () => {
try {
const key = props.name ?? index.value
await parent.changeIndex(index.value, key)
if (parent.children[index.value]?.href) {
window.location.href = parent.children[index.value].href
return
}
if (parent.children[index.value]?.href) {
window.location.href = parent.children[index.value].href
return
}
if (parent.children[index.value]?.to) {
const to = parent.children[index.value].to
if (to && router) {
router.push(to)
} else {
location.replace(to)
if (parent.children[index.value]?.to) {
const to = parent.children[index.value].to
if (to && router) {
router.push(to)
} else {
location.replace(to)
}
}
}
} catch (err) { }
const change = async () => {
try {
const key = props.name ?? index.value
await parent.changeIndex(index.value, key)
if (parent.children[index.value]?.href) {
window.location.href = parent.children[index.value].href
return
}
if (parent.children[index.value]?.to) {
const to = parent.children[index.value].to
if (to && router) {
router.push(to)
} else {
location.replace(to)
}
}
} catch (err) {
console.error('Error in change function:', err);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FR]: tabbar添加新功能
1 participant