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 onClick props to the TabBar.Item #6702

Merged
merged 5 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 45 additions & 8 deletions src/components/tab-bar/demos/demo1.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react'
import { Badge, TabBar } from 'antd-mobile'
import { Badge, TabBar, Toast } from 'antd-mobile'
Copy link
Member

Choose a reason for hiding this comment

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

这个 Toast 没用到……

import { DemoBlock } from 'demos'
import {
AppOutline,
Expand All @@ -10,7 +10,7 @@ import {
} from 'antd-mobile-icons'

export default () => {
const tabs = [
const tabs1 = [
{
key: 'home',
title: '首页',
Expand All @@ -37,21 +37,45 @@ export default () => {
},
]

const tabs2 = [
{
key: 'home',
title: '首页',
icon: <AppOutline />,
onClick: () => {
Toast.show('onClick 首页')
},
},
{
key: 'todo',
title: '待办',
icon: <UnorderedListOutline />,
},
{
key: 'personalCenter',
title: '我的',
icon: <UserOutline />,
onClick: (key: string) => {
Toast.show(`onClick ${key}`)
},
},
]

const [activeKey, setActiveKey] = useState('todo')

return (
<>
<DemoBlock title='基础用法' padding='0'>
<TabBar>
{tabs.map(item => (
{tabs1.map(item => (
<TabBar.Item key={item.key} icon={item.icon} title={item.title} />
))}
</TabBar>
</DemoBlock>

<DemoBlock title='徽标' padding='0'>
<TabBar>
{tabs.map(item => (
{tabs1.map(item => (
<TabBar.Item
key={item.key}
icon={item.icon}
Expand All @@ -64,35 +88,48 @@ export default () => {

<DemoBlock title='仅图标' padding='0'>
<TabBar>
{tabs.map(item => (
{tabs1.map(item => (
<TabBar.Item key={item.key} icon={item.icon} />
))}
</TabBar>
</DemoBlock>

<DemoBlock title='仅标题' padding='0'>
<TabBar>
{tabs.map(item => (
{tabs1.map(item => (
<TabBar.Item key={item.key} title={item.title} />
))}
</TabBar>
</DemoBlock>

<DemoBlock title='受控组件' padding='0'>
<TabBar activeKey={activeKey} onChange={setActiveKey}>
{tabs.map(item => (
{tabs1.map(item => (
<TabBar.Item key={item.key} icon={item.icon} title={item.title} />
))}
</TabBar>
</DemoBlock>

<DemoBlock title='开启安全区' padding='0'>
<TabBar safeArea>
{tabs.map(item => (
{tabs1.map(item => (
<TabBar.Item key={item.key} icon={item.icon} title={item.title} />
))}
</TabBar>
</DemoBlock>

<DemoBlock title='Item 点击事件' padding='0'>
zombieJ marked this conversation as resolved.
Show resolved Hide resolved
<TabBar safeArea>
{tabs2.map(item => (
<TabBar.Item
key={item.key}
icon={item.icon}
title={item.title}
onClick={item.onClick}
/>
))}
</TabBar>
</DemoBlock>
</>
)
}
1 change: 1 addition & 0 deletions src/components/tab-bar/index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Useful for switching between different pages.
| icon | Icon | `ReactNode \| ((active: boolean) => ReactNode)` | - |
| key | Corresponding to `activeKey` | `string` | - |
| title | Title | `ReactNode \| ((active: boolean) => ReactNode)` | - |
| onClick | Callback when click item | `(key: string) => void` | - |
Copy link
Member

Choose a reason for hiding this comment

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

文档加个版本号,下个 minor


## FAQ

Expand Down
1 change: 1 addition & 0 deletions src/components/tab-bar/index.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
| icon | 图标 | `ReactNode \| ((active: boolean) => ReactNode)` | - |
| key | 对应 `activeKey` | `string` | - |
| title | 标题 | `ReactNode \| ((active: boolean) => ReactNode)` | - |
| onClick | 点击事件的回调 | `(key: string) => void` | - |

## FAQ

Expand Down
4 changes: 4 additions & 0 deletions src/components/tab-bar/tab-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type TabBarItemProps = {
icon?: ReactNode | ((active: boolean) => ReactNode)
title?: ReactNode | ((active: boolean) => ReactNode)
badge?: BadgeProps['content']
onClick?: (key: string) => void
Copy link
Member

Choose a reason for hiding this comment

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

item 里的点击不需要给 key,因为事件本身就是在对应的 item 上的。直接是 VoidFunction 即可

} & NativeProps

/* istanbul ignore next */
Expand Down Expand Up @@ -117,8 +118,11 @@ export const TabBar: FC<TabBarProps> = p => {
key={item.key}
onClick={() => {
const { key } = item

if (key === undefined || key === null) return

setActiveKey(key.toString())
item.props.onClick?.(key as string)
}}
className={classNames(`${classPrefix}-item`, {
[`${classPrefix}-item-active`]: active,
Expand Down
Loading