Skip to content

Commit

Permalink
feat(TabBar): add onClick props to the TabBar.Item
Browse files Browse the repository at this point in the history
  • Loading branch information
Layouwen committed Aug 7, 2024
1 parent 7d99732 commit 6454b83
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
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'
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'>
<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` | - |

## 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
} & 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

0 comments on commit 6454b83

Please sign in to comment.