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 3 commits
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
2 changes: 1 addition & 1 deletion 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'

Check warning on line 2 in src/components/tab-bar/demos/demo1.tsx

View workflow job for this annotation

GitHub Actions / check

'Toast' is defined but never used
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 Down
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
16 changes: 16 additions & 0 deletions src/components/tab-bar/tests/tab-bar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,20 @@ describe('TabBar', () => {

expect(container).toMatchSnapshot()
})

test('item onClick should be called', () => {
const onClickFn = jest.fn()

render(
<TabBar>
<TabBar.Item key='home' />
<TabBar.Item key='icon' onClick={onClickFn} />
<TabBar.Item />
</TabBar>
)

fireEvent.click(document.querySelectorAll(`.adm-tab-bar-item`)[1])

expect(onClickFn).toBeCalled()
})
})
Loading