victory-stack 35.11.4 → 36.2.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.
@@ -29,101 +29,64 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
29
29
 
30
30
  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; }
31
31
 
32
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
33
-
34
- 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); } }
35
-
36
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
37
-
38
- function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
39
-
40
- 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, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
41
-
42
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
43
-
44
32
  var fallbackProps = {
45
33
  width: 450,
46
34
  height: 300,
47
35
  padding: 50
48
36
  };
49
37
 
50
- var VictoryStack =
51
- /*#__PURE__*/
52
- function (_React$Component) {
53
- _inherits(VictoryStack, _React$Component);
54
-
55
- function VictoryStack(props) {
56
- var _this;
57
-
58
- _classCallCheck(this, VictoryStack);
59
-
60
- _this = _possibleConstructorReturn(this, (VictoryStack.__proto__ || Object.getPrototypeOf(VictoryStack)).call(this, props));
61
-
62
- if (props.animate) {
63
- _this.state = {
64
- nodesShouldLoad: false,
65
- nodesDoneLoad: false,
66
- animating: true
67
- };
68
- _this.setAnimationState = _victoryCore.Wrapper.setAnimationState.bind(_assertThisInitialized(_this));
69
- }
70
-
71
- return _this;
72
- }
73
-
74
- _createClass(VictoryStack, [{
75
- key: "shouldComponentUpdate",
76
- value: function shouldComponentUpdate(nextProps) {
77
- if (this.props.animate) {
78
- if (!(0, _reactFastCompare.default)(this.props, nextProps)) {
79
- this.setAnimationState(this.props, nextProps);
80
- return false;
81
- }
82
- }
83
-
84
- return true;
85
- }
86
- }, {
87
- key: "getNewChildren",
88
- value: function getNewChildren(props, childComponents, calculatedProps) {
89
- var children = (0, _helperMethods.getChildren)(props, childComponents, calculatedProps);
90
-
91
- var getAnimationProps = _victoryCore.Wrapper.getAnimationProps.bind(this);
92
-
93
- var newChildren = children.map(function (child, index) {
94
- var childProps = (0, _assign2.default)({
95
- animate: getAnimationProps(props, child, index)
96
- }, child.props);
97
- return _react.default.cloneElement(child, childProps);
98
- });
99
- /*
100
- reverse render order for children of `VictoryStack` so that higher children in the stack
101
- are rendered behind lower children. This looks nicer for stacked bars with cornerRadius, and
102
- areas with strokes
103
- */
104
-
105
- return newChildren.reverse();
106
- }
107
- }, {
108
- key: "renderContainer",
109
- value: function renderContainer(containerComponent, props) {
110
- var containerProps = (0, _defaults2.default)({}, containerComponent.props, props);
111
- return _react.default.cloneElement(containerComponent, containerProps);
112
- }
113
- }, {
114
- key: "getContainerProps",
115
- value: function getContainerProps(props, calculatedProps) {
116
- var width = props.width,
117
- height = props.height,
118
- standalone = props.standalone,
119
- theme = props.theme,
120
- polar = props.polar,
121
- horizontal = props.horizontal,
122
- name = props.name;
123
- var domain = calculatedProps.domain,
124
- scale = calculatedProps.scale,
125
- style = calculatedProps.style,
126
- origin = calculatedProps.origin;
38
+ var VictoryStack = function (initialProps) {
39
+ // eslint-disable-next-line no-use-before-define
40
+ var role = VictoryStackMemo.role;
41
+
42
+ var _Hooks$useAnimationSt = _victoryCore.Hooks.useAnimationState(),
43
+ setAnimationState = _Hooks$useAnimationSt.setAnimationState,
44
+ getAnimationProps = _Hooks$useAnimationSt.getAnimationProps,
45
+ getProps = _Hooks$useAnimationSt.getProps;
46
+
47
+ var props = getProps(initialProps);
48
+
49
+ var modifiedProps = _victoryCore.Helpers.modifyProps(props, fallbackProps, role);
50
+
51
+ var eventKey = modifiedProps.eventKey,
52
+ containerComponent = modifiedProps.containerComponent,
53
+ standalone = modifiedProps.standalone,
54
+ groupComponent = modifiedProps.groupComponent,
55
+ externalEventMutations = modifiedProps.externalEventMutations,
56
+ width = modifiedProps.width,
57
+ height = modifiedProps.height,
58
+ theme = modifiedProps.theme,
59
+ polar = modifiedProps.polar,
60
+ horizontal = modifiedProps.horizontal,
61
+ name = modifiedProps.name;
62
+
63
+ var childComponents = _react.default.Children.toArray(modifiedProps.children);
64
+
65
+ var calculatedProps = (0, _helperMethods.useMemoizedProps)(modifiedProps);
66
+ var domain = calculatedProps.domain,
67
+ scale = calculatedProps.scale,
68
+ style = calculatedProps.style,
69
+ origin = calculatedProps.origin;
70
+
71
+ var newChildren = _react.default.useMemo(function () {
72
+ var children = (0, _helperMethods.getChildren)(props, childComponents, calculatedProps);
73
+ var orderedChildren = children.map(function (child, index) {
74
+ var childProps = (0, _assign2.default)({
75
+ animate: getAnimationProps(props, child, index, "victory-stack")
76
+ }, child.props);
77
+ return _react.default.cloneElement(child, childProps);
78
+ });
79
+ /*
80
+ reverse render order for children of `VictoryStack` so that higher children in the stack
81
+ are rendered behind lower children. This looks nicer for stacked bars with cornerRadius, and
82
+ areas with strokes
83
+ */
84
+
85
+ return orderedChildren.reverse();
86
+ }, [props, childComponents, calculatedProps, getAnimationProps]);
87
+
88
+ var containerProps = _react.default.useMemo(function () {
89
+ if (standalone) {
127
90
  return {
128
91
  domain: domain,
129
92
  scale: scale,
@@ -138,103 +101,78 @@ function (_React$Component) {
138
101
  name: name
139
102
  };
140
103
  }
141
- }, {
142
- key: "render",
143
- value: function render() {
144
- var role = this.constructor.role;
145
- var props = this.state && this.state.nodesWillExit ? this.state.oldProps || this.props : this.props;
146
-
147
- var modifiedProps = _victoryCore.Helpers.modifyProps(props, fallbackProps, role);
148
-
149
- var eventKey = modifiedProps.eventKey,
150
- containerComponent = modifiedProps.containerComponent,
151
- standalone = modifiedProps.standalone,
152
- groupComponent = modifiedProps.groupComponent,
153
- externalEventMutations = modifiedProps.externalEventMutations;
154
-
155
- var childComponents = _react.default.Children.toArray(modifiedProps.children);
156
-
157
- var calculatedProps = (0, _helperMethods.getCalculatedProps)(modifiedProps, childComponents);
158
- var newChildren = this.getNewChildren(modifiedProps, childComponents, calculatedProps);
159
- var containerProps = standalone ? this.getContainerProps(modifiedProps, calculatedProps) : {};
160
- var container = standalone ? this.renderContainer(containerComponent, containerProps) : groupComponent;
161
-
162
- var events = _victoryCore.Wrapper.getAllEvents(props);
163
-
164
- if (!(0, _isEmpty2.default)(events)) {
165
- return _react.default.createElement(_victorySharedEvents.VictorySharedEvents, {
166
- container: container,
167
- eventKey: eventKey,
168
- events: events,
169
- externalEventMutations: externalEventMutations
170
- }, newChildren);
171
- }
172
104
 
173
- return _react.default.cloneElement(container, container.props, newChildren);
105
+ return {};
106
+ }, [standalone, domain, scale, width, height, theme, style, horizontal, polar, origin, name]);
107
+
108
+ var container = _react.default.useMemo(function () {
109
+ if (standalone) {
110
+ var defaultContainerProps = (0, _defaults2.default)({}, containerComponent.props, containerProps);
111
+ return _react.default.cloneElement(containerComponent, defaultContainerProps);
174
112
  }
175
- }]);
176
113
 
177
- return VictoryStack;
178
- }(_react.default.Component);
114
+ return groupComponent;
115
+ }, [groupComponent, standalone, containerComponent, containerProps]);
179
116
 
180
- exports.default = VictoryStack;
181
- Object.defineProperty(VictoryStack, "displayName", {
182
- configurable: true,
183
- enumerable: true,
184
- writable: true,
185
- value: "VictoryStack"
186
- });
187
- Object.defineProperty(VictoryStack, "role", {
188
- configurable: true,
189
- enumerable: true,
190
- writable: true,
191
- value: "stack"
192
- });
193
- Object.defineProperty(VictoryStack, "propTypes", {
194
- configurable: true,
195
- enumerable: true,
196
- writable: true,
197
- value: _objectSpread({}, _victoryCore.CommonProps.baseProps, {
198
- bins: _propTypes.default.oneOfType([_propTypes.default.arrayOf(_propTypes.default.oneOfType([_victoryCore.PropTypes.nonNegative, _propTypes.default.instanceOf(Date)])), _victoryCore.PropTypes.nonNegative]),
199
- categories: _propTypes.default.oneOfType([_propTypes.default.arrayOf(_propTypes.default.string), _propTypes.default.shape({
200
- x: _propTypes.default.arrayOf(_propTypes.default.string),
201
- y: _propTypes.default.arrayOf(_propTypes.default.string)
202
- })]),
203
- children: _propTypes.default.oneOfType([_propTypes.default.arrayOf(_propTypes.default.node), _propTypes.default.node]),
204
- colorScale: _propTypes.default.oneOfType([_propTypes.default.arrayOf(_propTypes.default.string), _propTypes.default.oneOf(["grayscale", "qualitative", "heatmap", "warm", "cool", "red", "green", "blue"])]),
205
- fillInMissingData: _propTypes.default.bool,
206
- horizontal: _propTypes.default.bool,
207
- labelComponent: _propTypes.default.element,
208
- labels: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.array]),
209
- style: _propTypes.default.shape({
210
- parent: _propTypes.default.object,
211
- data: _propTypes.default.object,
212
- labels: _propTypes.default.object
213
- }),
214
- xOffset: _propTypes.default.number
215
- })
216
- });
217
- Object.defineProperty(VictoryStack, "defaultProps", {
218
- configurable: true,
219
- enumerable: true,
220
- writable: true,
221
- value: {
222
- containerComponent: _react.default.createElement(_victoryCore.VictoryContainer, null),
223
- groupComponent: _react.default.createElement("g", null),
224
- standalone: true,
225
- theme: _victoryCore.VictoryTheme.grayscale,
226
- fillInMissingData: true
117
+ var events = _react.default.useMemo(function () {
118
+ return _victoryCore.Wrapper.getAllEvents(props);
119
+ }, [props]);
120
+
121
+ var previousProps = _victoryCore.Hooks.usePreviousProps(initialProps);
122
+
123
+ _react.default.useEffect(function () {
124
+ // This is called before dismount to keep state in sync
125
+ return function () {
126
+ if (initialProps.animate) {
127
+ setAnimationState(previousProps, initialProps);
128
+ }
129
+ };
130
+ }, [setAnimationState, previousProps, initialProps]);
131
+
132
+ if (!(0, _isEmpty2.default)(events)) {
133
+ return _react.default.createElement(_victorySharedEvents.VictorySharedEvents, {
134
+ container: container,
135
+ eventKey: eventKey,
136
+ events: events,
137
+ externalEventMutations: externalEventMutations
138
+ }, newChildren);
227
139
  }
140
+
141
+ return _react.default.cloneElement(container, container.props, newChildren);
142
+ };
143
+
144
+ VictoryStack.propTypes = _objectSpread({}, _victoryCore.CommonProps.baseProps, {
145
+ bins: _propTypes.default.oneOfType([_propTypes.default.arrayOf(_propTypes.default.oneOfType([_victoryCore.PropTypes.nonNegative, _propTypes.default.instanceOf(Date)])), _victoryCore.PropTypes.nonNegative]),
146
+ categories: _propTypes.default.oneOfType([_propTypes.default.arrayOf(_propTypes.default.string), _propTypes.default.shape({
147
+ x: _propTypes.default.arrayOf(_propTypes.default.string),
148
+ y: _propTypes.default.arrayOf(_propTypes.default.string)
149
+ })]),
150
+ children: _propTypes.default.oneOfType([_propTypes.default.arrayOf(_propTypes.default.node), _propTypes.default.node]),
151
+ colorScale: _propTypes.default.oneOfType([_propTypes.default.arrayOf(_propTypes.default.string), _propTypes.default.oneOf(["grayscale", "qualitative", "heatmap", "warm", "cool", "red", "green", "blue"])]),
152
+ fillInMissingData: _propTypes.default.bool,
153
+ horizontal: _propTypes.default.bool,
154
+ labelComponent: _propTypes.default.element,
155
+ labels: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.array]),
156
+ style: _propTypes.default.shape({
157
+ parent: _propTypes.default.object,
158
+ data: _propTypes.default.object,
159
+ labels: _propTypes.default.object
160
+ }),
161
+ xOffset: _propTypes.default.number
228
162
  });
229
- Object.defineProperty(VictoryStack, "expectedComponents", {
230
- configurable: true,
231
- enumerable: true,
232
- writable: true,
233
- value: ["groupComponent", "containerComponent", "labelComponent"]
234
- });
235
- Object.defineProperty(VictoryStack, "getChildren", {
236
- configurable: true,
237
- enumerable: true,
238
- writable: true,
239
- value: _helperMethods.getChildren
240
- });
163
+ VictoryStack.defaultProps = {
164
+ containerComponent: _react.default.createElement(_victoryCore.VictoryContainer, null),
165
+ groupComponent: _react.default.createElement("g", null),
166
+ standalone: true,
167
+ theme: _victoryCore.VictoryTheme.grayscale,
168
+ fillInMissingData: true
169
+ };
170
+
171
+ var VictoryStackMemo = _react.default.memo(VictoryStack, _reactFastCompare.default);
172
+
173
+ VictoryStackMemo.displayName = "VictoryStack";
174
+ VictoryStackMemo.role = "stack";
175
+ VictoryStackMemo.expectedComponents = ["groupComponent", "containerComponent", "labelComponent"];
176
+ VictoryStackMemo.getChildren = _helperMethods.getChildren;
177
+ var _default = VictoryStackMemo;
178
+ exports.default = _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "victory-stack",
3
- "version": "35.11.4",
3
+ "version": "36.2.1",
4
4
  "description": "Stack Layout Component for Victory",
5
5
  "keywords": [
6
6
  "data visualization",
@@ -22,8 +22,8 @@
22
22
  "lodash": "^4.17.19",
23
23
  "prop-types": "^15.5.8",
24
24
  "react-fast-compare": "^2.0.0",
25
- "victory-core": "^35.11.4",
26
- "victory-shared-events": "^35.11.4"
25
+ "victory-core": "^36.2.1",
26
+ "victory-shared-events": "^36.2.1"
27
27
  },
28
28
  "peerDependencies": {
29
29
  "react": "^16.6.0 || ^17.0.0"
@@ -31,6 +31,5 @@
31
31
  "scripts": {
32
32
  "version": "yarn run nps build-libs && nps build-dists"
33
33
  },
34
- "sideEffects": false,
35
- "gitHead": "59ec44a7be4b1aea88df1a6769ed16f0a60689df"
34
+ "sideEffects": false
36
35
  }
@@ -3,6 +3,7 @@
3
3
  import { assign, keys, orderBy } from "lodash";
4
4
  import React from "react";
5
5
  import { Helpers, Scale, Wrapper } from "victory-core";
6
+ import isEqual from "react-fast-compare";
6
7
 
7
8
  const fallbackProps = {
8
9
  width: 450,
@@ -160,6 +161,37 @@ export function getCalculatedProps(props, childComponents) {
160
161
  };
161
162
  }
162
163
 
164
+ // We need to remove sharedEvents in order to memoize the calculated data
165
+ // With shared events, the props change on every event, and every value is re-calculated
166
+ const withoutSharedEvents = (props) => {
167
+ const { children } = props;
168
+ const modifiedChildren = React.Children.toArray(children).map((child) => {
169
+ return {
170
+ ...child,
171
+ props: Helpers.omit(child.props, ["sharedEvents"])
172
+ };
173
+ });
174
+ props.children = modifiedChildren;
175
+ return props;
176
+ };
177
+
178
+ export function useMemoizedProps(initialProps) {
179
+ const modifiedProps = withoutSharedEvents(initialProps);
180
+ const [props, setProps] = React.useState(modifiedProps);
181
+
182
+ // React.memo uses shallow equality to compare objects. This way props
183
+ // will only be re-calculated when they change.
184
+ React.useEffect(() => {
185
+ if (!isEqual(modifiedProps, props)) {
186
+ setProps(modifiedProps);
187
+ }
188
+ }, [props, setProps, modifiedProps]);
189
+
190
+ return React.useMemo(() => {
191
+ return getCalculatedProps(props, props.children);
192
+ }, [props]);
193
+ }
194
+
163
195
  function getLabels(props, datasets, index) {
164
196
  if (!props.labels) {
165
197
  return undefined;