-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from js-tool-pack/fix-transition-group
Fix transition group
- Loading branch information
Showing
29 changed files
with
537 additions
and
337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 31 additions & 35 deletions
66
packages/components/src/transition-group/TransitionGroup.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,38 @@ | ||
import React, { memo, useRef } from 'react'; | ||
import { Transition } from '../transition/index'; | ||
import { ChildStatus } from './transition-group.enums'; | ||
import { useDispatcher } from './transition-gorup.hooks'; | ||
import { TransitionGroupProps } from './transition-group.types'; | ||
import React from 'react'; | ||
import { useFlips } from './useFlips'; | ||
import { useWrapper } from './useWrapper'; | ||
import { useChildMap } from './useChildMap'; | ||
import type { TransitionGroupProps } from './transition-group.types'; | ||
import { RequiredPart } from '@tool-pack/types'; | ||
|
||
const TransitionGroup: React.FC<TransitionGroupProps> = ( | ||
props, | ||
): React.ReactElement => { | ||
const { mode, name, on, tag, appear, children, ...rest } = props; | ||
const containerRef = useRef<HTMLElement>(null); | ||
/** | ||
* v1 版有部分 bug 不好解决。 | ||
* | ||
* v2版参考了 {@link https://github.com/peoplesing1832/react-transition/blob/npm/src/v4/TransitionGroup.tsx} 该实现。 | ||
* | ||
* 相比 v1 版, v2 需要元素外部套 Transition 组件,可随机插入(v1不行),动画更流畅。 | ||
* | ||
* 目前还是不够完美,以后需要再次优化。。。 | ||
*/ | ||
|
||
const [_children, _appear] = useDispatcher( | ||
containerRef, | ||
name, | ||
children, | ||
appear, | ||
on, | ||
); | ||
const defaultProps = { | ||
tag: 'div', | ||
name: 't-group', | ||
} satisfies TransitionGroupProps; | ||
|
||
return React.createElement( | ||
tag as string, | ||
{ ...rest, ref: containerRef }, | ||
_children.map((child) => { | ||
return ( | ||
<Transition | ||
name={name} | ||
mode={mode} | ||
appear={child.status === ChildStatus.enter && _appear} | ||
key={child.component.key} | ||
on={child.on}> | ||
{child.status === ChildStatus.leave ? undefined : child.component} | ||
</Transition> | ||
); | ||
}), | ||
); | ||
const TransitionGroup: React.FC<TransitionGroupProps> = (props) => { | ||
const { name, children, ...rest } = props as RequiredPart< | ||
TransitionGroupProps, | ||
keyof typeof defaultProps | ||
>; | ||
|
||
const childMap = useChildMap(children, name); | ||
const [wrapper, parentRef] = useWrapper(childMap, rest); | ||
useFlips(parentRef, name); | ||
return wrapper; | ||
}; | ||
|
||
TransitionGroup.defaultProps = { tag: 'div' }; | ||
TransitionGroup.defaultProps = defaultProps; | ||
TransitionGroup.displayName = 'TransitionGroup'; | ||
|
||
export default memo(TransitionGroup); | ||
export default React.memo(TransitionGroup); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.