victory-create-container 36.9.1 → 36.9.2-next.1

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.
@@ -3,34 +3,31 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.makeCreateContainerFunction = exports.createContainer = exports.combineContainerMixins = void 0;
6
+ exports.createContainer = void 0;
7
+ exports.makeCreateContainerFunction = makeCreateContainerFunction;
7
8
 
8
- var _isFunction2 = _interopRequireDefault(require("lodash/isFunction"));
9
+ var _toPairs2 = _interopRequireDefault(require("lodash/toPairs"));
9
10
 
10
11
  var _isEmpty2 = _interopRequireDefault(require("lodash/isEmpty"));
11
12
 
12
- var _flow2 = _interopRequireDefault(require("lodash/flow"));
13
-
14
- var _includes2 = _interopRequireDefault(require("lodash/includes"));
13
+ var _groupBy2 = _interopRequireDefault(require("lodash/groupBy"));
15
14
 
16
15
  var _forOwn2 = _interopRequireDefault(require("lodash/forOwn"));
17
16
 
18
- var _groupBy2 = _interopRequireDefault(require("lodash/groupBy"));
17
+ var _victoryZoomContainer = require("victory-zoom-container");
19
18
 
20
- var _toPairs2 = _interopRequireDefault(require("lodash/toPairs"));
19
+ var _victorySelectionContainer = require("victory-selection-container");
20
+
21
+ var _react = _interopRequireDefault(require("react"));
21
22
 
22
23
  var _victoryCore = require("victory-core");
23
24
 
24
25
  var _victoryVoronoiContainer = require("victory-voronoi-container");
25
26
 
26
- var _victoryZoomContainer = require("victory-zoom-container");
27
-
28
- var _victorySelectionContainer = require("victory-selection-container");
27
+ var _victoryCursorContainer = require("victory-cursor-container");
29
28
 
30
29
  var _victoryBrushContainer = require("victory-brush-container");
31
30
 
32
- var _victoryCursorContainer = require("victory-cursor-container");
33
-
34
31
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35
32
 
36
33
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
@@ -47,26 +44,6 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
47
44
 
48
45
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
49
46
 
50
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
51
-
52
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
53
-
54
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
55
-
56
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
57
-
58
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
59
-
60
- function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
61
-
62
- function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
63
-
64
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
65
-
66
- function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
67
-
68
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
69
-
70
47
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
71
48
 
72
49
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
@@ -134,94 +111,63 @@ var combineDefaultEvents = function (defaultEvents) {
134
111
  return events.filter(Boolean);
135
112
  };
136
113
 
