Skip to content

Commit fa390e6

Browse files
author
刘欢
committed
feat(card): unified provision of custom prefixCls
1 parent e60f45a commit fa390e6

File tree

3 files changed

+74
-8
lines changed

3 files changed

+74
-8
lines changed

src/components/card/card.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import classNames from 'classnames'
22
import type { CSSProperties, FC, ReactNode } from 'react'
33
import React from 'react'
44
import { NativeProps, withNativeProps } from '../../utils/native-props'
5-
6-
const classPrefix = `adm-card`
5+
import { useConfig } from '../config-provider'
76

87
export type CardProps = {
98
title?: ReactNode
@@ -17,25 +16,28 @@ export type CardProps = {
1716
onBodyClick?: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => void
1817
onHeaderClick?: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => void
1918
children?: ReactNode
19+
prefixCls?: string
2020
} & NativeProps
2121

2222
export const Card: FC<CardProps> = props => {
23+
const { getPrefixCls } = useConfig()
24+
const prefixCls = getPrefixCls('card', props.prefixCls)
2325
const renderHeader = () => {
2426
if (!(props.title || props.extra)) {
2527
return null
2628
}
2729
return (
2830
<div
29-
className={classNames(`${classPrefix}-header`, props.headerClassName)}
31+
className={classNames(`${prefixCls}-header`, props.headerClassName)}
3032
style={props.headerStyle}
3133
onClick={props.onHeaderClick}
3234
>
3335
{props.icon && (
34-
<div className={`${classPrefix}-header-icon`}>{props.icon}</div>
36+
<div className={`${prefixCls}-header-icon`}>{props.icon}</div>
3537
)}
36-
<div className={`${classPrefix}-header-title`}>{props.title}</div>
38+
<div className={`${prefixCls}-header-title`}>{props.title}</div>
3739
{props.extra && (
38-
<div className={`${classPrefix}-header-extra`}>{props.extra}</div>
40+
<div className={`${prefixCls}-header-extra`}>{props.extra}</div>
3941
)}
4042
</div>
4143
)
@@ -47,7 +49,7 @@ export const Card: FC<CardProps> = props => {
4749
}
4850
return (
4951
<div
50-
className={classNames(`${classPrefix}-body`, props.bodyClassName)}
52+
className={classNames(`${prefixCls}-body`, props.bodyClassName)}
5153
style={props.bodyStyle}
5254
onClick={props.onBodyClick}
5355
>
@@ -58,7 +60,7 @@ export const Card: FC<CardProps> = props => {
5860

5961
return withNativeProps(
6062
props,
61-
<div className={classPrefix} onClick={props.onClick}>
63+
<div className={prefixCls} onClick={props.onClick}>
6264
{renderHeader()}
6365
{renderBody()}
6466
</div>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`should apply prefixCls from ConfigProvider 1`] = `
4+
<div>
5+
<div
6+
class="config-prefix-card"
7+
data-testid="test-card-id"
8+
>
9+
<div
10+
class="config-prefix-card-header"
11+
>
12+
<div
13+
class="config-prefix-card-header-title"
14+
>
15+
title
16+
</div>
17+
</div>
18+
</div>
19+
</div>
20+
`;
21+
22+
exports[`should prioritize component prefixCls over ConfigProvider 1`] = `
23+
<div>
24+
<div
25+
class="component-prefix"
26+
data-testid="test-card-id"
27+
>
28+
<div
29+
class="component-prefix-header"
30+
>
31+
<div
32+
class="component-prefix-header-title"
33+
>
34+
title
35+
</div>
36+
</div>
37+
</div>
38+
</div>
39+
`;

src/components/card/tests/card.test.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { RightOutline } from 'antd-mobile-icons'
22
import * as React from 'react'
33
import { fireEvent, render, testA11y, waitFor } from 'testing'
44
import Card from '../'
5+
import ConfigProvider from '../../config-provider'
56

67
const classPrefix = `adm-card`
78

@@ -47,3 +48,27 @@ test('renders without children', async () => {
4748
)
4849
expect(getByTestId('test-card-id')).not.toHaveClass(`${classPrefix}-body`)
4950
})
51+
test('should apply prefixCls from ConfigProvider', () => {
52+
const { container } = render(
53+
<ConfigProvider prefixCls='config-prefix'>
54+
<Card title='title' data-testid='test-card-id' />
55+
</ConfigProvider>
56+
)
57+
expect(container.querySelector('.config-prefix-card')).toBeTruthy()
58+
expect(container).toMatchSnapshot()
59+
})
60+
61+
test('should prioritize component prefixCls over ConfigProvider', () => {
62+
const { container } = render(
63+
<ConfigProvider prefixCls='config-prefix'>
64+
<Card
65+
title='title'
66+
data-testid='test-card-id'
67+
prefixCls='component-prefix'
68+
/>
69+
</ConfigProvider>
70+
)
71+
expect(container.querySelector('.component-prefix')).toBeTruthy()
72+
expect(container.querySelector('.config-prefix-card')).toBeFalsy()
73+
expect(container).toMatchSnapshot()
74+
})

0 commit comments

Comments
 (0)