|
| 1 | +import * as React from 'react'; |
| 2 | +// @ts-ignore |
| 3 | +import CSSMotion from 'rc-animate/lib/CSSMotion'; |
| 4 | +import classNames from 'classnames'; |
| 5 | +import List, { ScrollInfo } from '../src/List'; |
| 6 | +import './animate.less'; |
| 7 | + |
| 8 | +let uuid = 0; |
| 9 | +function genItem() { |
| 10 | + const item = { |
| 11 | + id: `key_${uuid}`, |
| 12 | + uuid, |
| 13 | + }; |
| 14 | + uuid += 1; |
| 15 | + return item; |
| 16 | +} |
| 17 | + |
| 18 | +const originDataSource: Item[] = []; |
| 19 | +for (let i = 0; i < 100000; i += 1) { |
| 20 | + originDataSource.push(genItem()); |
| 21 | +} |
| 22 | + |
| 23 | +interface Item { |
| 24 | + id: string; |
| 25 | + uuid: number; |
| 26 | +} |
| 27 | + |
| 28 | +interface MyItemProps extends Item { |
| 29 | + visible: boolean; |
| 30 | + motionAppear: boolean; |
| 31 | + onClose: (id: string) => void; |
| 32 | + onLeave: (id: string) => void; |
| 33 | + onAppear: (...args: any[]) => void; |
| 34 | + onInsertBefore: (id: string) => void; |
| 35 | + onInsertAfter: (id: string) => void; |
| 36 | +} |
| 37 | + |
| 38 | +const getCurrentHeight = (node: HTMLElement) => ({ height: node.offsetHeight }); |
| 39 | +const getMaxHeight = (node: HTMLElement) => { |
| 40 | + return { height: node.scrollHeight }; |
| 41 | +}; |
| 42 | +const getCollapsedHeight = () => ({ height: 0, opacity: 0 }); |
| 43 | + |
| 44 | +const MyItem: React.FC<MyItemProps> = ( |
| 45 | + { id, uuid, visible, onClose, onLeave, onAppear, onInsertBefore, onInsertAfter, motionAppear }, |
| 46 | + ref, |
| 47 | +) => { |
| 48 | + return ( |
| 49 | + <CSSMotion |
| 50 | + visible={visible} |
| 51 | + ref={ref} |
| 52 | + motionName="motion" |
| 53 | + motionAppear={motionAppear} |
| 54 | + onAppearStart={getCollapsedHeight} |
| 55 | + onAppearActive={getMaxHeight} |
| 56 | + onAppearEnd={onAppear} |
| 57 | + onLeaveStart={getCurrentHeight} |
| 58 | + onLeaveActive={getCollapsedHeight} |
| 59 | + onLeaveEnd={() => { |
| 60 | + onLeave(id); |
| 61 | + }} |
| 62 | + > |
| 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> |
| 93 | + </div> |
| 94 | + ); |
| 95 | + }} |
| 96 | + </CSSMotion> |
| 97 | + ); |
| 98 | +}; |
| 99 | + |
| 100 | +const ForwardMyItem = React.forwardRef(MyItem); |
| 101 | + |
| 102 | +const Demo = () => { |
| 103 | + const [dataSource, setDataSource] = React.useState(originDataSource); |
| 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>>(); |
| 109 | + |
| 110 | + const onClose = (id: string) => { |
| 111 | + setCloseMap({ |
| 112 | + ...closeMap, |
| 113 | + [id]: true, |
| 114 | + }); |
| 115 | + }; |
| 116 | + |
| 117 | + const onLeave = (id: string) => { |
| 118 | + const newDataSource = dataSource.filter(item => item.id !== id); |
| 119 | + setDataSource(newDataSource); |
| 120 | + }; |
| 121 | + |
| 122 | + const onAppear = (...args: any[]) => { |
| 123 | + setAnimating(false); |
| 124 | + }; |
| 125 | + |
| 126 | + function lockForAnimation() { |
| 127 | + setAnimating(true); |
| 128 | + } |
| 129 | + |
| 130 | + const onInsertBefore = (id: string) => { |
| 131 | + const index = dataSource.findIndex(item => item.id === id); |
| 132 | + const newDataSource = [...dataSource.slice(0, index), genItem(), ...dataSource.slice(index)]; |
| 133 | + setInsertIndex(index); |
| 134 | + setDataSource(newDataSource); |
| 135 | + lockForAnimation(); |
| 136 | + }; |
| 137 | + const onInsertAfter = (id: string) => { |
| 138 | + const index = dataSource.findIndex(item => item.id === id) + 1; |
| 139 | + const newDataSource = [...dataSource.slice(0, index), genItem(), ...dataSource.slice(index)]; |
| 140 | + setInsertIndex(index); |
| 141 | + setDataSource(newDataSource); |
| 142 | + lockForAnimation(); |
| 143 | + }; |
| 144 | + |
| 145 | + return ( |
| 146 | + <React.StrictMode> |
| 147 | + <div> |
| 148 | + <h2>Animate</h2> |
| 149 | + <p>Current: {dataSource.length} records</p> |
| 150 | + |
| 151 | + <List<Item> |
| 152 | + dataSource={dataSource} |
| 153 | + data-id="list" |
| 154 | + height={200} |
| 155 | + itemHeight={30} |
| 156 | + itemKey="id" |
| 157 | + disabled={animating} |
| 158 | + ref={listRef} |
| 159 | + style={{ |
| 160 | + border: '1px solid red', |
| 161 | + boxSizing: 'border-box', |
| 162 | + }} |
| 163 | + > |
| 164 | + {(item, index) => ( |
| 165 | + <ForwardMyItem |
| 166 | + {...item} |
| 167 | + motionAppear={animating && insertIndex === index} |
| 168 | + visible={!closeMap[item.id]} |
| 169 | + onClose={onClose} |
| 170 | + onLeave={onLeave} |
| 171 | + onAppear={onAppear} |
| 172 | + onInsertBefore={onInsertBefore} |
| 173 | + onInsertAfter={onInsertAfter} |
| 174 | + /> |
| 175 | + )} |
| 176 | + </List> |
| 177 | + </div> |
| 178 | + </React.StrictMode> |
| 179 | + ); |
| 180 | +}; |
| 181 | + |
| 182 | +export default Demo; |
0 commit comments