137
- var combineContainerMixins = function (mixins, Container) {
138
- var _class;
139
-
140
- // similar to Object.assign(A, B), this function will decide conflicts in favor mixinB.
141
- // this applies to propTypes and defaultProps.
142
- // getChildren will call A's getChildren() and pass the resulting children to B's.
143
- // defaultEvents attempts to resolve any conflicts between A and B's defaultEvents.
144
- var Classes = mixins.map(function (mixin) {
145
- return mixin(Container);
146
- });
147
- var instances = Classes.map(function (Class) {
148
- return new Class();
149
- });
150
- var NaiveCombinedContainer = (0, _flow2.default)(mixins)(Container);
151
- var displayType = Classes.map(function (Class) {
152
- var match = Class.displayName.match(/Victory(.*)Container/);
153
- return match[1] || "";
154
- }).join("");
155
- return _class = /*#__PURE__*/function (_NaiveCombinedContain) {
156
- _inherits(VictoryCombinedContainer, _NaiveCombinedContain);
157
-
158
- var _super = _createSuper(VictoryCombinedContainer);
159
-
160
- function VictoryCombinedContainer() {
161
- _classCallCheck(this, VictoryCombinedContainer);
162
-
163
- return _super.apply(this, arguments);
164
- }
165
-
166
- _createClass(VictoryCombinedContainer, [{
167
- key: "getChildren",
168
- value: function getChildren(props) {
169
- return instances.reduce(function (children, instance) {
170
- return instance.getChildren(_objectSpread(_objectSpread({}, props), {}, {
171
- children: children
172
- }));
173
- }, props.children);
174
- }
175
- }]);
176
-
177
- return VictoryCombinedContainer;
178
- }(NaiveCombinedContainer), _class.displayName = "Victory".concat(displayType, "Container"), _class.propTypes = Classes.reduce(function (propTypes, Class) {
179
- return _objectSpread(_objectSpread({}, propTypes), Class.propTypes);
180
- }, {}), _class.defaultProps = Classes.reduce(function (defaultProps, Class) {
181
- return _objectSpread(_objectSpread({}, defaultProps), Class.defaultProps);
182
- }, {}), _class.defaultEvents = function (props) {
183
- return combineDefaultEvents(Classes.reduce(function (defaultEvents, Class) {
184
- var events = (0, _isFunction2.default)(Class.defaultEvents) ? Class.defaultEvents(props) : Class.defaultEvents;
185
- return [].concat(_toConsumableArray(defaultEvents), _toConsumableArray(events));
186
- }, []));
187
- }, _class;
114
+ /**
115
+ * Container hooks are used to provide the container logic to the container components through props and a modified children object
116
+ * - These hooks contain shared logic for both web and Victory Native containers.
117
+ * - In this utility, we call multiple of these hooks with the props returned by the previous to combine the container logic.
118
+ */
119
+ var CONTAINER_HOOKS = {
120
+ zoom: _victoryZoomContainer.useVictoryZoomContainer,
121
+ selection: _victorySelectionContainer.useVictorySelectionContainer,
122
+ brush: _victoryBrushContainer.useVictoryBrushContainer,
123
+ cursor: _victoryCursorContainer.useVictoryCursorContainer,
124
+ voronoi: _victoryVoronoiContainer.useVictoryVoronoiContainer
188
125
  };
189
-
190
- exports.combineContainerMixins = combineContainerMixins;
191
-
192
- var checkBehaviorName = function (behavior, behaviors) {
193
- if (behavior && !(0, _includes2.default)(behaviors, behavior)) {
194
- _victoryCore.Log.warn("\"".concat(behavior, "\" is not a valid behavior. Choose from [").concat(behaviors.join(", "), "]."));
195
- }
126
+ /**
127
+ * Container hooks are wrappers that return a VictoryContainer with the props provided by their respective hooks, and the modified children.
128
+ * - These containers are specific to the web. Victory Native has its own container components.
129
+ * - For this utility, we only need the container components to extract the defaultEvents.
130
+ */
131
+
132
+ var CONTAINER_COMPONENTS = {
133
+ zoom: _victoryZoomContainer.VictoryZoomContainer,
134
+ selection: _victorySelectionContainer.VictorySelectionContainer,
135
+ brush: _victoryBrushContainer.VictoryBrushContainer,
136
+ cursor: _victoryCursorContainer.VictoryCursorContainer,
137
+ voronoi: _victoryVoronoiContainer.VictoryVoronoiContainer
196
138
  };
197
139
 
198
- var makeCreateContainerFunction = function (mixinMap, Container) {
199
- return function (behaviorA, behaviorB) {
200
- var behaviors = Object.keys(mixinMap);
201
- checkBehaviorName(behaviorA, behaviors);
202
- checkBehaviorName(behaviorB, behaviors);
203
-
204
- if (arguments.length <= 2 ? 0 : arguments.length - 2) {
205
- _victoryCore.Log.warn("too many arguments given to createContainer (maximum accepted: 2).");
206
- }
140
+ function makeCreateContainerFunction(containerComponents, VictoryContainerBase) {
141
+ return function combineContainers(containerA, containerB) {
142
+ var ContainerA = containerComponents[containerA];
143
+ var ContainerB = containerComponents[containerB];
144
+ var useContainerA = CONTAINER_HOOKS[containerA];
145
+ var useContainerB = CONTAINER_HOOKS[containerB];
146
+
147
+ var CombinedContainer = function (props) {
148
+ var _useContainerA = useContainerA(props),
149
+ childrenA = _useContainerA.children,
150
+ propsA = _useContainerA.props;
151
+
152
+ var _useContainerB = useContainerB(_objectSpread(_objectSpread({}, propsA), {}, {
153
+ children: childrenA
154
+ })),
155
+ combinedChildren = _useContainerB.children,
156
+ combinedProps = _useContainerB.props;
157
+
158
+ return /*#__PURE__*/_react.default.createElement(VictoryContainerBase, combinedProps, combinedChildren);
159
+ };
207
160
 
208
- var firstMixins = mixinMap[behaviorA];
209
- var secondMixins = mixinMap[behaviorB] || [];
161
+ CombinedContainer.displayName = "Victory".concat(containerA).concat(containerB, "Container");
162
+ CombinedContainer.role = "container";
210
163
 
211
- if (!firstMixins) {
212
- return Container;
213
- }
164
+ CombinedContainer.defaultEvents = function (props) {
165
+ return combineDefaultEvents([].concat(_toConsumableArray(ContainerA.defaultEvents(props)), _toConsumableArray(ContainerB.defaultEvents(props))));
166
+ };
214
167
 
215
- return combineContainerMixins([].concat(_toConsumableArray(firstMixins), _toConsumableArray(secondMixins)), Container);
168
+ return CombinedContainer;
216
169
  };
217
- };
170
+ }
218
171
 
219
- exports.makeCreateContainerFunction = makeCreateContainerFunction;
220
- var createContainer = makeCreateContainerFunction({
221
- zoom: [_victoryZoomContainer.zoomContainerMixin],
222
- voronoi: [_victoryVoronoiContainer.voronoiContainerMixin],
223
- selection: [_victorySelectionContainer.selectionContainerMixin],
224
- cursor: [_victoryCursorContainer.cursorContainerMixin],
225
- brush: [_victoryBrushContainer.brushContainerMixin]
226
- }, _victoryCore.VictoryContainer);
172
+ var createContainer = makeCreateContainerFunction(CONTAINER_COMPONENTS, _victoryCore.VictoryContainer);
227
173
  exports.createContainer = createContainer;
package/lib/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from "./create-container";
1
+ export * from "./create-container";
2
2
  //# sourceMappingURL=index.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "victory-create-container",
3
- "version": "36.9.1",
3
+ "version": "36.9.2-next.1",
4
4
  "description": "Container Helper for Victory",
5
5
  "keywords": [
6
6
  "data visualization",
@@ -21,19 +21,16 @@
21
21
  "license": "MIT",
22
22
  "dependencies": {
23
23
  "lodash": "^4.17.19",
24
- "victory-brush-container": "^36.9.1",
25
- "victory-core": "^36.9.1",
26
- "victory-cursor-container": "^36.9.1",
27
- "victory-selection-container": "^36.9.1",
28
- "victory-voronoi-container": "^36.9.1",
29
- "victory-zoom-container": "^36.9.1"
24
+ "victory-brush-container": "^36.9.2-next.1",
25
+ "victory-core": "^36.9.2-next.1",
26
+ "victory-cursor-container": "^36.9.2-next.1",
27
+ "victory-selection-container": "^36.9.2-next.1",
28
+ "victory-voronoi-container": "^36.9.2-next.1",
29
+ "victory-zoom-container": "^36.9.2-next.1"
30
30
  },
31
31
  "peerDependencies": {
32
32
  "react": ">=16.6.0"
33
33
  },
34
- "publishConfig": {
35
- "provenance": true
36
- },
37
34
  "wireit": {
38
35
  "### THESE WIREIT CONFIGS ARE GENERATED ####": {},
39
36
  "### DO NOT MODIFY THESE MANUALLY ####": {},
@@ -286,7 +283,7 @@
286
283
  ]
287
284
  },
288
285
  "jest": {
289
- "command": "nps jest:pkg",
286
+ "command": "jest --passWithNoTests",
290
287
  "files": [
291
288
  "src/**/*.test.*",
292
289
  "../../.babelrc.js",
@@ -295,9 +292,14 @@
295
292
  ],
296
293
  "output": [],
297
294
  "dependencies": [
298
- "build:lib:cjs",
299
- "../victory-vendor:build:lib:cjs",
300
- "../victory-voronoi:build:lib:cjs"
295
+ "../victory-brush-container:build",
296
+ "../victory-core:build",
297
+ "../victory-cursor-container:build",
298
+ "../victory-selection-container:build",
299
+ "../victory-voronoi-container:build",
300
+ "../victory-zoom-container:build",
301
+ "../victory-vendor:build",
302
+ "../victory-voronoi:build"
301
303
  ],
302
304
  "packageLocks": [
303
305
  "pnpm-lock.yaml"
@@ -0,0 +1,169 @@
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
+ );
package/LICENSE.txt DELETED
@@ -1,30 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2015-2020 Formidable Labs.
4
-
5
- Copyright (c) 2016-2020 Alexey Svetliakov <https://github.com/asvetliakov>,
6
- snerks <https://github.com/snerks>, Krzysztof Cebula <https://github.com
7
- Havret>, Vitaliy Polyanskiy <https://github.com/alreadyExisted>, James Lismore
8
- <https://github.com/jlismore>, Stack Builders <https://github.com
9
- stackbuilders>, Esteban Ibarra <https://github.com/ibarrae>, Dominic Lee
10
- <https://github.com/dominictwlee>, Dave Vedder <https://github.com
11
- veddermatic>, Alec Flett <https://github.com/alecf> and potentially other
12
- DefinitelyTyped contributors.
13
-
14
- Permission is hereby granted, free of charge, to any person obtaining a copy
15
- of this software and associated documentation files (the "Software"), to deal
16
- in the Software without restriction, including without limitation the rights
17
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
- copies of the Software, and to permit persons to whom the Software is
19
- furnished to do so, subject to the following conditions:
20
-
21
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
- SOFTWARE.
28
-
29
- The above copyright notice and this permission notice shall be included in all
30
- copies or substantial portions of the Software.
@@ -1,181 +0,0 @@
1
- import React from "react";
2
- import {
3
- toPairs,
4
- groupBy,
5
- forOwn,
6
- includes,
7
- flow,
8
- isEmpty,
9
- isFunction,
10
- } from "lodash";
11
- import { VictoryContainer, Log } from "victory-core";
12
- import { voronoiContainerMixin } from "victory-voronoi-container";
13
- import { zoomContainerMixin } from "victory-zoom-container";
14
- import { selectionContainerMixin } from "victory-selection-container";
15
- import { brushContainerMixin } from "victory-brush-container";
16
- import { cursorContainerMixin } from "victory-cursor-container";
17
-
18
- export type ContainerType =
19
- | "brush"
20
- | "cursor"
21
- | "selection"
22
- | "voronoi"
23
- | "zoom";
24
-
25
- type MixinFunction = (...args: any[]) => any;
26
-
27
- function ensureArray<T>(thing: T): [] | T | T[] {
28
- if (!thing) {
29
- return [];
30
- } else if (!Array.isArray(thing)) {
31
- return [thing];
32
- }
33
- return thing;
34
- }
35
-
36
- const combineEventHandlers = (eventHandlersArray: any[]) => {
37
- // takes an array of event handler objects and produces one eventHandlers object
38
- // creates a custom combinedHandler() for events with multiple conflicting handlers
39
- return eventHandlersArray.reduce((localHandlers, finalHandlers) => {
40
- forOwn(localHandlers, (localHandler, eventName) => {
41
- const existingHandler = finalHandlers[eventName];
42
- if (existingHandler) {
43
- // create new handler for event that concats the existing handler's mutations with new ones
44
- finalHandlers[eventName] = function combinedHandler(...params) {
45
- // named for debug clarity
46
- // sometimes handlers return undefined; use empty array instead, for concat()
47
- const existingMutations = ensureArray(existingHandler(...params));
48
- const localMutations = ensureArray(localHandler(...params));
49
- return existingMutations.concat(localMutations);
50
- };
51
- } else {
52
- finalHandlers[eventName] = localHandler;
53
- }
54
- });
55
- return finalHandlers;
56
- });
57
- };
58
-
59
- const combineDefaultEvents = (defaultEvents: any[]) => {
60
- // takes a defaultEvents array and returns one equal or lesser length,
61
- // by combining any events that have the same target
62
- const eventsByTarget = groupBy(defaultEvents, "target");
63
- const events = toPairs(eventsByTarget).map(([target, eventsArray]) => {
64
- const newEventsArray = eventsArray.filter(Boolean);
65
- return isEmpty(newEventsArray)
66
- ? null
67
- : {
68
- target,
69
- eventHandlers: combineEventHandlers(
70
- eventsArray.map((event) => event.eventHandlers),
71
- ),
72
- // note: does not currently handle eventKey or childName
73
- };
74
- });
75
- return events.filter(Boolean);
76
- };
77
-
78
- export const combineContainerMixins = (
79
- mixins: MixinFunction[],
80
- Container: React.ComponentType<any>,
81
- ) => {
82
- // similar to Object.assign(A, B), this function will decide conflicts in favor mixinB.
83
- // this applies to propTypes and defaultProps.
84
- // getChildren will call A's getChildren() and pass the resulting children to B's.
85
- // defaultEvents attempts to resolve any conflicts between A and B's defaultEvents.
86
- const Classes = mixins.map((mixin) => mixin(Container));
87
- const instances = Classes.map((Class) => new Class());
88
- const NaiveCombinedContainer = flow(mixins)(Container);
89
-
90
- const displayType = Classes.map((Class) => {
91
- const match = Class.displayName.match(/Victory(.*)Container/);
92
- return match[1] || "";
93
- }).join("");
94
-
95
- return class VictoryCombinedContainer extends NaiveCombinedContainer {
96
- static displayName = `Victory${displayType}Container`;
97
-
98
- static propTypes = Classes.reduce(
99
- (propTypes, Class) => ({ ...propTypes, ...Class.propTypes }),
100
- {},
101
- );
102
-
103
- static defaultProps = Classes.reduce(
104
- (defaultProps, Class) => ({ ...defaultProps, ...Class.defaultProps }),
105
- {},
106
- );
107
-
108
- static defaultEvents = (props) => {
109
- return combineDefaultEvents(
110
- Classes.reduce((defaultEvents, Class) => {
111
- const events = isFunction(Class.defaultEvents)
112
- ? Class.defaultEvents(props)
113
- : Class.defaultEvents;
114
- return [...defaultEvents, ...events];
115
- }, []),
116
- );
117
- };
118
-
119
- getChildren(props) {
120
- return instances.reduce(
121
- (children, instance) => instance.getChildren({ ...props, children }),
122
- props.children,
123
- );
124
- }
125
- };
126
- };
127
-
128
- const checkBehaviorName = (
129
- behavior: ContainerType,
130
- behaviors: ContainerType[],
131
- ) => {
132
- if (behavior && !includes(behaviors, behavior)) {
133
- Log.warn(
134
- `"${behavior}" is not a valid behavior. Choose from [${behaviors.join(
135
- ", ",
136
- )}].`,
137
- );
138
- }
139
- };
140
-
141
- export const makeCreateContainerFunction =
142
- (
143
- mixinMap: Record<ContainerType, MixinFunction[]>,
144
- Container: React.ComponentType<any>,
145
- ) =>
146
- (
147
- behaviorA: ContainerType,
148
- behaviorB: ContainerType,
149
- ...invalid: ContainerType[]
150
- ) => {
151
- const behaviors = Object.keys(mixinMap) as ContainerType[];
152
-
153
- checkBehaviorName(behaviorA, behaviors);
154
- checkBehaviorName(behaviorB, behaviors);
155
-
156
- if (invalid.length) {
157
- Log.warn(
158
- "too many arguments given to createContainer (maximum accepted: 2).",
159
- );
160
- }
161
-
162
- const firstMixins = mixinMap[behaviorA];
163
- const secondMixins = mixinMap[behaviorB] || [];
164
-
165
- if (!firstMixins) {
166
- return Container;
167
- }
168
-
169
- return combineContainerMixins([...firstMixins, ...secondMixins], Container);
170
- };
171
-
172
- export const createContainer = makeCreateContainerFunction(
173
- {
174
- zoom: [zoomContainerMixin],
175
- voronoi: [voronoiContainerMixin],
176
- selection: [selectionContainerMixin],
177
- cursor: [cursorContainerMixin],
178
- brush: [brushContainerMixin],
179
- },
180
- VictoryContainer,
181
- );