@@ -44,6 +44,8 @@ export interface CSSMotionListProps
4444
4545 /** This will always trigger after final visible changed. Even if no motion configured. */
4646 onVisibleChanged ?: ( visible : boolean , info : { key : React . Key } ) => void ;
47+ /** All motion leaves in the screen */
48+ onAllRemoved ?: ( ) => void ;
4749}
4850
4951export interface CSSMotionListState {
@@ -95,16 +97,23 @@ export function genCSSMotionList(
9597 } ;
9698 }
9799
100+ // ZombieJ: Return the count of rest keys. It's safe to refactor if need more info.
98101 removeKey = ( removeKey : React . Key ) => {
99- this . setState ( ( { keyEntities } ) => ( {
100- keyEntities : keyEntities . map ( entity => {
101- if ( entity . key !== removeKey ) return entity ;
102- return {
103- ...entity ,
104- status : STATUS_REMOVED ,
105- } ;
106- } ) ,
107- } ) ) ;
102+ const { keyEntities } = this . state ;
103+ const nextKeyEntities = keyEntities . map ( entity => {
104+ if ( entity . key !== removeKey ) return entity ;
105+ return {
106+ ...entity ,
107+ status : STATUS_REMOVED ,
108+ } ;
109+ } ) ;
110+
111+ this . setState ( {
112+ keyEntities : nextKeyEntities ,
113+ } ) ;
114+
115+ return nextKeyEntities . filter ( ( { status } ) => status !== STATUS_REMOVED )
116+ . length ;
108117 } ;
109118
110119 render ( ) {
@@ -113,6 +122,7 @@ export function genCSSMotionList(
113122 component,
114123 children,
115124 onVisibleChanged,
125+ onAllRemoved,
116126 ...restProps
117127 } = this . props ;
118128
@@ -139,7 +149,11 @@ export function genCSSMotionList(
139149 onVisibleChanged ?.( changedVisible , { key : eventProps . key } ) ;
140150
141151 if ( ! changedVisible ) {
142- this . removeKey ( eventProps . key ) ;
152+ const restKeysCount = this . removeKey ( eventProps . key ) ;
153+
154+ if ( restKeysCount === 0 && onAllRemoved ) {
155+ onAllRemoved ( ) ;
156+ }
143157 }
144158 } }
145159 >
0 commit comments