11import {
22 Close as CloseIcon ,
3- CircleWarningFill ,
43 CircleSuccessFill ,
4+ CircleWarningFill ,
55 CircleCloseFill ,
66 CircleInfoFill ,
77 CircleInfo ,
88} from '@pkg/icons' ;
9+ import { mergeReactDefaultProps , getClasses } from '@pkg/shared' ;
910import { CollapseTransition } from '~/collapse-transition' ;
10- import type { RequiredPart } from '@tool-pack/types' ;
1111import { getClassNames } from '@tool-pack/basic' ;
1212import type { AlertProps } from './alert.types' ;
13- import { getClasses } from '@pkg/shared' ;
1413import React , { useState } from 'react' ;
1514import { Button } from '~/button' ;
1615import { Icon } from '~/icon' ;
@@ -34,58 +33,57 @@ const Icons: Record<Required<AlertProps>['type'], React.FC> = {
3433 primary : CircleInfo ,
3534} ;
3635
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 ) ;
4340
44- const [ visible , setVisible ] = useState ( true ) ;
45- const DefaultIcon = Icons [ type ] ;
41+ const [ visible , setVisible ] = useState ( true ) ;
42+ const DefaultIcon = Icons [ type ] ;
4643
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+ ) }
6373 </ 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+ ) ;
7875
79- if ( ! closable ) return Body ;
80- return < CollapseTransition > { visible && Body } </ CollapseTransition > ;
76+ if ( ! closable ) return Body ;
77+ return < CollapseTransition > { visible && Body } </ CollapseTransition > ;
8178
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+ ) ;
8987
90- Alert . defaultProps = defaultProps ;
9188Alert . displayName = 'Alert' ;
89+ Alert . defaultProps = defaultProps ;
0 commit comments