Skip to content

Commit a5078cb

Browse files
committed
support insert
1 parent 4c0f4cd commit a5078cb

File tree

3 files changed

+206
-117
lines changed

3 files changed

+206
-117
lines changed

examples/animate.tsx

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22
// @ts-ignore
33
import CSSMotion from 'rc-animate/lib/CSSMotion';
44
import classNames from 'classnames';
5-
import List from '../src/List';
5+
import List, { ScrollInfo } from '../src/List';
66
import './animate.less';
77

88
let uuid = 0;
@@ -27,58 +27,72 @@ interface Item {
2727

2828
interface MyItemProps extends Item {
2929
visible: boolean;
30+
motionAppear: boolean;
3031
onClose: (id: string) => void;
3132
onLeave: (id: string) => void;
33+
onAppear: (...args: any[]) => void;
3234
onInsertBefore: (id: string) => void;
3335
onInsertAfter: (id: string) => void;
3436
}
3537

3638
const getCurrentHeight = (node: HTMLElement) => ({ height: node.offsetHeight });
39+
const getMaxHeight = (node: HTMLElement) => {
40+
return { height: node.scrollHeight };
41+
};
3742
const getCollapsedHeight = () => ({ height: 0, opacity: 0 });
3843

3944
const MyItem: React.FC<MyItemProps> = (
40-
{ id, uuid, visible, onClose, onLeave, onInsertBefore, onInsertAfter },
45+
{ id, uuid, visible, onClose, onLeave, onAppear, onInsertBefore, onInsertAfter, motionAppear },
4146
ref,
4247
) => {
4348
return (
4449
<CSSMotion
4550
visible={visible}
4651
ref={ref}
4752
motionName="motion"
53+
motionAppear={motionAppear}
54+
onAppearStart={getCollapsedHeight}
55+
onAppearActive={getMaxHeight}
56+
onAppearEnd={onAppear}
4857
onLeaveStart={getCurrentHeight}
4958
onLeaveActive={getCollapsedHeight}
5059
onLeaveEnd={() => {
5160
onLeave(id);
5261
}}
5362
>
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>
7993
</div>
80-
</div>
81-
)}
94+
);
95+
}}
8296
</CSSMotion>
8397
);
8498
};
@@ -88,6 +102,10 @@ const ForwardMyItem = React.forwardRef(MyItem);
88102
const Demo = () => {
89103
const [dataSource, setDataSource] = React.useState(originDataSource);
90104
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>>();
91109

92110
const onClose = (id: string) => {
93111
setCloseMap({
@@ -101,15 +119,27 @@ const Demo = () => {
101119
setDataSource(newDataSource);
102120
};
103121

122+
const onAppear = (...args: any[]) => {
123+
setAnimating(false);
124+
};
125+
126+
function lockForAnimation() {
127+
setAnimating(true);
128+
}
129+
104130
const onInsertBefore = (id: string) => {
105131
const index = dataSource.findIndex(item => item.id === id);
106132
const newDataSource = [...dataSource.slice(0, index), genItem(), ...dataSource.slice(index)];
133+
setInsertIndex(index);
107134
setDataSource(newDataSource);
135+
lockForAnimation();
108136
};
109137
const onInsertAfter = (id: string) => {
110138
const index = dataSource.findIndex(item => item.id === id) + 1;
111139
const newDataSource = [...dataSource.slice(0, index), genItem(), ...dataSource.slice(index)];
140+
setInsertIndex(index);
112141
setDataSource(newDataSource);
142+
lockForAnimation();
113143
};
114144

115145
return (
@@ -118,24 +148,27 @@ const Demo = () => {
118148
<h2>Animate</h2>
119149
<p>Current: {dataSource.length} records</p>
120150

121-
<List
151+
<List<Item>
122152
dataSource={dataSource}
123153
data-id="list"
124154
height={200}
125155
itemHeight={30}
126156
itemKey="id"
157+
disabled={animating}
158+
ref={listRef}
127159
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',
131162
}}
132163
>
133-
{item => (
164+
{(item, index) => (
134165
<ForwardMyItem
135166
{...item}
167+
motionAppear={animating && insertIndex === index}
136168
visible={!closeMap[item.id]}
137169
onClose={onClose}
138170
onLeave={onLeave}
171+
onAppear={onAppear}
139172
onInsertBefore={onInsertBefore}
140173
onInsertAfter={onInsertAfter}
141174
/>

0 commit comments

Comments
 (0)