|
@@ -1,21 +1,23 @@
|
|
|
import React, { CSSProperties, ReactNode } from 'react';
|
|
|
-import { isEqual } from "lodash";
|
|
|
+import { isEqual, noop } from "lodash";
|
|
|
|
|
|
|
|
|
interface AnimationEventsNeedBind {
|
|
|
- onAnimationStart: (e: AnimationEvent) => void
|
|
|
- onAnimationEnd: (e: AnimationEvent) => void
|
|
|
+ onAnimationStart: (e: React.AnimationEvent) => void
|
|
|
+ onAnimationEnd: (e: React.AnimationEvent) => void
|
|
|
}
|
|
|
|
|
|
interface AnimationProps {
|
|
|
- startClassName: string;
|
|
|
- endClassName: string;
|
|
|
+ startClassName?: string;
|
|
|
+ endClassName?: string;
|
|
|
children: ({}: {
|
|
|
- className: string,
|
|
|
- style: CSSProperties,
|
|
|
+ animationClassName: string,
|
|
|
+ animationStyle: CSSProperties,
|
|
|
animationEventsNeedBind: AnimationEventsNeedBind
|
|
|
}) => ReactNode
|
|
|
animationState: "enter" | "leave"
|
|
|
+ onAnimationEnd?:()=>void;
|
|
|
+ onAnimationStart?:()=>void;
|
|
|
}
|
|
|
|
|
|
interface AnimationState {
|
|
@@ -24,38 +26,43 @@ interface AnimationState {
|
|
|
}
|
|
|
|
|
|
|
|
|
-class Animation extends React.Component<AnimationProps, AnimationState> {
|
|
|
+class CSSAnimation extends React.Component<AnimationProps, AnimationState> {
|
|
|
constructor(props) {
|
|
|
super(props);
|
|
|
this.state = {
|
|
|
- currentClassName: "",
|
|
|
+ currentClassName: this.props.startClassName,
|
|
|
extraStyle: {}
|
|
|
};
|
|
|
+ console.log("===>", this.props);
|
|
|
}
|
|
|
|
|
|
componentDidUpdate(prevProps: Readonly<AnimationProps>, prevState: Readonly<AnimationState>, snapshot?: any) {
|
|
|
- const changedKeys = Object.keys(this.props).filter(key => isEqual(this.props[key], prevProps[key]));
|
|
|
- if ("state" in changedKeys) {
|
|
|
- if (this.props.animationState === "enter") {
|
|
|
- this.setState({
|
|
|
- currentClassName: this.props.startClassName,
|
|
|
- extraStyle: {}
|
|
|
- });
|
|
|
- } else {
|
|
|
- this.setState({
|
|
|
- currentClassName: this.props.endClassName,
|
|
|
- extraStyle: {}
|
|
|
- });
|
|
|
- }
|
|
|
+ const changedKeys = Object.keys(this.props).filter(key => !isEqual(this.props[key], prevProps[key]));
|
|
|
+ console.log('changedKeys', changedKeys, prevProps, this.props);
|
|
|
+ if (changedKeys.includes("animationState")) {
|
|
|
+ // if (this.props.animationState === "enter") {
|
|
|
+ // this.setState({
|
|
|
+ // currentClassName: this.props.startClassName,
|
|
|
+ // extraStyle: {}
|
|
|
+ // }, this.props.onAnimationStart ?? noop);
|
|
|
+ // } else {
|
|
|
+ // this.setState({
|
|
|
+ // currentClassName: this.props.endClassName,
|
|
|
+ // extraStyle: {}
|
|
|
+ // }, this.props.onAnimationEnd ?? noop);
|
|
|
+ // }
|
|
|
+ }
|
|
|
+ if (changedKeys.includes("startClassName")){
|
|
|
+ this.setState({
|
|
|
+ currentClassName: this.props.startClassName,
|
|
|
+ extraStyle: {}
|
|
|
+ }, this.props.onAnimationStart ?? noop);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
handleAnimationStart = () => {
|
|
|
- this.setState({
|
|
|
- currentClassName: this.props.startClassName,
|
|
|
- extraStyle: {}
|
|
|
- });
|
|
|
+ this.props.onAnimationStart();
|
|
|
}
|
|
|
|
|
|
|
|
@@ -63,14 +70,17 @@ class Animation extends React.Component<AnimationProps, AnimationState> {
|
|
|
this.setState({
|
|
|
currentClassName: this.props.endClassName,
|
|
|
extraStyle: {}
|
|
|
+ }, ()=>{
|
|
|
+ this.props.onAnimationEnd();
|
|
|
});
|
|
|
}
|
|
|
|
|
|
|
|
|
render() {
|
|
|
+ console.log("mount", this.state.currentClassName);
|
|
|
return this.props.children({
|
|
|
- className: this.state.currentClassName,
|
|
|
- style: this.state.extraStyle,
|
|
|
+ animationClassName: this.state.currentClassName ?? "",
|
|
|
+ animationStyle: this.state.extraStyle,
|
|
|
animationEventsNeedBind: {
|
|
|
onAnimationStart: this.handleAnimationStart,
|
|
|
onAnimationEnd: this.handleAnimationEnd
|
|
@@ -80,4 +90,4 @@ class Animation extends React.Component<AnimationProps, AnimationState> {
|
|
|
|
|
|
}
|
|
|
|
|
|
-export default Animation;
|
|
|
+export default CSSAnimation;
|