@ -9,6 +9,7 @@ import pickAttrs from 'rc-util/lib/pickAttrs';
import type { ReactElement } from 'react' ;
import * as React from 'react' ;
import { replaceElement } from '../_util/reactNode' ;
import warning from '../_util/warning' ;
import { ConfigContext } from '../config-provider' ;
import ErrorBoundary from './ErrorBoundary' ;
@ -20,7 +21,10 @@ export interface AlertProps {
type ? : 'success' | 'info' | 'warning' | 'error' ;
/** Whether Alert can be closed */
closable? : boolean ;
/** Close text to show */
/ * *
* @deprecated please use ` closeIcon ` instead .
* Close text to show
* /
closeText? : React.ReactNode ;
/** Content of Alert */
message? : React.ReactNode ;
@ -41,7 +45,7 @@ export interface AlertProps {
banner? : boolean ;
icon? : React.ReactNode ;
/** Custom closeIcon */
closeIcon? : React.ReactNode ;
closeIcon? : boolean | React . ReactNode ;
action? : React.ReactNode ;
onMouseEnter? : React.MouseEventHandler < HTMLDivElement > ;
onMouseLeave? : React.MouseEventHandler < HTMLDivElement > ;
@ -78,16 +82,17 @@ const IconNode: React.FC<IconNodeProps> = (props) => {
interface CloseIconProps {
isClosable : boolean ;
prefixCls : AlertProps [ 'prefixCls' ] ;
closeText : AlertProps [ 'closeText' ] ;
closeIcon : AlertProps [ 'closeIcon' ] ;
handleClose : AlertProps [ 'onClose' ] ;
}
const CloseIcon : React.FC < CloseIconProps > = ( props ) = > {
const { isClosable , closeText , prefixCls , closeIcon , handleClose } = props ;
const { isClosable , prefixCls , closeIcon , handleClose } = props ;
const mergedCloseIcon =
closeIcon === true || closeIcon === undefined ? < CloseOutlined / > : closeIcon ;
return isClosable ? (
< button type = "button" onClick = { handleClose } className = { ` ${ prefixCls } -close-icon ` } tabIndex = { 0 } >
{ closeText ? < span className = { ` ${ prefixCls } -close-text ` } > { closeText } < / span > : c loseIcon}
{ mergedC loseIcon}
< / button >
) : null ;
} ;
@ -111,12 +116,14 @@ const Alert: CompoundedComponent = ({
showIcon ,
closable ,
closeText ,
closeIcon = < CloseOutlined / > ,
closeIcon ,
action ,
. . . props
} ) = > {
const [ closed , setClosed ] = React . useState ( false ) ;
if ( process . env . NODE_ENV !== 'production' ) {
warning ( ! closeText , 'Alert' , '`closeText` is deprecated. Please use `closeIcon` instead.' ) ;
}
const ref = React . useRef < HTMLDivElement > ( null ) ;
const { getPrefixCls , direction } = React . useContext ( ConfigContext ) ;
const prefixCls = getPrefixCls ( 'alert' , customizePrefixCls ) ;
@ -136,8 +143,18 @@ const Alert: CompoundedComponent = ({
return banner ? 'warning' : 'info' ;
} ;
// closeable when closeText is assigned
const isClosable = closeText ? true : closable ;
// closeable when closeText or closeIcon is assigned
const isClosable = React . useMemo ( ( ) = > {
if ( closeText ) {
return true ;
}
if ( typeof closable === 'boolean' ) {
return closable ;
}
// should be true when closeIcon is 0 or ''
return closeIcon !== false && closeIcon !== null && closeIcon !== undefined ;
} , [ closeText , closeIcon , closable ] ) ;
const type = getType ( ) ;
// banner mode defaults to Icon
@ -199,10 +216,9 @@ const Alert: CompoundedComponent = ({
< / div >
{ action ? < div className = { ` ${ prefixCls } -action ` } > { action } < / div > : null }
< CloseIcon
isClosable = { ! ! isClosable }
closeText = { closeText }
isClosable = { isClosable }
prefixCls = { prefixCls }
closeIcon = { closeIcon }
closeIcon = { closeText || close Icon }
handleClose = { handleClose }
/ >
< / div >