From 6454b836fbb9ecfd89e236c68cc1776b205447e1 Mon Sep 17 00:00:00 2001 From: Avan Date: Wed, 7 Aug 2024 11:04:24 +0800 Subject: [PATCH] feat(TabBar): add onClick props to the TabBar.Item --- src/components/tab-bar/demos/demo1.tsx | 53 ++++++++++++++++++++++---- src/components/tab-bar/index.en.md | 1 + src/components/tab-bar/index.zh.md | 1 + src/components/tab-bar/tab-bar.tsx | 4 ++ 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/components/tab-bar/demos/demo1.tsx b/src/components/tab-bar/demos/demo1.tsx index b638da8205..2d04c51457 100644 --- a/src/components/tab-bar/demos/demo1.tsx +++ b/src/components/tab-bar/demos/demo1.tsx @@ -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, @@ -10,7 +10,7 @@ import { } from 'antd-mobile-icons' export default () => { - const tabs = [ + const tabs1 = [ { key: 'home', title: '首页', @@ -37,13 +37,37 @@ export default () => { }, ] + const tabs2 = [ + { + key: 'home', + title: '首页', + icon: , + onClick: () => { + Toast.show('onClick 首页') + }, + }, + { + key: 'todo', + title: '待办', + icon: , + }, + { + key: 'personalCenter', + title: '我的', + icon: , + onClick: (key: string) => { + Toast.show(`onClick ${key}`) + }, + }, + ] + const [activeKey, setActiveKey] = useState('todo') return ( <> - {tabs.map(item => ( + {tabs1.map(item => ( ))} @@ -51,7 +75,7 @@ export default () => { - {tabs.map(item => ( + {tabs1.map(item => ( { - {tabs.map(item => ( + {tabs1.map(item => ( ))} @@ -72,7 +96,7 @@ export default () => { - {tabs.map(item => ( + {tabs1.map(item => ( ))} @@ -80,7 +104,7 @@ export default () => { - {tabs.map(item => ( + {tabs1.map(item => ( ))} @@ -88,11 +112,24 @@ export default () => { - {tabs.map(item => ( + {tabs1.map(item => ( ))} + + + + {tabs2.map(item => ( + + ))} + + ) } diff --git a/src/components/tab-bar/index.en.md b/src/components/tab-bar/index.en.md index 22990e7dc9..3f2fe36238 100644 --- a/src/components/tab-bar/index.en.md +++ b/src/components/tab-bar/index.en.md @@ -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 diff --git a/src/components/tab-bar/index.zh.md b/src/components/tab-bar/index.zh.md index 7d8873c243..08f1b9b5ec 100644 --- a/src/components/tab-bar/index.zh.md +++ b/src/components/tab-bar/index.zh.md @@ -33,6 +33,7 @@ | icon | 图标 | `ReactNode \| ((active: boolean) => ReactNode)` | - | | key | 对应 `activeKey` | `string` | - | | title | 标题 | `ReactNode \| ((active: boolean) => ReactNode)` | - | +| onClick | 点击事件的回调 | `(key: string) => void` | - | ## FAQ diff --git a/src/components/tab-bar/tab-bar.tsx b/src/components/tab-bar/tab-bar.tsx index 02c3f7e56a..83c9958d0b 100644 --- a/src/components/tab-bar/tab-bar.tsx +++ b/src/components/tab-bar/tab-bar.tsx @@ -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 */ @@ -117,8 +118,11 @@ export const TabBar: FC = 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,