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