1
1
import {
2
2
Close as CloseIcon ,
3
- CircleWarningFill ,
4
3
CircleSuccessFill ,
4
+ CircleWarningFill ,
5
5
CircleCloseFill ,
6
6
CircleInfoFill ,
7
7
CircleInfo ,
8
8
} from '@pkg/icons' ;
9
+ import { mergeReactDefaultProps , getClasses } from '@pkg/shared' ;
9
10
import { CollapseTransition } from '~/collapse-transition' ;
10
- import type { RequiredPart } from '@tool-pack/types' ;
11
11
import { getClassNames } from '@tool-pack/basic' ;
12
12
import type { AlertProps } from './alert.types' ;
13
- import { getClasses } from '@pkg/shared' ;
14
13
import React , { useState } from 'react' ;
15
14
import { Button } from '~/button' ;
16
15
import { Icon } from '~/icon' ;
@@ -34,58 +33,57 @@ const Icons: Record<Required<AlertProps>['type'], React.FC> = {
34
33
primary : CircleInfo ,
35
34
} ;
36
35
37
- export const Alert : React . FC < AlertProps > = React . forwardRef <
38
- HTMLDivElement ,
39
- AlertProps
40
- > ( ( props , ref ) => {
41
- const { bordered, closable, children, onClose, attrs, title, icon, type } =
42
- props as RequiredPart < AlertProps , keyof typeof defaultProps > ;
36
+ export const Alert = React . forwardRef < HTMLDivElement , AlertProps > (
37
+ ( props , ref ) => {
38
+ const { bordered, closable, children, onClose, attrs, title, icon, type } =
39
+ mergeReactDefaultProps ( props , defaultProps ) ;
43
40
44
- const [ visible , setVisible ] = useState ( true ) ;
45
- const DefaultIcon = Icons [ type ] ;
41
+ const [ visible , setVisible ] = useState ( true ) ;
42
+ const DefaultIcon = Icons [ type ] ;
46
43
47
- const Body = (
48
- < div
49
- { ...attrs }
50
- className = { getClassNames ( cls . root , attrs ?. className , {
51
- [ `${ cls . root } --${ type } ` ] : type !== 'primary' ,
52
- [ cls [ '--' ] . centered ] : title && ! children ,
53
- [ cls [ '--' ] . bordered ] : bordered ,
54
- } ) }
55
- ref = { ref }
56
- >
57
- { icon !== null && (
58
- < Icon className = { cls . __ . icon } > { icon || < DefaultIcon /> } </ Icon >
59
- ) }
60
- < div className = { cls . __ . content } >
61
- { title && < div className = { cls . __ . title } > { title } </ div > }
62
- { children && < div className = { cls . __ . desc } > { children } </ div > }
44
+ const Body = (
45
+ < div
46
+ { ...attrs }
47
+ className = { getClassNames ( cls . root , attrs ?. className , {
48
+ [ `${ cls . root } --${ type } ` ] : type !== 'primary' ,
49
+ [ cls [ '--' ] . centered ] : title && ! children ,
50
+ [ cls [ '--' ] . bordered ] : bordered ,
51
+ } ) }
52
+ ref = { ref }
53
+ >
54
+ { icon !== null && (
55
+ < Icon className = { cls . __ . icon } > { icon || < DefaultIcon /> } </ Icon >
56
+ ) }
57
+ < div className = { cls . __ . content } >
58
+ { title && < div className = { cls . __ . title } > { title } </ div > }
59
+ { children && < div className = { cls . __ . desc } > { children } </ div > }
60
+ </ div >
61
+ { closable && (
62
+ < Button
63
+ className = { cls . __ [ 'close-btn' ] }
64
+ onClick = { close }
65
+ size = "small"
66
+ plain = "text"
67
+ >
68
+ < Icon className = { cls . __ [ 'close-icon' ] } >
69
+ < CloseIcon />
70
+ </ Icon >
71
+ </ Button >
72
+ ) }
63
73
</ div >
64
- { closable && (
65
- < Button
66
- className = { cls . __ [ 'close-btn' ] }
67
- onClick = { close }
68
- size = "small"
69
- plain = "text"
70
- >
71
- < Icon className = { cls . __ [ 'close-icon' ] } >
72
- < CloseIcon />
73
- </ Icon >
74
- </ Button >
75
- ) }
76
- </ div >
77
- ) ;
74
+ ) ;
78
75
79
- if ( ! closable ) return Body ;
80
- return < CollapseTransition > { visible && Body } </ CollapseTransition > ;
76
+ if ( ! closable ) return Body ;
77
+ return < CollapseTransition > { visible && Body } </ CollapseTransition > ;
81
78
82
- function close ( e : React . MouseEvent < HTMLButtonElement > ) {
83
- if ( ! closable ) return ;
84
- onClose ?.( e ) ;
85
- if ( e . isDefaultPrevented ( ) ) return ;
86
- setVisible ( false ) ;
87
- }
88
- } ) ;
79
+ function close ( e : React . MouseEvent < HTMLButtonElement > ) {
80
+ if ( ! closable ) return ;
81
+ onClose ?.( e ) ;
82
+ if ( e . isDefaultPrevented ( ) ) return ;
83
+ setVisible ( false ) ;
84
+ }
85
+ } ,
86
+ ) ;
89
87
90
- Alert . defaultProps = defaultProps ;
91
88
Alert . displayName = 'Alert' ;
89
+ Alert . defaultProps = defaultProps ;
0 commit comments