-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathCSSMotionList.tsx
More file actions
169 lines (150 loc) · 4.5 KB
/
CSSMotionList.tsx
File metadata and controls
169 lines (150 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* eslint react/prop-types: 0 */
import omit from 'rc-util/lib/omit';
import * as React from 'react';
import type { CSSMotionProps } from './CSSMotion';
import OriginCSSMotion from './CSSMotion';
import type { KeyObject } from './util/diff';
import {
diffKeys,
parseKeys,
STATUS_ADD,
STATUS_KEEP,
STATUS_REMOVE,
STATUS_REMOVED,
} from './util/diff';
import { supportTransition } from './util/motion';
import pick from './util/pick';
const MOTION_PROP_NAMES = [
'eventProps',
'visible',
'children',
'motionName',
'motionAppear',
'motionEnter',
'motionLeave',
'motionLeaveImmediately',
'motionDeadline',
'removeOnLeave',
'leavedClassName',
'onAppearStart',
'onAppearActive',
'onAppearEnd',
'onEnterStart',
'onEnterActive',
'onEnterEnd',
'onLeaveStart',
'onLeaveActive',
'onLeaveEnd',
];
export interface CSSMotionListProps
extends Omit<CSSMotionProps, 'onVisibleChanged'>,
Omit<React.HTMLAttributes<any>, 'children'> {
keys: (React.Key | { key: React.Key; [name: string]: any })[];
component?: string | React.ComponentType | false;
/** This will always trigger after final visible changed. Even if no motion configured. */
onVisibleChanged?: (visible: boolean, info: { key: React.Key }) => void;
/** All motion leaves in the screen */
onAllRemoved?: () => void;
}
export interface CSSMotionListState {
keyEntities: KeyObject[];
}
/**
* Generate a CSSMotionList component with config
* @param transitionSupport No need since CSSMotionList no longer depends on transition support
* @param CSSMotion CSSMotion component
*/
export function genCSSMotionList(
transitionSupport: boolean,
CSSMotion = OriginCSSMotion,
): React.ComponentClass<CSSMotionListProps> {
class CSSMotionList extends React.Component<
CSSMotionListProps,
CSSMotionListState
> {
static defaultProps = {
component: 'div',
};
state: CSSMotionListState = {
keyEntities: [],
};
static getDerivedStateFromProps(
{ keys }: CSSMotionListProps,
{ keyEntities }: CSSMotionListState,
) {
const parsedKeyObjects = parseKeys(keys);
const mixedKeyEntities = diffKeys(keyEntities, parsedKeyObjects);
return {
keyEntities: mixedKeyEntities.filter(entity => {
const prevEntity = keyEntities.find(({ key }) => entity.key === key);
// Remove if already mark as removed
if (
prevEntity &&
prevEntity.status === STATUS_REMOVED &&
entity.status === STATUS_REMOVE
) {
return false;
}
return true;
}),
};
}
// ZombieJ: Return the count of rest keys. It's safe to refactor if need more info.
removeKey = (removeKey: React.Key) => {
const { keyEntities } = this.state;
const nextKeyEntities = keyEntities.map(entity => {
if (entity.key !== removeKey) return entity;
return {
...entity,
status: STATUS_REMOVED,
};
});
this.setState({
keyEntities: nextKeyEntities,
});
return nextKeyEntities.filter(({ status }) => status !== STATUS_REMOVED)
.length;
};
render() {
const { keyEntities } = this.state;
const {
component,
children,
onVisibleChanged,
onAllRemoved,
...restProps
} = this.props;
const Component = component || React.Fragment;
const motionProps = pick(restProps, MOTION_PROP_NAMES as any);
const componentProps = omit(restProps, MOTION_PROP_NAMES as any);
return (
<Component {...componentProps}>
{keyEntities.map(({ status, ...eventProps }) => {
const visible = status === STATUS_ADD || status === STATUS_KEEP;
return (
<CSSMotion
{...motionProps}
key={eventProps.key}
visible={visible}
eventProps={eventProps}
onVisibleChanged={changedVisible => {
onVisibleChanged?.(changedVisible, { key: eventProps.key });
if (!changedVisible) {
const restKeysCount = this.removeKey(eventProps.key);
if (restKeysCount === 0 && onAllRemoved) {
onAllRemoved();
}
}
}}
>
{children}
</CSSMotion>
);
})}
</Component>
);
}
}
return CSSMotionList;
}
export default genCSSMotionList(supportTransition);