|
| 1 | +/** |
| 2 | + * title: transition |
| 3 | + * description: 手动搭配 Transition 使用(在 TransitionGroup 内部列表条目也是包裹了 Transition 的) |
| 4 | + */ |
| 5 | + |
| 6 | +import { |
| 7 | + transitionCBAdapter, |
| 8 | + TransitionGroup, |
| 9 | + Transition, |
| 10 | + Button, |
| 11 | + Space, |
| 12 | +} from '@tool-pack/react-ui'; |
| 13 | +import React, { useState, useRef } from 'react'; |
| 14 | +import styles from './list.module.scss'; |
| 15 | + |
| 16 | +const App: React.FC = () => { |
| 17 | + const [list, setList] = useState<number[]>([ |
| 18 | + ...Array.from({ length: 10 }).keys(), |
| 19 | + ]); |
| 20 | + |
| 21 | + const index = useRef(list.length); |
| 22 | + function addChild() { |
| 23 | + const splice = list.splice(~~(Math.random() * list.length), list.length); |
| 24 | + list.push(index.current); |
| 25 | + list.push(...splice); |
| 26 | + index.current++; |
| 27 | + setList(list.slice()); |
| 28 | + } |
| 29 | + function removeChild(item: number) { |
| 30 | + const index = list.indexOf(item); |
| 31 | + if (index === -1) return; |
| 32 | + list.splice(index, 1); |
| 33 | + setList(list.slice()); |
| 34 | + } |
| 35 | + function removeRandomChild() { |
| 36 | + removeChild(list[~~(Math.random() * list.length)]!); |
| 37 | + } |
| 38 | + |
| 39 | + return ( |
| 40 | + <div className={styles['root']}> |
| 41 | + <Space style={{ justifyContent: 'center' }}> |
| 42 | + <Button onClick={addChild} type="primary"> |
| 43 | + 添加 |
| 44 | + </Button> |
| 45 | + <Button onClick={removeRandomChild} type="warning" plain> |
| 46 | + 移除 |
| 47 | + </Button> |
| 48 | + </Space> |
| 49 | + <br /> |
| 50 | + <TransitionGroup className="group-container" tag="section" name="group"> |
| 51 | + {list.map((item) => { |
| 52 | + return ( |
| 53 | + <Transition |
| 54 | + on={transitionCBAdapter({ |
| 55 | + onBeforeEnter() { |
| 56 | + console.log(item + '正在进来'); |
| 57 | + }, |
| 58 | + onBeforeLeave() { |
| 59 | + console.log(item + '正在离去'); |
| 60 | + }, |
| 61 | + onAfterEnter() { |
| 62 | + console.log(item + '已进来'); |
| 63 | + }, |
| 64 | + onAfterLeave() { |
| 65 | + console.log(item + '已离去'); |
| 66 | + }, |
| 67 | + })} |
| 68 | + key={item} |
| 69 | + > |
| 70 | + <div>{item}</div> |
| 71 | + </Transition> |
| 72 | + ); |
| 73 | + })} |
| 74 | + </TransitionGroup> |
| 75 | + </div> |
| 76 | + ); |
| 77 | +}; |
| 78 | + |
| 79 | +export default App; |
0 commit comments