|
|
@@ -1,7 +1,9 @@
|
|
|
import { message } from 'antd'
|
|
|
import type { CSSProperties } from 'react'
|
|
|
+import { useState } from 'react'
|
|
|
import React from 'react'
|
|
|
import { ModalContext } from './context'
|
|
|
+import { useDispatch } from 'umi'
|
|
|
export interface ModalFullConfig<T = any> {
|
|
|
destroyOnClose?: boolean | string
|
|
|
visiblePropName?: string
|
|
|
@@ -26,17 +28,14 @@ export interface ModalStoreState {
|
|
|
currentModal: ModalItem[]
|
|
|
}
|
|
|
|
|
|
-class ModalStore extends React.Component<ModalStoreProps, ModalStoreState> {
|
|
|
- constructor(props: ModalStoreProps) {
|
|
|
- super(props)
|
|
|
- this.state = {
|
|
|
- currentModal: []
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+const ModalStore: React.FC<ModalStoreProps> = props => {
|
|
|
+ const dispatch = useDispatch()
|
|
|
+ const [state, setState] = useState<ModalStoreState>({
|
|
|
+ currentModal: []
|
|
|
+ })
|
|
|
// 获取modal的config
|
|
|
- private getModalConfig(key: string) {
|
|
|
- const { modalMap = {}, visiblePropName = 'visible' } = this.props
|
|
|
+ function getModalConfig(key: string) {
|
|
|
+ const { modalMap = {}, visiblePropName = 'visible' } = props
|
|
|
const componentKey = key.split('@')?.[0]
|
|
|
let config = modalMap[componentKey]
|
|
|
if (typeof config === 'function') config = { component: config }
|
|
|
@@ -50,14 +49,14 @@ class ModalStore extends React.Component<ModalStoreProps, ModalStoreState> {
|
|
|
return { visiblePropName, onClosePropName, destroyOnClose, config }
|
|
|
}
|
|
|
|
|
|
- private setModalState(fn: (prev: ModalItem[]) => ModalItem[]) {
|
|
|
- this.setState(prev => ({ currentModal: fn(prev.currentModal) }))
|
|
|
+ function setModalState(fn: (prev: ModalItem[]) => ModalItem[]) {
|
|
|
+ setState(prev => ({ currentModal: fn(prev.currentModal) }))
|
|
|
}
|
|
|
|
|
|
- private getCloseFunction(key: string, cb?: (...args: any[]) => void) {
|
|
|
+ function getCloseFunction(key: string, cb?: (...args: any[]) => void) {
|
|
|
return (...args: any[]) => {
|
|
|
- const { visiblePropName } = this.getModalConfig(key)
|
|
|
- this.setModalState(prev => {
|
|
|
+ const { visiblePropName } = getModalConfig(key)
|
|
|
+ setModalState(prev => {
|
|
|
return prev.map(item => {
|
|
|
if (item.key === key) {
|
|
|
return { ...item, [visiblePropName]: false }
|
|
|
@@ -71,37 +70,43 @@ class ModalStore extends React.Component<ModalStoreProps, ModalStoreState> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private getDestroyFunction(key: string, cb?: (...args: any[]) => void) {
|
|
|
+ function getDestroyFunction(key: string, cb?: (...args: any[]) => void) {
|
|
|
return (...args: any[]) => {
|
|
|
- this.setModalState(prev => prev.filter(v => v.key !== key))
|
|
|
+ setModalState(prev => prev.filter(v => v.key !== key))
|
|
|
if (typeof cb === 'function') {
|
|
|
cb(...args)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private push = (key: string, state: any) => {
|
|
|
+ function push(key: string, compState: any) {
|
|
|
+ console.log(compState)
|
|
|
+
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
- key = state?.dataId ? `${key}@${state?.dataId}` : key
|
|
|
- const { visiblePropName, onClosePropName, destroyOnClose } = this.getModalConfig(key)
|
|
|
+ key = compState?.dataId ? `${key}@${compState?.dataId}` : key
|
|
|
+ const { visiblePropName, onClosePropName, destroyOnClose } = getModalConfig(key)
|
|
|
|
|
|
- this.setModalState(prevModals => {
|
|
|
+ setModalState(prevModals => {
|
|
|
const defaultProps: any = {
|
|
|
[visiblePropName]: key.startsWith('D') ? false : true,
|
|
|
- [onClosePropName]: this.getCloseFunction(key, state?.[onClosePropName])
|
|
|
+ [onClosePropName]: getCloseFunction(key, compState?.[onClosePropName])
|
|
|
}
|
|
|
-
|
|
|
+ compState?.dataId &&
|
|
|
+ dispatch({
|
|
|
+ type: 'refresh/commitAction',
|
|
|
+ payload: compState.dataId
|
|
|
+ })
|
|
|
if (destroyOnClose) {
|
|
|
const prop = typeof destroyOnClose === 'string' ? destroyOnClose : onClosePropName
|
|
|
|
|
|
defaultProps[prop] =
|
|
|
destroyOnClose === 'afterVisibleChange'
|
|
|
- ? (visible: boolean) => !visible && this.getDestroyFunction(key)()
|
|
|
- : this.getDestroyFunction(key)
|
|
|
+ ? (visible: boolean) => !visible && getDestroyFunction(key)()
|
|
|
+ : getDestroyFunction(key)
|
|
|
}
|
|
|
|
|
|
const newModal = {
|
|
|
- ...state,
|
|
|
+ ...compState,
|
|
|
...defaultProps,
|
|
|
key
|
|
|
}
|
|
|
@@ -125,7 +130,7 @@ class ModalStore extends React.Component<ModalStoreProps, ModalStoreState> {
|
|
|
if (key.startsWith('D')) {
|
|
|
// 利用事件循环,先让dom元素渲染出来再使其展示动画(否则可能导致抽屉无动画直接出现)
|
|
|
setTimeout(() => {
|
|
|
- this.setModalState(prevModals =>
|
|
|
+ setModalState(prevModals =>
|
|
|
prevModals.map(item => {
|
|
|
if (item.key === key) {
|
|
|
return { ...item, visible: true }
|
|
|
@@ -137,9 +142,9 @@ class ModalStore extends React.Component<ModalStoreProps, ModalStoreState> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private renderModal = (item: ModalItem) => {
|
|
|
- const { config } = this.getModalConfig(item.key)
|
|
|
- const nodeProps = this.renderMaskProps(item)
|
|
|
+ function renderModal(item: ModalItem) {
|
|
|
+ const { config } = getModalConfig(item.key)
|
|
|
+ const nodeProps = renderMaskProps(item)
|
|
|
if (config) {
|
|
|
// 弹窗
|
|
|
if ('component' in config) {
|
|
|
@@ -151,12 +156,10 @@ class ModalStore extends React.Component<ModalStoreProps, ModalStoreState> {
|
|
|
return null
|
|
|
}
|
|
|
|
|
|
- private renderMaskProps = (item: ModalItem) => {
|
|
|
+ function renderMaskProps(item: ModalItem) {
|
|
|
// 没有dataId 不是抽屉
|
|
|
if (!item.dataId) return item
|
|
|
- const drawerStore = this.state.currentModal
|
|
|
- .filter(modal => modal.dataId)
|
|
|
- .map(modal => modal.dataId)
|
|
|
+ const drawerStore = state.currentModal.filter(modal => modal.dataId).map(modal => modal.dataId)
|
|
|
const len = drawerStore.length
|
|
|
if (len === 1) return item
|
|
|
// 抽屉至少2个
|
|
|
@@ -174,17 +177,15 @@ class ModalStore extends React.Component<ModalStoreProps, ModalStoreState> {
|
|
|
}
|
|
|
return item
|
|
|
}
|
|
|
- render() {
|
|
|
- const { currentModal } = this.state
|
|
|
- const { children } = this.props
|
|
|
-
|
|
|
- return (
|
|
|
- <ModalContext.Provider value={this.push}>
|
|
|
- {children}
|
|
|
- {currentModal.map(this.renderModal)}
|
|
|
- </ModalContext.Provider>
|
|
|
- )
|
|
|
- }
|
|
|
+ const { currentModal } = state
|
|
|
+ const { children } = props
|
|
|
+
|
|
|
+ return (
|
|
|
+ <ModalContext.Provider value={push}>
|
|
|
+ {children}
|
|
|
+ {currentModal.map(renderModal)}
|
|
|
+ </ModalContext.Provider>
|
|
|
+ )
|
|
|
}
|
|
|
|
|
|
export default ModalStore
|