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 all 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
11 changes: 6 additions & 5 deletions src/components/tab-bar/index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ Useful for switching between different pages.

### Props

| Name | Description | Type | Default |
| --- | --- | --- | --- |
| badge | Badge,the same as [Badge](/components/badge) `content` prop | `React.ReactNode \| typeof Badge.dot` | - |
| icon | Icon | `ReactNode \| ((active: boolean) => ReactNode)` | - |
| Name | Description | Type | Default | Version |
| --- | --- | --- | --- | --- |
| badge | Badge,the same as [Badge](/components/badge) `content` prop | `React.ReactNode \| typeof Badge.dot` | - | |
| icon | Icon | `ReactNode \| ((active: boolean) => ReactNode)` | - | |
| key | Corresponding to `activeKey` | `string` | - |
| title | Title | `ReactNode \| ((active: boolean) => ReactNode)` | - |
| title | Title | `ReactNode \| ((active: boolean) => ReactNode)` | - | |
| onClick | Callback when click item | `() => void` | - | 5.38.0 |

## FAQ

Expand Down
13 changes: 7 additions & 6 deletions src/components/tab-bar/index.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@

### 属性

| 属性 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| badge | 徽标,同 [Badge](/zh/components/badge) 的 `content` 属性 | `React.ReactNode \| typeof Badge.dot` | - |
| icon | 图标 | `ReactNode \| ((active: boolean) => ReactNode)` | - |
| key | 对应 `activeKey` | `string` | - |
| title | 标题 | `ReactNode \| ((active: boolean) => ReactNode)` | - |
| 属性 | 说明 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
| badge | 徽标,同 [Badge](/zh/components/badge) 的 `content` 属性 | `React.ReactNode \| typeof Badge.dot` | - | |
| icon | 图标 | `ReactNode \| ((active: boolean) => ReactNode)` | - | |
| key | 对应 `activeKey` | `string` | - | |
| title | 标题 | `ReactNode \| ((active: boolean) => ReactNode)` | - | |
| onClick | 点击事件的回调 | `() => void` | - | 5.38.0 |

## 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?: () => 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?.()
}}
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