Skip to content

Commit 090f3a7

Browse files
author
刘欢
committed
feat(Popup): unified provision of custom prefixCls
1 parent bfde19d commit 090f3a7

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

src/components/popup/popup-base-props.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { CloseOutline } from 'antd-mobile-icons'
12
import React, { CSSProperties, ReactNode } from 'react'
23
import { GetContainer } from '../../utils/render-to-container'
3-
import { MaskProps } from '../mask'
44
import { PropagationEvent } from '../../utils/with-stop-propagation'
5-
import { CloseOutline } from 'antd-mobile-icons'
5+
import { MaskProps } from '../mask'
66

77
export type PopupBaseProps = {
88
afterClose?: () => void
@@ -24,6 +24,7 @@ export type PopupBaseProps = {
2424
showCloseButton?: boolean
2525
stopPropagation?: PropagationEvent[]
2626
visible?: boolean
27+
prefixCls?: string
2728
}
2829

2930
export const defaultPopupBaseProps = {

src/components/popup/popup.tsx

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1+
import { animated, useSpring } from '@react-spring/web'
2+
import { useDrag } from '@use-gesture/react'
3+
import { useIsomorphicLayoutEffect, useUnmountedRef } from 'ahooks'
14
import classNames from 'classnames'
2-
import React, { useState, useRef } from 'react'
35
import type { FC, PropsWithChildren } from 'react'
4-
import { useIsomorphicLayoutEffect, useUnmountedRef } from 'ahooks'
6+
import React, { useRef, useState } from 'react'
57
import { NativeProps, withNativeProps } from '../../utils/native-props'
6-
import { mergeProps } from '../../utils/with-default-props'
7-
import Mask from '../mask'
8-
import { useLockScroll } from '../../utils/use-lock-scroll'
98
import { renderToContainer } from '../../utils/render-to-container'
10-
import { useSpring, animated } from '@react-spring/web'
11-
import { withStopPropagation } from '../../utils/with-stop-propagation'
129
import { ShouldRender } from '../../utils/should-render'
13-
import { defaultPopupBaseProps, PopupBaseProps } from './popup-base-props'
1410
import { useInnerVisible } from '../../utils/use-inner-visible'
11+
import { useLockScroll } from '../../utils/use-lock-scroll'
12+
import { mergeProps } from '../../utils/with-default-props'
13+
import { withStopPropagation } from '../../utils/with-stop-propagation'
1514
import { useConfig } from '../config-provider'
16-
import { useDrag } from '@use-gesture/react'
17-
18-
const classPrefix = `adm-popup`
15+
import Mask from '../mask'
16+
import { defaultPopupBaseProps, PopupBaseProps } from './popup-base-props'
1917

2018
export type PopupProps = PopupBaseProps &
2119
PropsWithChildren<{
@@ -31,13 +29,13 @@ const defaultProps = {
3129
}
3230

3331
export const Popup: FC<PopupProps> = p => {
34-
const { locale, popup: componentConfig = {} } = useConfig()
32+
const { locale, popup: componentConfig = {}, getPrefixCls } = useConfig()
3533
const props = mergeProps(defaultProps, componentConfig, p)
36-
34+
const prefixCls = getPrefixCls('popup', props.prefixCls)
3735
const bodyCls = classNames(
38-
`${classPrefix}-body`,
36+
`${prefixCls}-body`,
3937
props.bodyClassName,
40-
`${classPrefix}-body-position-${props.position}`
38+
`${prefixCls}-body-position-${props.position}`
4139
)
4240

4341
const [active, setActive] = useState(props.visible)
@@ -93,7 +91,7 @@ export const Popup: FC<PopupProps> = p => {
9391
withNativeProps(
9492
props,
9593
<div
96-
className={classPrefix}
94+
className={prefixCls}
9795
onClick={props.onClick}
9896
style={{
9997
display: active ? undefined : 'none',
@@ -146,7 +144,7 @@ export const Popup: FC<PopupProps> = p => {
146144
{props.showCloseButton && (
147145
<a
148146
className={classNames(
149-
`${classPrefix}-close-icon`,
147+
`${prefixCls}-close-icon`,
150148
'adm-plain-anchor'
151149
)}
152150
onClick={() => {

src/components/popup/tests/popup.test.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,37 @@ describe('Popup', () => {
6969

7070
it('context', () => {
7171
render(
72-
<ConfigProvider popup={{ closeIcon: 'little' }}>
72+
<ConfigProvider
73+
popup={{ closeIcon: 'little' }}
74+
prefixCls='config-prefix'
75+
>
7376
<Popup visible showCloseButton>
7477
foobar
7578
</Popup>
7679
</ConfigProvider>
7780
)
78-
81+
expect(document.querySelector('.config-prefix-popup')).toBeTruthy()
7982
expect(screen.getByText('little')).toBeVisible()
8083
})
8184

8285
it('props override context', () => {
8386
render(
84-
<ConfigProvider popup={{ closeIcon: 'little' }}>
85-
<Popup visible showCloseButton closeIcon='bamboo'>
87+
<ConfigProvider
88+
popup={{ closeIcon: 'little' }}
89+
prefixCls='config-prefix'
90+
>
91+
<Popup
92+
visible
93+
showCloseButton
94+
closeIcon='bamboo'
95+
prefixCls='component-prefix'
96+
>
8697
foobar
8798
</Popup>
8899
</ConfigProvider>
89100
)
90-
101+
expect(document.querySelector('.component-prefix')).toBeTruthy()
102+
expect(document.querySelector('.config-prefix-popup')).toBeFalsy()
91103
expect(screen.getByText('bamboo')).toBeVisible()
92104
})
93105
})

0 commit comments

Comments
 (0)