diff --git a/src/components/card/card.tsx b/src/components/card/card.tsx index 992de61950..98fb8862e7 100644 --- a/src/components/card/card.tsx +++ b/src/components/card/card.tsx @@ -2,8 +2,7 @@ import classNames from 'classnames' import type { CSSProperties, FC, ReactNode } from 'react' import React from 'react' import { NativeProps, withNativeProps } from '../../utils/native-props' - -const classPrefix = `adm-card` +import { useConfig } from '../config-provider' export type CardProps = { title?: ReactNode @@ -17,25 +16,28 @@ export type CardProps = { onBodyClick?: (event: React.MouseEvent) => void onHeaderClick?: (event: React.MouseEvent) => void children?: ReactNode + prefixCls?: string } & NativeProps export const Card: FC = props => { + const { getPrefixCls } = useConfig() + const prefixCls = getPrefixCls('card', props.prefixCls) const renderHeader = () => { if (!(props.title || props.extra)) { return null } return (
{props.icon && ( -
{props.icon}
+
{props.icon}
)} -
{props.title}
+
{props.title}
{props.extra && ( -
{props.extra}
+
{props.extra}
)}
) @@ -47,7 +49,7 @@ export const Card: FC = props => { } return (
@@ -58,7 +60,7 @@ export const Card: FC = props => { return withNativeProps( props, -
+
{renderHeader()} {renderBody()}
diff --git a/src/components/card/tests/__snapshots__/card.test.tsx.snap b/src/components/card/tests/__snapshots__/card.test.tsx.snap new file mode 100644 index 0000000000..e6fa28ea7a --- /dev/null +++ b/src/components/card/tests/__snapshots__/card.test.tsx.snap @@ -0,0 +1,39 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should apply prefixCls from ConfigProvider 1`] = ` +
+
+
+
+ title +
+
+
+
+`; + +exports[`should prioritize component prefixCls over ConfigProvider 1`] = ` +
+
+
+
+ title +
+
+
+
+`; diff --git a/src/components/card/tests/card.test.tsx b/src/components/card/tests/card.test.tsx index ed4cfdddf6..77f32812e2 100644 --- a/src/components/card/tests/card.test.tsx +++ b/src/components/card/tests/card.test.tsx @@ -2,6 +2,7 @@ import { RightOutline } from 'antd-mobile-icons' import * as React from 'react' import { fireEvent, render, testA11y, waitFor } from 'testing' import Card from '../' +import ConfigProvider from '../../config-provider' const classPrefix = `adm-card` @@ -47,3 +48,27 @@ test('renders without children', async () => { ) expect(getByTestId('test-card-id')).not.toHaveClass(`${classPrefix}-body`) }) +test('should apply prefixCls from ConfigProvider', () => { + const { container } = render( + + + + ) + expect(container.querySelector('.config-prefix-card')).toBeTruthy() + expect(container).toMatchSnapshot() +}) + +test('should prioritize component prefixCls over ConfigProvider', () => { + const { container } = render( + + + + ) + expect(container.querySelector('.component-prefix')).toBeTruthy() + expect(container.querySelector('.config-prefix-card')).toBeFalsy() + expect(container).toMatchSnapshot() +})