Skip to content
Open
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
22 changes: 11 additions & 11 deletions src/components/badge/badge.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import classNames from 'classnames'
import type { CSSProperties, FC, ReactNode } from 'react'
import React from 'react'
import type { FC, ReactNode, CSSProperties } from 'react'
import { NativeProps, withNativeProps } from '../../utils/native-props'

const classPrefix = `adm-badge`
import { useConfig } from '../config-provider'

export const dot = <React.Fragment />

Expand All @@ -14,17 +13,20 @@ export type BadgeProps = {
children?: ReactNode
wrapperClassName?: string
wrapperStyle?: CSSProperties
prefixCls?: string
} & NativeProps<'--right' | '--top' | '--color'>

export const Badge: FC<BadgeProps> = props => {
const { content, color, children } = props
const { getPrefixCls } = useConfig()
const prefixCls = getPrefixCls('badge', props?.prefixCls)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The props object in a React functional component is guaranteed to be defined. Therefore, the optional chaining operator (?.) is not necessary here. You can safely access props.prefixCls directly for cleaner code.

Suggested change
const prefixCls = getPrefixCls('badge', props?.prefixCls)
const prefixCls = getPrefixCls('badge', props.prefixCls)


const isDot = content === dot

const badgeClass = classNames(classPrefix, {
[`${classPrefix}-fixed`]: !!children,
[`${classPrefix}-dot`]: isDot,
[`${classPrefix}-bordered`]: props.bordered,
const badgeClass = classNames(prefixCls, {
[`${prefixCls}-fixed`]: !!children,
[`${prefixCls}-dot`]: isDot,
[`${prefixCls}-bordered`]: props.bordered,
})

const element =
Expand All @@ -39,16 +41,14 @@ export const Badge: FC<BadgeProps> = props => {
} as BadgeProps['style']
}
>
{!isDot && (
<div className={`${classPrefix}-content`}>{content}</div>
)}
{!isDot && <div className={`${prefixCls}-content`}>{content}</div>}
</div>
)
: null

return children ? (
<div
className={classNames(`${classPrefix}-wrapper`, props.wrapperClassName)}
className={classNames(`${prefixCls}-wrapper`, props.wrapperClassName)}
style={props.wrapperStyle}
>
{children}
Expand Down
40 changes: 40 additions & 0 deletions src/components/badge/tests/__snapshots__/badge.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,43 @@ exports[`Badge renders with color 1`] = `
</div>
</div>
`;

exports[`Badge should apply custom prefixCls(ConfigProvider) 1`] = `
<div>
<div
class="config-prefix-badge-wrapper"
>
text
<div
class="config-prefix-badge config-prefix-badge-fixed"
style="--color: #108ee9;"
>
<div
class="config-prefix-badge-content"
>
</div>
</div>
</div>
</div>
`;

exports[`Badge should apply custom prefixCls(component) 1`] = `
<div>
<div
class="component-prefix-wrapper"
>
text
<div
class="component-prefix component-prefix-fixed"
style="--color: #108ee9;"
>
<div
class="component-prefix-content"
>
</div>
</div>
</div>
</div>
`;
26 changes: 26 additions & 0 deletions src/components/badge/tests/badge.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react'
import { render, testA11y } from 'testing'
import Badge from '../'
import ConfigProvider from '../../config-provider'

describe('Badge', () => {
test('passes a11y test', async () => {
Expand Down Expand Up @@ -44,4 +45,29 @@ describe('Badge', () => {
expect(element.parentNode).toHaveClass('test')
expect(element.parentNode).toHaveStyle('color: red')
})

test('should apply custom prefixCls(component)', () => {
const { container } = render(
<ConfigProvider prefixCls='config-prefix'>
<Badge color='#108ee9' content='新' prefixCls='component-prefix'>
text
</Badge>
</ConfigProvider>
)
expect(container.querySelector('.component-prefix')).toBeTruthy()
expect(container.querySelector('.config-prefix-badge')).toBeFalsy()
expect(container).toMatchSnapshot()
})

test('should apply custom prefixCls(ConfigProvider)', () => {
const { container } = render(
<ConfigProvider prefixCls='config-prefix'>
<Badge color='#108ee9' content='新'>
text
</Badge>
</ConfigProvider>
)
expect(container.querySelector('.config-prefix-badge')).toBeTruthy()
expect(container).toMatchSnapshot()
})
})
Loading