victory-create-container 36.9.2-next.3 → 37.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -20
- package/README.md +2 -2
- package/dist/victory-create-container.js +5225 -7160
- package/dist/victory-create-container.min.js +1 -1
- package/es/create-container.d.ts +46 -46
- package/es/create-container.js +58 -145
- package/es/index.d.ts +1 -1
- package/lib/create-container.d.ts +46 -46
- package/lib/create-container.js +59 -155
- package/lib/index.d.ts +1 -1
- package/lib/index.js +0 -2
- package/package.json +11 -8
- package/src/create-container.ts +173 -0
- package/src/create-container.tsx +0 -169
package/src/create-container.tsx
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
VictoryZoomContainer,
|
|
3
|
-
useVictoryZoomContainer,
|
|
4
|
-
} from "victory-zoom-container";
|
|
5
|
-
import {
|
|
6
|
-
VictorySelectionContainer,
|
|
7
|
-
useVictorySelectionContainer,
|
|
8
|
-
} from "victory-selection-container";
|
|
9
|
-
import React from "react";
|
|
10
|
-
import { VictoryContainer } from "victory-core";
|
|
11
|
-
import { forOwn, groupBy, isEmpty, toPairs } from "lodash";
|
|
12
|
-
import {
|
|
13
|
-
VictoryVoronoiContainer,
|
|
14
|
-
useVictoryVoronoiContainer,
|
|
15
|
-
} from "victory-voronoi-container";
|
|
16
|
-
import {
|
|
17
|
-
VictoryCursorContainer,
|
|
18
|
-
useVictoryCursorContainer,
|
|
19
|
-
} from "victory-cursor-container";
|
|
20
|
-
import {
|
|
21
|
-
VictoryBrushContainer,
|
|
22
|
-
useVictoryBrushContainer,
|
|
23
|
-
} from "victory-brush-container";
|
|
24
|
-
|
|
25
|
-
function ensureArray<T>(thing: T): [] | T | T[] {
|
|
26
|
-
if (!thing) {
|
|
27
|
-
return [];
|
|
28
|
-
} else if (!Array.isArray(thing)) {
|
|
29
|
-
return [thing];
|
|
30
|
-
}
|
|
31
|
-
return thing;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const combineEventHandlers = (eventHandlersArray: any[]) => {
|
|
35
|
-
// takes an array of event handler objects and produces one eventHandlers object
|
|
36
|
-
// creates a custom combinedHandler() for events with multiple conflicting handlers
|
|
37
|
-
return eventHandlersArray.reduce((localHandlers, finalHandlers) => {
|
|
38
|
-
forOwn(localHandlers, (localHandler, eventName) => {
|
|
39
|
-
const existingHandler = finalHandlers[eventName];
|
|
40
|
-
if (existingHandler) {
|
|
41
|
-
// create new handler for event that concats the existing handler's mutations with new ones
|
|
42
|
-
finalHandlers[eventName] = function combinedHandler(...params) {
|
|
43
|
-
// named for debug clarity
|
|
44
|
-
// sometimes handlers return undefined; use empty array instead, for concat()
|
|
45
|
-
const existingMutations = ensureArray(existingHandler(...params));
|
|
46
|
-
const localMutations = ensureArray(localHandler(...params));
|
|
47
|
-
return existingMutations.concat(localMutations);
|
|
48
|
-
};
|
|
49
|
-
} else {
|
|
50
|
-
finalHandlers[eventName] = localHandler;
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
return finalHandlers;
|
|
54
|
-
});
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const combineDefaultEvents = (defaultEvents: any[]) => {
|
|
58
|
-
// takes a defaultEvents array and returns one equal or lesser length,
|
|
59
|
-
// by combining any events that have the same target
|
|
60
|
-
const eventsByTarget = groupBy(defaultEvents, "target");
|
|
61
|
-
const events = toPairs(eventsByTarget).map(([target, eventsArray]) => {
|
|
62
|
-
const newEventsArray = eventsArray.filter(Boolean);
|
|
63
|
-
return isEmpty(newEventsArray)
|
|
64
|
-
? null
|
|
65
|
-
: {
|
|
66
|
-
target,
|
|
67
|
-
eventHandlers: combineEventHandlers(
|
|
68
|
-
eventsArray.map((event) => event.eventHandlers),
|
|
69
|
-
),
|
|
70
|
-
// note: does not currently handle eventKey or childName
|
|
71
|
-
};
|
|
72
|
-
});
|
|
73
|
-
return events.filter(Boolean);
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
export type ContainerType =
|
|
77
|
-
| "zoom"
|
|
78
|
-
| "selection"
|
|
79
|
-
| "brush"
|
|
80
|
-
| "cursor"
|
|
81
|
-
| "voronoi";
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Container hooks are used to provide the container logic to the container components through props and a modified children object
|
|
85
|
-
* - These hooks contain shared logic for both web and Victory Native containers.
|
|
86
|
-
* - In this utility, we call multiple of these hooks with the props returned by the previous to combine the container logic.
|
|
87
|
-
*/
|
|
88
|
-
const CONTAINER_HOOKS = {
|
|
89
|
-
zoom: useVictoryZoomContainer,
|
|
90
|
-
selection: useVictorySelectionContainer,
|
|
91
|
-
brush: useVictoryBrushContainer,
|
|
92
|
-
cursor: useVictoryCursorContainer,
|
|
93
|
-
voronoi: useVictoryVoronoiContainer,
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Container hooks are wrappers that return a VictoryContainer with the props provided by their respective hooks, and the modified children.
|
|
98
|
-
* - These containers are specific to the web. Victory Native has its own container components.
|
|
99
|
-
* - For this utility, we only need the container components to extract the defaultEvents.
|
|
100
|
-
*/
|
|
101
|
-
const CONTAINER_COMPONENTS = {
|
|
102
|
-
zoom: VictoryZoomContainer,
|
|
103
|
-
selection: VictorySelectionContainer,
|
|
104
|
-
brush: VictoryBrushContainer,
|
|
105
|
-
cursor: VictoryCursorContainer,
|
|
106
|
-
voronoi: VictoryVoronoiContainer,
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
type ContainerComponents = Record<
|
|
110
|
-
ContainerType,
|
|
111
|
-
React.ComponentType<any> & {
|
|
112
|
-
defaultEvents: (props: any) => any[];
|
|
113
|
-
}
|
|
114
|
-
>;
|
|
115
|
-
|
|
116
|
-
export function makeCreateContainerFunction<
|
|
117
|
-
TContainerComponents extends ContainerComponents,
|
|
118
|
-
>(
|
|
119
|
-
containerComponents: TContainerComponents,
|
|
120
|
-
VictoryContainerBase: typeof VictoryContainer,
|
|
121
|
-
) {
|
|
122
|
-
type ContainerProps<T extends ContainerType> = React.ComponentProps<
|
|
123
|
-
TContainerComponents[T]
|
|
124
|
-
>;
|
|
125
|
-
|
|
126
|
-
return function combineContainers<
|
|
127
|
-
TContainerA extends ContainerType,
|
|
128
|
-
TContainerB extends ContainerType,
|
|
129
|
-
>(containerA: TContainerA, containerB: TContainerB) {
|
|
130
|
-
const ContainerA = containerComponents[containerA];
|
|
131
|
-
const ContainerB = containerComponents[containerB];
|
|
132
|
-
const useContainerA = CONTAINER_HOOKS[containerA];
|
|
133
|
-
const useContainerB = CONTAINER_HOOKS[containerB];
|
|
134
|
-
|
|
135
|
-
const CombinedContainer = (
|
|
136
|
-
props: ContainerProps<TContainerA> & ContainerProps<TContainerB>,
|
|
137
|
-
) => {
|
|
138
|
-
const { children: childrenA, props: propsA } = useContainerA(props);
|
|
139
|
-
const { children: combinedChildren, props: combinedProps } =
|
|
140
|
-
useContainerB({
|
|
141
|
-
...propsA,
|
|
142
|
-
children: childrenA,
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
return (
|
|
146
|
-
<VictoryContainerBase {...combinedProps}>
|
|
147
|
-
{combinedChildren}
|
|
148
|
-
</VictoryContainerBase>
|
|
149
|
-
);
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
CombinedContainer.displayName = `Victory${containerA}${containerB}Container`;
|
|
153
|
-
CombinedContainer.role = "container";
|
|
154
|
-
CombinedContainer.defaultEvents = (
|
|
155
|
-
props: ContainerProps<TContainerA> & ContainerProps<TContainerB>,
|
|
156
|
-
) =>
|
|
157
|
-
combineDefaultEvents([
|
|
158
|
-
...ContainerA.defaultEvents(props),
|
|
159
|
-
...ContainerB.defaultEvents(props),
|
|
160
|
-
]);
|
|
161
|
-
|
|
162
|
-
return CombinedContainer;
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export const createContainer = makeCreateContainerFunction(
|
|
167
|
-
CONTAINER_COMPONENTS,
|
|
168
|
-
VictoryContainer,
|
|
169
|
-
);
|