@@ -2,7 +2,7 @@ import * as React from 'react';
2
2
// @ts -ignore
3
3
import CSSMotion from 'rc-animate/lib/CSSMotion' ;
4
4
import classNames from 'classnames' ;
5
- import List from '../src/List' ;
5
+ import List , { ScrollInfo } from '../src/List' ;
6
6
import './animate.less' ;
7
7
8
8
let uuid = 0 ;
@@ -27,58 +27,72 @@ interface Item {
27
27
28
28
interface MyItemProps extends Item {
29
29
visible : boolean ;
30
+ motionAppear : boolean ;
30
31
onClose : ( id : string ) => void ;
31
32
onLeave : ( id : string ) => void ;
33
+ onAppear : ( ...args : any [ ] ) => void ;
32
34
onInsertBefore : ( id : string ) => void ;
33
35
onInsertAfter : ( id : string ) => void ;
34
36
}
35
37
36
38
const getCurrentHeight = ( node : HTMLElement ) => ( { height : node . offsetHeight } ) ;
39
+ const getMaxHeight = ( node : HTMLElement ) => {
40
+ return { height : node . scrollHeight } ;
41
+ } ;
37
42
const getCollapsedHeight = ( ) => ( { height : 0 , opacity : 0 } ) ;
38
43
39
44
const MyItem : React . FC < MyItemProps > = (
40
- { id, uuid, visible, onClose, onLeave, onInsertBefore, onInsertAfter } ,
45
+ { id, uuid, visible, onClose, onLeave, onAppear , onInsertBefore, onInsertAfter, motionAppear } ,
41
46
ref ,
42
47
) => {
43
48
return (
44
49
< CSSMotion
45
50
visible = { visible }
46
51
ref = { ref }
47
52
motionName = "motion"
53
+ motionAppear = { motionAppear }
54
+ onAppearStart = { getCollapsedHeight }
55
+ onAppearActive = { getMaxHeight }
56
+ onAppearEnd = { onAppear }
48
57
onLeaveStart = { getCurrentHeight }
49
58
onLeaveActive = { getCollapsedHeight }
50
59
onLeaveEnd = { ( ) => {
51
60
onLeave ( id ) ;
52
61
} }
53
62
>
54
- { ( { className, style } , motionRef ) => (
55
- < div ref = { motionRef } className = { classNames ( 'item' , className ) } style = { style } data-id = { id } >
56
- < div style = { { height : uuid % 2 ? 100 : undefined } } >
57
- < button
58
- onClick = { ( ) => {
59
- onClose ( id ) ;
60
- } }
61
- >
62
- Close
63
- </ button >
64
- < button
65
- onClick = { ( ) => {
66
- onInsertBefore ( id ) ;
67
- } }
68
- >
69
- Insert Before
70
- </ button >
71
- < button
72
- onClick = { ( ) => {
73
- onInsertAfter ( id ) ;
74
- } }
75
- >
76
- Insert After
77
- </ button >
78
- { id }
63
+ { ( { className, style } , motionRef ) => {
64
+ // if (uuid >= 100) {
65
+ // console.log('=>', id, className, style);
66
+ // }
67
+ return (
68
+ < div ref = { motionRef } className = { classNames ( 'item' , className ) } style = { style } data-id = { id } >
69
+ < div style = { { height : uuid % 2 ? 100 : undefined } } >
70
+ < button
71
+ onClick = { ( ) => {
72
+ onClose ( id ) ;
73
+ } }
74
+ >
75
+ Close
76
+ </ button >
77
+ < button
78
+ onClick = { ( ) => {
79
+ onInsertBefore ( id ) ;
80
+ } }
81
+ >
82
+ Insert Before
83
+ </ button >
84
+ < button
85
+ onClick = { ( ) => {
86
+ onInsertAfter ( id ) ;
87
+ } }
88
+ >
89
+ Insert After
90
+ </ button >
91
+ { id }
92
+ </ div >
79
93
</ div >
80
- </ div >
81
- ) }
94
+ ) ;
95
+ } }
82
96
</ CSSMotion >
83
97
) ;
84
98
} ;
@@ -88,6 +102,10 @@ const ForwardMyItem = React.forwardRef(MyItem);
88
102
const Demo = ( ) => {
89
103
const [ dataSource , setDataSource ] = React . useState ( originDataSource ) ;
90
104
const [ closeMap , setCloseMap ] = React . useState < { [ id : number ] : boolean } > ( { } ) ;
105
+ const [ animating , setAnimating ] = React . useState ( false ) ;
106
+ const [ insertIndex , setInsertIndex ] = React . useState < number > ( ) ;
107
+
108
+ const listRef = React . useRef < List < Item > > ( ) ;
91
109
92
110
const onClose = ( id : string ) => {
93
111
setCloseMap ( {
@@ -101,15 +119,27 @@ const Demo = () => {
101
119
setDataSource ( newDataSource ) ;
102
120
} ;
103
121
122
+ const onAppear = ( ...args : any [ ] ) => {
123
+ setAnimating ( false ) ;
124
+ } ;
125
+
126
+ function lockForAnimation ( ) {
127
+ setAnimating ( true ) ;
128
+ }
129
+
104
130
const onInsertBefore = ( id : string ) => {
105
131
const index = dataSource . findIndex ( item => item . id === id ) ;
106
132
const newDataSource = [ ...dataSource . slice ( 0 , index ) , genItem ( ) , ...dataSource . slice ( index ) ] ;
133
+ setInsertIndex ( index ) ;
107
134
setDataSource ( newDataSource ) ;
135
+ lockForAnimation ( ) ;
108
136
} ;
109
137
const onInsertAfter = ( id : string ) => {
110
138
const index = dataSource . findIndex ( item => item . id === id ) + 1 ;
111
139
const newDataSource = [ ...dataSource . slice ( 0 , index ) , genItem ( ) , ...dataSource . slice ( index ) ] ;
140
+ setInsertIndex ( index ) ;
112
141
setDataSource ( newDataSource ) ;
142
+ lockForAnimation ( ) ;
113
143
} ;
114
144
115
145
return (
@@ -118,24 +148,27 @@ const Demo = () => {
118
148
< h2 > Animate</ h2 >
119
149
< p > Current: { dataSource . length } records</ p >
120
150
121
- < List
151
+ < List < Item >
122
152
dataSource = { dataSource }
123
153
data-id = "list"
124
154
height = { 200 }
125
155
itemHeight = { 30 }
126
156
itemKey = "id"
157
+ disabled = { animating }
158
+ ref = { listRef }
127
159
style = { {
128
- // border: '1px solid red',
129
- // boxSizing: 'border-box',
130
- boxShadow : '0 0 2px red' ,
160
+ border : '1px solid red' ,
161
+ boxSizing : 'border-box' ,
131
162
} }
132
163
>
133
- { item => (
164
+ { ( item , index ) => (
134
165
< ForwardMyItem
135
166
{ ...item }
167
+ motionAppear = { animating && insertIndex === index }
136
168
visible = { ! closeMap [ item . id ] }
137
169
onClose = { onClose }
138
170
onLeave = { onLeave }
171
+ onAppear = { onAppear }
139
172
onInsertBefore = { onInsertBefore }
140
173
onInsertAfter = { onInsertAfter }
141
174
/>
0 commit comments