@@ -4,6 +4,7 @@ import Button, { ButtonType, ButtonProps } from '@uiw/react-button';
4
4
import Icon , { IconProps } from '@uiw/react-icon' ;
5
5
import { IProps , noop } from '@uiw/utils' ;
6
6
import './style/index.less' ;
7
+ import CallShow from './CallShow' ;
7
8
8
9
export interface ModalProps extends IProps , OverlayProps {
9
10
type ?: ButtonType ;
@@ -27,105 +28,113 @@ export interface ModalProps extends IProps, OverlayProps {
27
28
onConfirm ?: ( evn : React . MouseEvent < HTMLButtonElement > & MouseEvent ) => void ;
28
29
}
29
30
30
- export default React . forwardRef < OverlayProps , ModalProps > ( ( props , ref ) => {
31
- const {
32
- prefixCls = 'w-modal' ,
33
- className,
34
- children,
35
- useButton = true ,
36
- usePortal = true ,
37
- autoFocus = false ,
38
- isOpen : _ = false ,
39
- title,
40
- cancelText,
41
- cancelButtonProps,
42
- confirmButtonProps,
43
- content,
44
- confirmText = '确认' ,
45
- type = 'light' ,
46
- icon,
47
- maxWidth = 500 ,
48
- minWidth = 320 ,
49
- width,
50
- isCloseButtonShown = true ,
51
- onCancel = noop ,
52
- onConfirm = noop ,
53
- bodyStyle,
54
- ...other
55
- } = props ;
56
- const [ isOpen , setIsOpen ] = useState ( props . isOpen ) ;
57
- useEffect ( ( ) => {
58
- if ( props . isOpen !== isOpen ) {
59
- setIsOpen ( props . isOpen ) ;
60
- }
61
- } , [ props . isOpen ] ) ;
31
+ type ShowModalProps = {
32
+ show ?: ( props : Omit < ModalProps , 'onClosed' | 'isOpen' > & { children : React . ReactNode } ) => void ;
33
+ } ;
34
+
35
+ const Modal : React . ForwardRefExoticComponent < ModalProps & React . RefAttributes < OverlayProps > > & ShowModalProps =
36
+ React . forwardRef < OverlayProps , ModalProps > ( ( props , ref ) => {
37
+ const {
38
+ prefixCls = 'w-modal' ,
39
+ className,
40
+ children,
41
+ useButton = true ,
42
+ usePortal = true ,
43
+ autoFocus = false ,
44
+ isOpen : _ = false ,
45
+ title,
46
+ cancelText,
47
+ cancelButtonProps,
48
+ confirmButtonProps,
49
+ content,
50
+ confirmText = '确认' ,
51
+ type = 'light' ,
52
+ icon,
53
+ maxWidth = 500 ,
54
+ minWidth = 320 ,
55
+ width,
56
+ isCloseButtonShown = true ,
57
+ onCancel = noop ,
58
+ onConfirm = noop ,
59
+ bodyStyle,
60
+ ...other
61
+ } = props ;
62
+ const [ isOpen , setIsOpen ] = useState ( props . isOpen ) ;
63
+ useEffect ( ( ) => {
64
+ if ( props . isOpen !== isOpen ) {
65
+ setIsOpen ( props . isOpen ) ;
66
+ }
67
+ } , [ props . isOpen ] ) ;
62
68
63
- const [ loading , setLoading ] = useState ( false ) ;
64
- const cls = [ prefixCls , className , type ? `${ type } ` : null ] . filter ( Boolean ) . join ( ' ' ) . trim ( ) ;
65
- function onClose ( ) {
66
- setIsOpen ( false ) ;
67
- }
68
- async function handleCancel ( e : React . MouseEvent < HTMLButtonElement , MouseEvent > & MouseEvent ) {
69
- setLoading ( true ) ;
70
- try {
71
- onCancel && ( await onCancel ( e ) ) ;
72
- } catch ( e ) { }
73
- setIsOpen ( false ) ;
74
- setLoading ( false ) ;
75
- }
76
- async function handleConfirm ( e : React . MouseEvent < HTMLButtonElement , MouseEvent > & MouseEvent ) {
77
- setLoading ( true ) ;
78
- try {
79
- onConfirm && ( await onConfirm ( e ) ) ;
80
- } catch ( e ) { }
81
- setIsOpen ( false ) ;
82
- setLoading ( false ) ;
83
- }
84
- return (
85
- < Overlay usePortal = { usePortal } isOpen = { isOpen } { ...other } onClose = { onClose } className = { cls } >
86
- < div className = { `${ prefixCls } -container` } >
87
- < div
88
- className = { [
89
- `${ prefixCls } -inner` ,
90
- title ? `${ prefixCls } -shown-title` : null ,
91
- icon ? `${ prefixCls } -shown-icon` : null ,
92
- ]
93
- . filter ( Boolean )
94
- . join ( ' ' )
95
- . trim ( ) }
96
- style = { { maxWidth, minWidth, width } }
97
- >
98
- { ( title || icon ) && (
99
- < div className = { `${ prefixCls } -header` } >
100
- { icon && < Icon type = { icon } /> }
101
- { title && < h4 > { title } </ h4 > }
102
- { isCloseButtonShown && < Button basic onClick = { ( e ) => handleCancel ( e ) } icon = "close" type = "light" /> }
69
+ const [ loading , setLoading ] = useState ( false ) ;
70
+ const cls = [ prefixCls , className , type ? `${ type } ` : null ] . filter ( Boolean ) . join ( ' ' ) . trim ( ) ;
71
+ function onClose ( ) {
72
+ setIsOpen ( false ) ;
73
+ }
74
+ async function handleCancel ( e : React . MouseEvent < HTMLButtonElement , MouseEvent > & MouseEvent ) {
75
+ setLoading ( true ) ;
76
+ try {
77
+ onCancel && ( await onCancel ( e ) ) ;
78
+ } catch ( e ) { }
79
+ setIsOpen ( false ) ;
80
+ setLoading ( false ) ;
81
+ }
82
+ async function handleConfirm ( e : React . MouseEvent < HTMLButtonElement , MouseEvent > & MouseEvent ) {
83
+ setLoading ( true ) ;
84
+ try {
85
+ onConfirm && ( await onConfirm ( e ) ) ;
86
+ } catch ( e ) { }
87
+ setIsOpen ( false ) ;
88
+ setLoading ( false ) ;
89
+ }
90
+ return (
91
+ < Overlay usePortal = { usePortal } isOpen = { isOpen } { ...other } onClose = { onClose } className = { cls } >
92
+ < div className = { `${ prefixCls } -container` } >
93
+ < div
94
+ className = { [
95
+ `${ prefixCls } -inner` ,
96
+ title ? `${ prefixCls } -shown-title` : null ,
97
+ icon ? `${ prefixCls } -shown-icon` : null ,
98
+ ]
99
+ . filter ( Boolean )
100
+ . join ( ' ' )
101
+ . trim ( ) }
102
+ style = { { maxWidth, minWidth, width } }
103
+ >
104
+ { ( title || icon ) && (
105
+ < div className = { `${ prefixCls } -header` } >
106
+ { icon && < Icon type = { icon } /> }
107
+ { title && < h4 > { title } </ h4 > }
108
+ { isCloseButtonShown && < Button basic onClick = { ( e ) => handleCancel ( e ) } icon = "close" type = "light" /> }
109
+ </ div >
110
+ ) }
111
+ < div className = { `${ prefixCls } -body` } style = { bodyStyle } >
112
+ { children || content }
103
113
</ div >
104
- ) }
105
- < div className = { `${ prefixCls } -body` } style = { bodyStyle } >
106
- { children || content }
107
- </ div >
108
- { useButton && (
109
- < div className = { `${ prefixCls } -footer` } >
110
- < Button
111
- autoFocus = { autoFocus }
112
- type = { type }
113
- loading = { loading }
114
- disabled = { loading }
115
- { ...confirmButtonProps }
116
- onClick = { ( e ) => handleConfirm ( e ) }
117
- >
118
- { confirmText }
119
- </ Button >
120
- { cancelText && (
121
- < Button { ...cancelButtonProps } onClick = { ( e ) => handleCancel ( e ) } >
122
- { cancelText }
114
+ { useButton && (
115
+ < div className = { `${ prefixCls } -footer` } >
116
+ < Button
117
+ autoFocus = { autoFocus }
118
+ type = { type }
119
+ loading = { loading }
120
+ disabled = { loading }
121
+ { ...confirmButtonProps }
122
+ onClick = { ( e ) => handleConfirm ( e ) }
123
+ >
124
+ { confirmText }
123
125
</ Button >
124
- ) }
125
- </ div >
126
- ) }
126
+ { cancelText && (
127
+ < Button { ...cancelButtonProps } onClick = { ( e ) => handleCancel ( e ) } >
128
+ { cancelText }
129
+ </ Button >
130
+ ) }
131
+ </ div >
132
+ ) }
133
+ </ div >
127
134
</ div >
128
- </ div >
129
- </ Overlay >
130
- ) ;
131
- } ) ;
135
+ </ Overlay >
136
+ ) ;
137
+ } ) ;
138
+
139
+ Modal . show = CallShow ;
140
+ export default Modal ;
0 commit comments