This repository has been archived by the owner on Feb 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
/
add-events.js
210 lines (188 loc) · 8.16 KB
/
add-events.js
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import React from "react";
import { defaults, assign, keys, isFunction, pick, without, isEmpty } from "lodash";
import Events from "./events";
import isEqual from "react-fast-compare";
import VictoryTransition from "../victory-transition/victory-transition";
// used for checking state changes. Expected components can be passed in via options
const defaultComponents = [
{ name: "parent", index: "parent" }, { name: "data" }, { name: "labels" }
];
export default (WrappedComponent, options) => {
return class addEvents extends WrappedComponent {
constructor(props) {
super(props);
const getScopedEvents = Events.getScopedEvents.bind(this);
const boundGetEvents = Events.getEvents.bind(this);
this.state = {};
this.getEvents = (p, target, eventKey) => {
return boundGetEvents(p, target, eventKey, getScopedEvents);
};
this.getEventState = Events.getEventState.bind(this);
const calculatedValues = this.getCalculatedValues(props);
this.cacheValues(calculatedValues);
this.externalMutations = this.getExternalMutations(props);
}
componentDidUpdate() {
const externalMutations = this.getExternalMutations(this.props);
if (!isEqual(this.externalMutations, externalMutations)) {
this.externalMutations = externalMutations;
this.applyExternalMutations(this.props, externalMutations);
}
}
componentWillReceiveProps(nextProps) {
this.cacheValues(this.getCalculatedValues(nextProps));
}
applyExternalMutations(props, externalMutations) {
if (!isEmpty(externalMutations)) {
const callbacks = props.externalEventMutations.reduce((memo, mutation) => {
memo = isFunction(mutation.callback) ? memo.concat(mutation.callback) : memo;
return memo;
}, []);
const compiledCallbacks = callbacks.length ?
() => { callbacks.forEach((c) => c()); } : undefined;
this.setState(externalMutations, compiledCallbacks);
}
}
// compile all state changes from own and parent state. Order doesn't matter, as any state
// state change should trigger a re-render
getStateChanges(props, calculatedValues) {
const { hasEvents, getSharedEventState } = calculatedValues;
if (!hasEvents) { return {}; }
options = options || {};
const components = options.components || defaultComponents;
const getState = (key, type) => {
const baseState = defaults(
{}, this.getEventState(key, type), getSharedEventState(key, type)
);
return isEmpty(baseState) ? undefined : baseState;
};
return components.map((component) => {
if (!props.standalone && component.name === "parent") {
// don't check for changes on parent props for non-standalone components
return undefined;
} else {
return component.index !== undefined ?
getState(component.index, component.name) :
calculatedValues.dataKeys.map((key) => getState(key, component.name));
}
}).filter(Boolean);
}
getCalculatedValues(props) {
const { sharedEvents } = props;
const components = WrappedComponent.expectedComponents;
const componentEvents = Events.getComponentEvents(props, components);
const getSharedEventState = sharedEvents && isFunction(sharedEvents.getEventState) ?
sharedEvents.getEventState : () => undefined;
const baseProps = this.getBaseProps(props, getSharedEventState);
const dataKeys = keys(baseProps).filter((key) => key !== "parent");
const hasEvents = props.events || props.sharedEvents || componentEvents;
const events = this.getAllEvents(props);
return {
componentEvents, getSharedEventState, baseProps, dataKeys,
hasEvents, events
};
}
getExternalMutations(props) {
const { sharedEvents, externalEventMutations } = props;
return isEmpty(externalEventMutations) || sharedEvents ? undefined :
Events.getExternalMutations(
externalEventMutations, this.baseProps, this.state
);
}
cacheValues(obj) {
keys(obj).forEach((key) => {
this[key] = obj[key];
});
}
getBaseProps(props, getSharedEventState) {
getSharedEventState = getSharedEventState || this.getSharedEventState;
const sharedParentState = getSharedEventState("parent", "parent");
const parentState = this.getEventState("parent", "parent");
const baseParentProps = defaults({}, parentState, sharedParentState);
const parentPropsList = baseParentProps.parentControlledProps;
const parentProps = parentPropsList ? pick(baseParentProps, parentPropsList) : {};
const modifiedProps = defaults({}, parentProps, props);
return isFunction(WrappedComponent.getBaseProps) ?
WrappedComponent.getBaseProps(modifiedProps) : {};
}
getAllEvents(props) {
if (Array.isArray(this.componentEvents)) {
return Array.isArray(props.events) ?
this.componentEvents.concat(...props.events) : this.componentEvents;
}
return props.events;
}
getComponentProps(component, type, index) {
const { role } = WrappedComponent;
const key = this.dataKeys && this.dataKeys[index] || index;
const baseProps = this.baseProps[key] && this.baseProps[key][type] || this.baseProps[key];
if (!baseProps && !this.hasEvents) {
return undefined;
}
if (this.hasEvents) {
const baseEvents = this.getEvents(this.props, type, key);
const componentProps = defaults(
{ index, key: `${role}-${type}-${key}` },
this.getEventState(key, type),
this.getSharedEventState(key, type),
component.props,
baseProps
);
const events = defaults(
{}, Events.getPartialEvents(baseEvents, key, componentProps), componentProps.events
);
return assign(
{}, componentProps, { events }
);
}
return defaults(
{ index, key: `${role}-${type}-${key}` },
component.props,
baseProps
);
}
renderContainer(component, children) {
const isContainer = component.type && component.type.role === "container";
const parentProps = isContainer ? this.getComponentProps(component, "parent", "parent") : {};
return React.cloneElement(component, parentProps, children);
}
animateComponent(props, animationWhitelist) {
return (
<VictoryTransition animate={props.animate} animationWhitelist={animationWhitelist}>
{React.createElement(this.constructor, props)}
</VictoryTransition>
);
}
// Used by `VictoryLine` and `VictoryArea`
renderContinuousData(props) {
const { dataComponent, labelComponent, groupComponent } = props;
const dataKeys = without(this.dataKeys, "all");
const labelComponents = dataKeys.reduce((memo, key) => {
const labelProps = this.getComponentProps(labelComponent, "labels", key);
if (labelProps && labelProps.text !== undefined && labelProps.text !== null) {
memo = memo.concat(React.cloneElement(labelComponent, labelProps));
}
return memo;
}, []);
const dataProps = this.getComponentProps(dataComponent, "data", "all");
const children = [React.cloneElement(dataComponent, dataProps), ...labelComponents];
return this.renderContainer(groupComponent, children);
}
renderData(props) {
const { dataComponent, labelComponent, groupComponent } = props;
const dataComponents = this.dataKeys.map((_dataKey, index) => {
const dataProps = this.getComponentProps(dataComponent, "data", index);
return React.cloneElement(dataComponent, dataProps);
});
const labelComponents = this.dataKeys.map((_dataKey, index) => {
const labelProps = this.getComponentProps(labelComponent, "labels", index);
if (labelProps.text !== undefined && labelProps.text !== null) {
return React.cloneElement(labelComponent, labelProps);
}
return undefined;
}).filter(Boolean);
const children = [...dataComponents, ...labelComponents];
return this.renderContainer(groupComponent, children);
}
};
};