Skip to content

Commit a173286

Browse files
committed
feat: add baseMask components & format demo
1 parent 73124b7 commit a173286

File tree

15 files changed

+179
-14
lines changed

15 files changed

+179
-14
lines changed

.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"**/demo/**/*.jsx"
1010
],
1111
"rules": {
12-
"no-console": "off"
12+
"no-console": "off",
13+
"no-alert": "off"
1314
}
1415
}
1516
]

config/menus/mobile.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const mobileComponent: ComponentMenus = {
1010
dataEntry: [],
1111
feedback: [
1212
'src/base-popup',
13+
'src/base-mask',
1314
],
1415
guidance: [],
1516
other: [],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React, { useRef } from 'react'
2+
import type { UseModalEnhancedAction, UseModalEnhancedContext } from '@template-pro/mobile-ui'
3+
import { BaseMask, Button } from '@template-pro/mobile-ui'
4+
import styles from './style.less'
5+
6+
const Content = ({ enhancedAction }: Partial<UseModalEnhancedContext>) => (
7+
<div className={styles.overlayContent}>
8+
<h3>这是背景蒙层内容</h3>
9+
<Button onClick={enhancedAction?.close}>
10+
点击关闭背景蒙层
11+
</Button>
12+
</div>
13+
)
14+
15+
function App() {
16+
const maskRef = useRef<UseModalEnhancedAction>(null)
17+
return (
18+
<>
19+
<BaseMask
20+
trigger={<Button>打开背景蒙层</Button>}
21+
actionRef={maskRef}
22+
onMaskClick={() => window.console.log('点击了背景蒙层')}
23+
>
24+
<Content />
25+
</BaseMask>
26+
27+
<Button onClick={() => maskRef.current?.open()}>App ref 打开背景蒙层</Button>
28+
</>
29+
)
30+
}
31+
32+
export default App
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from 'react'
2+
import { BaseMask } from '@template-pro/mobile-ui'
3+
import { Button, Toast } from 'antd-mobile'
4+
import styles from './style.less'
5+
6+
export default () => (
7+
<>
8+
<BaseMask
9+
trigger={<Button>Click me</Button>}
10+
onClick={(event, action) => {
11+
window.console.log({ event, action })
12+
Toast.show({
13+
content: '1秒后打开背景蒙层',
14+
icon: 'loading',
15+
duration: 1000,
16+
afterClose() {
17+
action.open()
18+
setTimeout(() => {
19+
Toast.show({
20+
content: '1秒后关闭背景蒙层',
21+
duration: 1000,
22+
afterClose: action.close,
23+
})
24+
}, 500)
25+
},
26+
})
27+
}}
28+
>
29+
<div className={styles.overlayContent}>这是背景蒙层内容</div>
30+
</BaseMask>
31+
</>
32+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.overlayContent {
2+
position: absolute;
3+
top: 50%;
4+
left: 50%;
5+
display: flex;
6+
flex-direction: column;
7+
align-items: center;
8+
justify-content: space-around;
9+
width: 200px;
10+
height: 200px;
11+
margin-top: -100px;
12+
margin-left: -100px;
13+
background: white;
14+
border-radius: 16px;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# BaseMask
2+
3+
## 基础用法
4+
5+
<code hideActions='["CSB", "EXTERNAL"]' src="./demo/Base.tsx" />
6+
7+
## 通过事件手动控制打开
8+
9+
<code hideActions='["CSB", "EXTERNAL"]' src="./demo/Event.tsx" />
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React from 'react'
2+
import classNames from 'classnames'
3+
import type { MaskProps } from 'antd-mobile/es/components/mask'
4+
import Mask from 'antd-mobile/es/components/mask'
5+
import type { UseModalEnhancedProps } from '@template-pro/utils'
6+
import { useModalEnhanced } from '@template-pro/utils'
7+
import { defaultPrefixCls } from '../constants'
8+
9+
export type BaseMaskProps = MaskProps & Omit<UseModalEnhancedProps, 'content'> & {
10+
trigger?: React.ReactNode
11+
}
12+
13+
const BaseMask = (props: BaseMaskProps) => {
14+
const {
15+
className,
16+
children,
17+
trigger: propsTrigger,
18+
afterClose,
19+
...restProps
20+
} = props
21+
22+
const [
23+
visible,
24+
{ close },
25+
{ trigger, content },
26+
] = useModalEnhanced({
27+
children: propsTrigger,
28+
content: children,
29+
...restProps,
30+
})
31+
32+
const handleMaskClose = () => {
33+
afterClose?.()
34+
close()
35+
}
36+
37+
return (
38+
<>
39+
{trigger}
40+
<Mask
41+
visible={visible}
42+
afterClose={handleMaskClose}
43+
className={classNames(`${defaultPrefixCls}-base-mask`, className)}
44+
destroyOnClose
45+
{...restProps}
46+
>
47+
{content}
48+
</Mask>
49+
</>
50+
)
51+
}
52+
53+
export default BaseMask
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@import (less) '~antd-mobile/es/components/mask/mask.css';
2+
@import '../../styles/themes/default.less';
3+
4+
.@{cls-prefix}-base-mask {
5+
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './index.less'

packages/mobile-ui/src/base-popup/demo/Base.tsx

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { useRef } from 'react'
2-
import { Button } from 'antd-mobile'
32
import type { UseModalEnhancedAction, UseModalEnhancedContext } from '@template-pro/mobile-ui'
4-
import { BasePopup } from '@template-pro/mobile-ui'
3+
import { BasePopup, Button } from '@template-pro/mobile-ui'
54

65
const Content = ({ enhancedAction }: Partial<UseModalEnhancedContext>) => (
76
<div style={{ padding: 24 }}>
@@ -13,16 +12,22 @@ const Content = ({ enhancedAction }: Partial<UseModalEnhancedContext>) => (
1312
)
1413

1514
function App() {
16-
const drawerRef = useRef<UseModalEnhancedAction>(null)
15+
const popupRef = useRef<UseModalEnhancedAction>(null)
1716

1817
return (
19-
<BasePopup
20-
content={<Content />}
21-
bodyStyle={{ height: '40vh' }}
22-
actionRef={drawerRef}
23-
>
24-
<Button>打开弹出层</Button>
25-
</BasePopup>
18+
<>
19+
<BasePopup
20+
content={<Content />}
21+
bodyStyle={{ height: '40vh' }}
22+
actionRef={popupRef}
23+
>
24+
<Button>打开弹出层</Button>
25+
</BasePopup>
26+
27+
<Button onClick={() => popupRef.current?.open()}>
28+
App ref 打开弹出层
29+
</Button>
30+
</>
2631
)
2732
}
2833

packages/mobile-ui/src/base-popup/demo/Event.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
2-
import { BasePopup } from '@template-pro/mobile-ui'
3-
import { Button, Toast } from 'antd-mobile'
2+
import { BasePopup, Button } from '@template-pro/mobile-ui'
3+
import { Toast } from 'antd-mobile'
44

55
export default () => (
66
<>

packages/mobile-ui/src/button/style/index.less

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
@import '../../styles/themes/default.less';
33

44
.@{cls-prefix}-button {
5-
box-sizing: border-box;
5+
66
}

packages/mobile-ui/src/index.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ export { default as Button } from './button'
44
export type { BasePopupProps } from './base-popup'
55
export { default as BasePopup } from './base-popup'
66

7+
export type { BaseMaskProps } from './base-mask'
8+
export { default as BaseMask } from './base-mask'
9+
710
// Third-party library
811
export { ConditionInput } from '@template-pro/rc-ui'
912
export type { ConditionInputProps, ConditionInputCoverProps } from '@template-pro/rc-ui'

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"@template-pro/utils": ["./packages/utils/src/index"]
1818
},
1919
"types": ["node", "jest"],
20+
"typeRoots": ["./typings", "./node_modules/@types"],
2021
"allowSyntheticDefaultImports": true,
2122
"skipLibCheck": true,
2223
"declaration": false,

typings/index.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare module '*.less' {
2+
const value: {
3+
[key: string]: string
4+
}
5+
export = value
6+
}

0 commit comments

Comments
 (0)