11import React , { Component , ReactText } from 'react' ;
22import ReactDOM from 'react-dom' ;
3- import Animate from 'rc-animate' ;
3+ import classNames from 'classnames' ;
4+ import { CSSMotionList } from 'rc-motion' ;
45import createChainedFunction from 'rc-util/lib/createChainedFunction' ;
5- import classnames from 'classnames' ;
66import Notice , { NoticeProps } from './Notice' ;
77import useNotification from './useNotification' ;
88
@@ -113,17 +113,31 @@ class Notification extends Component<NotificationProps, NotificationState> {
113113 } ;
114114
115115 remove = ( key : React . Key ) => {
116- this . setState ( previousState => ( {
117- notices : previousState . notices . filter ( ( { notice } ) => notice . key !== key ) ,
116+ this . setState ( ( { notices } ) => ( {
117+ notices : notices . filter ( ( { notice } ) => notice . key !== key ) ,
118118 } ) ) ;
119119 } ;
120120
121+ noticePropsMap : Record <
122+ React . Key ,
123+ {
124+ props : NoticeProps & {
125+ key : ReactText ;
126+ } ;
127+ holderCallback ?: HolderReadyCallback ;
128+ }
129+ > = { } ;
130+
121131 render ( ) {
122132 const { notices } = this . state ;
123133 const { prefixCls, className, closeIcon, style } = this . props ;
124- const noticeNodes = notices . map ( ( { notice, holderCallback } , index ) => {
134+
135+ const noticeKeys : React . Key [ ] = [ ] ;
136+
137+ notices . forEach ( ( { notice, holderCallback } , index ) => {
125138 const update = Boolean ( index === notices . length - 1 && notice . updateKey ) ;
126139 const key = notice . updateKey ? notice . updateKey : notice . key ;
140+
127141 const onClose = createChainedFunction (
128142 this . remove . bind ( this , notice . key ! ) ,
129143 notice . onClose ,
@@ -139,33 +153,55 @@ class Notification extends Component<NotificationProps, NotificationState> {
139153 onClose,
140154 onClick : notice . onClick ,
141155 children : notice . content ,
142- } as ( NoticeProps & { key : ReactText } ) ;
143-
144- if ( holderCallback ) {
145- return (
146- < div
147- key = { key }
148- className = { `${ prefixCls } -hook-holder` }
149- ref = { div => {
150- if ( typeof key === 'undefined' ) {
151- return ;
152- }
153- if ( div ) {
154- this . hookRefs . set ( key , div ) ;
155- holderCallback ( div , noticeProps ) ;
156- } else {
157- this . hookRefs . delete ( key ) ;
158- }
159- } }
160- />
161- ) ;
162- }
156+ } as NoticeProps & { key : ReactText } ;
163157
164- return < Notice { ...noticeProps } /> ;
158+ // Give to motion
159+ noticeKeys . push ( key ) ;
160+ this . noticePropsMap [ key ] = { props : noticeProps , holderCallback } ;
165161 } ) ;
162+
166163 return (
167- < div className = { classnames ( prefixCls , className ) } style = { style } >
168- < Animate transitionName = { this . getTransitionName ( ) } > { noticeNodes } </ Animate >
164+ < div className = { classNames ( prefixCls , className ) } style = { style } >
165+ < CSSMotionList
166+ keys = { noticeKeys }
167+ motionName = { this . getTransitionName ( ) }
168+ onLeaveEnd = { ( _ , __ , { key } ) => {
169+ delete this . noticePropsMap [ key ] ;
170+ } }
171+ >
172+ { ( { key, className : motionClassName , style : motionStyle } ) => {
173+ const { props : noticeProps , holderCallback } = this . noticePropsMap [ key ] ;
174+ if ( holderCallback ) {
175+ return (
176+ < div
177+ key = { key }
178+ className = { classNames ( motionClassName , `${ prefixCls } -hook-holder` ) }
179+ style = { { ...motionStyle } }
180+ ref = { div => {
181+ if ( typeof key === 'undefined' ) {
182+ return ;
183+ }
184+
185+ if ( div ) {
186+ this . hookRefs . set ( key , div ) ;
187+ holderCallback ( div , noticeProps ) ;
188+ } else {
189+ this . hookRefs . delete ( key ) ;
190+ }
191+ } }
192+ />
193+ ) ;
194+ }
195+
196+ return (
197+ < Notice
198+ { ...noticeProps }
199+ className = { classNames ( motionClassName , noticeProps ?. className ) }
200+ style = { { ...motionStyle , ...noticeProps ?. style } }
201+ />
202+ ) ;
203+ } }
204+ </ CSSMotionList >
169205 </ div >
170206 ) ;
171207 }
0 commit comments