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

fix: nav_bar backIcon attribute is compatible with backArrow attribute #6657

Merged
merged 2 commits into from
Jul 2, 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
23 changes: 14 additions & 9 deletions src/components/nav-bar/nav-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import classNames from 'classnames'
import type { FC, ReactNode } from 'react'
import React from 'react'
import { NativeProps, withNativeProps } from '../../utils/native-props'
import { mergeProps } from '../../utils/with-default-props'
import { mergeProp, mergeProps } from '../../utils/with-default-props'
import { useConfig } from '../config-provider'

const classPrefix = `adm-nav-bar`
Expand All @@ -21,26 +21,31 @@ export type NavBarProps = {
children?: ReactNode
} & NativeProps<'--height' | '--border-bottom'>

const defaultProps = {
backIcon: true,
}
const defaultBackIcon = <LeftOutline />

export const NavBar: FC<NavBarProps> = props => {
const { navBar: componentConfig = {} } = useConfig()
const mergedProps = mergeProps(defaultProps, componentConfig, props)
const mergedProps = mergeProps(componentConfig, props)
const { back, backIcon, backArrow } = mergedProps

const mergedDefaultBackIcon = componentConfig.backIcon || defaultBackIcon

const mergedBackIcon = mergeProp<ReactNode>(
defaultBackIcon,
componentConfig.backIcon,
backArrow === true ? mergedDefaultBackIcon : backArrow,
backIcon === true ? mergedDefaultBackIcon : backIcon
)

return withNativeProps(
mergedProps,
<div className={classNames(classPrefix)}>
<div className={`${classPrefix}-left`} role='button'>
{back !== null && (
<div className={`${classPrefix}-back`} onClick={mergedProps.onBack}>
{(backIcon || backArrow) && (
{mergedBackIcon && (
<span className={`${classPrefix}-back-arrow`}>
{backIcon === true || backArrow === true
? componentConfig.backIcon || <LeftOutline />
: backIcon || backArrow}
{mergedBackIcon}
</span>
)}
<span aria-hidden='true'>{back}</span>
Expand Down
20 changes: 11 additions & 9 deletions src/components/nav-bar/tests/nav-bar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ describe('NavBar', () => {
})

it('legacy', () => {
const { baseElement } = render(<NavBar backArrow>Title</NavBar>)
expect(baseElement.querySelector('.antd-mobile-icon')).toBeTruthy()
const { baseElement } = render(
<NavBar backArrow={<span className='bamboo' />}>Title</NavBar>
)
expect(baseElement.querySelector('.bamboo')).toBeTruthy()
})

it('props', () => {
Expand All @@ -35,23 +37,23 @@ describe('NavBar', () => {
})

it('context', () => {
render(
<ConfigProvider navBar={{ backIcon: 'little' }}>
const { baseElement } = render(
<ConfigProvider navBar={{ backIcon: <span className='little' /> }}>
<NavBar backIcon>Title</NavBar>
</ConfigProvider>
)

expect(screen.getByText('little')).toBeVisible()
expect(baseElement.querySelector('.little')).toBeTruthy()
})

it('props override context', () => {
render(
<ConfigProvider navBar={{ backIcon: 'little' }}>
<NavBar backIcon='bamboo'>Title</NavBar>
const { baseElement } = render(
<ConfigProvider navBar={{ backIcon: <span className='little' /> }}>
<NavBar backIcon={<span className='bamboo' />}>Title</NavBar>
</ConfigProvider>
)

expect(screen.getByText('bamboo')).toBeVisible()
expect(baseElement.querySelector('.bamboo')).toBeTruthy()
})
})
})
Loading