Skip to content

Commit

Permalink
refactor: use mergeProp instead
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Jul 2, 2024
1 parent 3af77d5 commit 157664d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
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`}>
{
componentConfig.backIcon || backArrow || <LeftOutline />
}
{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()
})
})
})

0 comments on commit 157664d

Please sign in to comment.