Skip to content

Commit 73d7bab

Browse files
author
刘欢
committed
feat(Dialog): unified provision of custom prefixCls
1 parent a42e614 commit 73d7bab

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

src/components/dialog/dialog.tsx

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { NativeProps } from '../../utils/native-props'
55
import { mergeProps } from '../../utils/with-default-props'
66
import AutoCenter from '../auto-center'
77
import CenterPopup, { CenterPopupProps } from '../center-popup'
8+
import { useConfig } from '../config-provider'
89
import Image from '../image'
910
import { Action, DialogActionButton } from './dialog-action-button'
1011

@@ -22,6 +23,7 @@ export type DialogProps = Pick<
2223
| 'maskStyle'
2324
| 'stopPropagation'
2425
| 'visible'
26+
| 'prefixCls'
2527
> & {
2628
image?: string
2729
header?: ReactNode
@@ -49,24 +51,28 @@ const defaultProps = {
4951

5052
export const Dialog: FC<DialogProps> = p => {
5153
const props = mergeProps(defaultProps, p)
54+
const { getPrefixCls } = useConfig()
55+
const prefixCls = getPrefixCls('dialog', props.prefixCls)
5256

5357
const element = (
5458
<>
5559
{!!props.image && (
56-
<div className={cls('image-container')}>
60+
<div className={`${prefixCls}-image-container`}>
5761
<Image src={props.image} alt='dialog header image' width='100%' />
5862
</div>
5963
)}
6064
{!!props.header && (
61-
<div className={cls('header')}>
65+
<div className={`${prefixCls}-header`}>
6266
<AutoCenter>{props.header}</AutoCenter>
6367
</div>
6468
)}
65-
{!!props.title && <div className={cls('title')}>{props.title}</div>}
69+
{!!props.title && (
70+
<div className={`${prefixCls}-title`}>{props.title}</div>
71+
)}
6672
<div
6773
className={classNames(
68-
cls('content'),
69-
!props.content && cls('content-empty')
74+
`${prefixCls}-content`,
75+
!props.content && `${prefixCls}-content-empty`
7076
)}
7177
>
7278
{typeof props.content === 'string' ? (
@@ -75,11 +81,11 @@ export const Dialog: FC<DialogProps> = p => {
7581
props.content
7682
)}
7783
</div>
78-
<div className={cls('footer')}>
84+
<div className={`${prefixCls}-footer`}>
7985
{props.actions.map((row, index) => {
8086
const actions = Array.isArray(row) ? row : [row]
8187
return (
82-
<div className={cls('action-row')} key={index}>
88+
<div className={`${prefixCls}-action-row`} key={index}>
8389
{actions.map((action, index) => (
8490
<DialogActionButton
8591
key={action.key}
@@ -104,7 +110,7 @@ export const Dialog: FC<DialogProps> = p => {
104110

105111
return (
106112
<CenterPopup
107-
className={classNames(cls(), props.className)}
113+
className={classNames(prefixCls, props.className)}
108114
style={props.style}
109115
afterClose={props.afterClose}
110116
afterShow={props.afterShow}
@@ -119,8 +125,8 @@ export const Dialog: FC<DialogProps> = p => {
119125
getContainer={props.getContainer}
120126
bodyStyle={props.bodyStyle}
121127
bodyClassName={classNames(
122-
cls('body'),
123-
props.image && cls('with-image'),
128+
`${prefixCls}-body`,
129+
props.image && `${prefixCls}-with-image`,
124130
props.bodyClassName
125131
)}
126132
maskStyle={props.maskStyle}
@@ -131,12 +137,9 @@ export const Dialog: FC<DialogProps> = p => {
131137
forceRender={props.forceRender}
132138
role='dialog'
133139
aria-label={props['aria-label']}
140+
prefixCls={prefixCls}
134141
>
135142
{element}
136143
</CenterPopup>
137144
)
138145
}
139-
140-
function cls(name: string = '') {
141-
return 'adm-dialog' + (name && '-') + name
142-
}

src/components/dialog/tests/dialog.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
waitForElementToBeRemoved,
1313
} from 'testing'
1414
import Dialog, { DialogAlertProps } from '..'
15+
import ConfigProvider from '../../config-provider'
1516

1617
const classPrefix = `adm-dialog`
1718

@@ -234,4 +235,24 @@ describe('Dialog', () => {
234235
await promise
235236
})
236237
})
238+
239+
test('should apply custom prefixCls(ConfigProvider)', () => {
240+
render(
241+
<ConfigProvider prefixCls='config-prefix'>
242+
<Dialog visible={true} />
243+
</ConfigProvider>
244+
)
245+
expect(document.querySelector('.config-prefix-dialog')).toBeTruthy()
246+
expect(document.querySelector('.config-prefix-dialog-content')).toBeTruthy()
247+
})
248+
249+
test('should apply custom prefixCls(component)', () => {
250+
render(
251+
<ConfigProvider prefixCls='config-prefix'>
252+
<Dialog visible={true} prefixCls='component-prefix' />
253+
</ConfigProvider>
254+
)
255+
expect(document.querySelector('.component-prefix')).toBeTruthy()
256+
expect(document.querySelector('.component-prefix-content')).toBeTruthy()
257+
})
237258
})

0 commit comments

Comments
 (0)