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
18 changes: 10 additions & 8 deletions src/components/card/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,25 +16,28 @@ export type CardProps = {
onBodyClick?: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => void
onHeaderClick?: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => void
children?: ReactNode
prefixCls?: string
} & NativeProps

export const Card: FC<CardProps> = props => {
const { getPrefixCls } = useConfig()
const prefixCls = getPrefixCls('card', props.prefixCls)
const renderHeader = () => {
if (!(props.title || props.extra)) {
return null
}
return (
<div
className={classNames(`${classPrefix}-header`, props.headerClassName)}
className={classNames(`${prefixCls}-header`, props.headerClassName)}
style={props.headerStyle}
onClick={props.onHeaderClick}
>
{props.icon && (
<div className={`${classPrefix}-header-icon`}>{props.icon}</div>
<div className={`${prefixCls}-header-icon`}>{props.icon}</div>
)}
<div className={`${classPrefix}-header-title`}>{props.title}</div>
<div className={`${prefixCls}-header-title`}>{props.title}</div>
{props.extra && (
<div className={`${classPrefix}-header-extra`}>{props.extra}</div>
<div className={`${prefixCls}-header-extra`}>{props.extra}</div>
)}
</div>
)
Expand All @@ -47,7 +49,7 @@ export const Card: FC<CardProps> = props => {
}
return (
<div
className={classNames(`${classPrefix}-body`, props.bodyClassName)}
className={classNames(`${prefixCls}-body`, props.bodyClassName)}
style={props.bodyStyle}
onClick={props.onBodyClick}
>
Expand All @@ -58,7 +60,7 @@ export const Card: FC<CardProps> = props => {

return withNativeProps(
props,
<div className={classPrefix} onClick={props.onClick}>
<div className={prefixCls} onClick={props.onClick}>
{renderHeader()}
{renderBody()}
</div>
Expand Down
39 changes: 39 additions & 0 deletions src/components/card/tests/__snapshots__/card.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should apply prefixCls from ConfigProvider 1`] = `
<div>
<div
class="config-prefix-card"
data-testid="test-card-id"
>
<div
class="config-prefix-card-header"
>
<div
class="config-prefix-card-header-title"
>
title
</div>
</div>
</div>
</div>
`;

exports[`should prioritize component prefixCls over ConfigProvider 1`] = `
<div>
<div
class="component-prefix"
data-testid="test-card-id"
>
<div
class="component-prefix-header"
>
<div
class="component-prefix-header-title"
>
title
</div>
</div>
</div>
</div>
`;
25 changes: 25 additions & 0 deletions src/components/card/tests/card.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down Expand Up @@ -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(
<ConfigProvider prefixCls='config-prefix'>
<Card title='title' data-testid='test-card-id' />
</ConfigProvider>
)
expect(container.querySelector('.config-prefix-card')).toBeTruthy()
expect(container).toMatchSnapshot()
})

test('should prioritize component prefixCls over ConfigProvider', () => {
const { container } = render(
<ConfigProvider prefixCls='config-prefix'>
<Card
title='title'
data-testid='test-card-id'
prefixCls='component-prefix'
/>
</ConfigProvider>
)
expect(container.querySelector('.component-prefix')).toBeTruthy()
expect(container.querySelector('.config-prefix-card')).toBeFalsy()
expect(container).toMatchSnapshot()
})
Loading