victory-stack 35.11.3 → 36.2.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.
@@ -7,10 +7,11 @@ import {
7
7
  VictoryTheme,
8
8
  CommonProps,
9
9
  Wrapper,
10
- PropTypes as CustomPropTypes
10
+ PropTypes as CustomPropTypes,
11
+ Hooks
11
12
  } from "victory-core";
12
13
  import { VictorySharedEvents } from "victory-shared-events";
13
- import { getChildren, getCalculatedProps } from "./helper-methods";
14
+ import { getChildren, useMemoizedProps } from "./helper-methods";
14
15
  import isEqual from "react-fast-compare";
15
16
 
16
17
  const fallbackProps = {
@@ -19,102 +20,38 @@ const fallbackProps = {
19
20
  padding: 50
20
21
  };
21
22
 
22
- export default class VictoryStack extends React.Component {
23
- static displayName = "VictoryStack";
24
-
25
- static role = "stack";
26
-
27
- static propTypes = {
28
- ...CommonProps.baseProps,
29
- bins: PropTypes.oneOfType([
30
- PropTypes.arrayOf(
31
- PropTypes.oneOfType([
32
- CustomPropTypes.nonNegative,
33
- PropTypes.instanceOf(Date)
34
- ])
35
- ),
36
- CustomPropTypes.nonNegative
37
- ]),
38
- categories: PropTypes.oneOfType([
39
- PropTypes.arrayOf(PropTypes.string),
40
- PropTypes.shape({
41
- x: PropTypes.arrayOf(PropTypes.string),
42
- y: PropTypes.arrayOf(PropTypes.string)
43
- })
44
- ]),
45
- children: PropTypes.oneOfType([
46
- PropTypes.arrayOf(PropTypes.node),
47
- PropTypes.node
48
- ]),
49
- colorScale: PropTypes.oneOfType([
50
- PropTypes.arrayOf(PropTypes.string),
51
- PropTypes.oneOf([
52
- "grayscale",
53
- "qualitative",
54
- "heatmap",
55
- "warm",
56
- "cool",
57
- "red",
58
- "green",
59
- "blue"
60
- ])
61
- ]),
62
- fillInMissingData: PropTypes.bool,
63
- horizontal: PropTypes.bool,
64
- labelComponent: PropTypes.element,
65
- labels: PropTypes.oneOfType([PropTypes.func, PropTypes.array]),
66
- style: PropTypes.shape({
67
- parent: PropTypes.object,
68
- data: PropTypes.object,
69
- labels: PropTypes.object
70
- }),
71
- xOffset: PropTypes.number
72
- };
73
-
74
- static defaultProps = {
75
- containerComponent: <VictoryContainer />,
76
- groupComponent: <g />,
77
- standalone: true,
78
- theme: VictoryTheme.grayscale,
79
- fillInMissingData: true
80
- };
81
-
82
- static expectedComponents = [
83
- "groupComponent",
84
- "containerComponent",
85
- "labelComponent"
86
- ];
87
-
88
- static getChildren = getChildren;
89
-
90
- constructor(props) {
91
- super(props);
92
- if (props.animate) {
93
- this.state = {
94
- nodesShouldLoad: false,
95
- nodesDoneLoad: false,
96
- animating: true
97
- };
98
- this.setAnimationState = Wrapper.setAnimationState.bind(this);
99
- }
100
- }
23
+ const VictoryStack = (initialProps) => {
24
+ // eslint-disable-next-line no-use-before-define
25
+ const { role } = VictoryStackMemo;
26
+ const { setAnimationState, getAnimationProps, getProps } =
27
+ Hooks.useAnimationState();
101
28
 
102
- shouldComponentUpdate(nextProps) {
103
- if (this.props.animate) {
104
- if (!isEqual(this.props, nextProps)) {
105
- this.setAnimationState(this.props, nextProps);
106
- return false;
107
- }
108
- }
109
- return true;
110
- }
29
+ const props = getProps(initialProps);
30
+
31
+ const modifiedProps = Helpers.modifyProps(props, fallbackProps, role);
32
+ const {
33
+ eventKey,
34
+ containerComponent,
35
+ standalone,
36
+ groupComponent,
37
+ externalEventMutations,
38
+ width,
39
+ height,
40
+ theme,
41
+ polar,
42
+ horizontal,
43
+ name
44
+ } = modifiedProps;
45
+
46
+ const childComponents = React.Children.toArray(modifiedProps.children);
47
+ const calculatedProps = useMemoizedProps(modifiedProps);
48
+ const { domain, scale, style, origin } = calculatedProps;
111
49
 
112
- getNewChildren(props, childComponents, calculatedProps) {
50
+ const newChildren = React.useMemo(() => {
113
51
  const children = getChildren(props, childComponents, calculatedProps);
114
- const getAnimationProps = Wrapper.getAnimationProps.bind(this);
115
- const newChildren = children.map((child, index) => {
52
+ const orderedChildren = children.map((child, index) => {
116
53
  const childProps = assign(
117
- { animate: getAnimationProps(props, child, index) },
54
+ { animate: getAnimationProps(props, child, index, "victory-stack") },
118
55
  child.props
119
56
  );
120
57
  return React.cloneElement(child, childProps);
@@ -124,73 +61,149 @@ export default class VictoryStack extends React.Component {
124
61
  are rendered behind lower children. This looks nicer for stacked bars with cornerRadius, and
125
62
  areas with strokes
126
63
  */
127
- return newChildren.reverse();
128
- }
129
-
130
- renderContainer(containerComponent, props) {
131
- const containerProps = defaults({}, containerComponent.props, props);
132
- return React.cloneElement(containerComponent, containerProps);
133
- }
64
+ return orderedChildren.reverse();
65
+ }, [props, childComponents, calculatedProps, getAnimationProps]);
134
66
 
135
- getContainerProps(props, calculatedProps) {
136
- const { width, height, standalone, theme, polar, horizontal, name } = props;
137
- const { domain, scale, style, origin } = calculatedProps;
138
- return {
139
- domain,
140
- scale,
141
- width,
142
- height,
143
- standalone,
144
- theme,
145
- style: style.parent,
146
- horizontal,
147
- polar,
148
- origin,
149
- name
150
- };
151
- }
67
+ const containerProps = React.useMemo(() => {
68
+ if (standalone) {
69
+ return {
70
+ domain,
71
+ scale,
72
+ width,
73
+ height,
74
+ standalone,
75
+ theme,
76
+ style: style.parent,
77
+ horizontal,
78
+ polar,
79
+ origin,
80
+ name
81
+ };
82
+ }
83
+ return {};
84
+ }, [
85
+ standalone,
86
+ domain,
87
+ scale,
88
+ width,
89
+ height,
90
+ theme,
91
+ style,
92
+ horizontal,
93
+ polar,
94
+ origin,
95
+ name
96
+ ]);
152
97
 
153
- render() {
154
- const { role } = this.constructor;
155
- const props =
156
- this.state && this.state.nodesWillExit
157
- ? this.state.oldProps || this.props
158
- : this.props;
159
- const modifiedProps = Helpers.modifyProps(props, fallbackProps, role);
160
- const {
161
- eventKey,
162
- containerComponent,
163
- standalone,
164
- groupComponent,
165
- externalEventMutations
166
- } = modifiedProps;
167
- const childComponents = React.Children.toArray(modifiedProps.children);
168
- const calculatedProps = getCalculatedProps(modifiedProps, childComponents);
169
- const newChildren = this.getNewChildren(
170
- modifiedProps,
171
- childComponents,
172
- calculatedProps
173
- );
174
- const containerProps = standalone
175
- ? this.getContainerProps(modifiedProps, calculatedProps)
176
- : {};
177
- const container = standalone
178
- ? this.renderContainer(containerComponent, containerProps)
179
- : groupComponent;
180
- const events = Wrapper.getAllEvents(props);
181
- if (!isEmpty(events)) {
182
- return (
183
- <VictorySharedEvents
184
- container={container}
185
- eventKey={eventKey}
186
- events={events}
187
- externalEventMutations={externalEventMutations}
188
- >
189
- {newChildren}
190
- </VictorySharedEvents>
98
+ const container = React.useMemo(() => {
99
+ if (standalone) {
100
+ const defaultContainerProps = defaults(
101
+ {},
102
+ containerComponent.props,
103
+ containerProps
191
104
  );
105
+ return React.cloneElement(containerComponent, defaultContainerProps);
192
106
  }
107
+ return groupComponent;
108
+ }, [groupComponent, standalone, containerComponent, containerProps]);
109
+
110
+ const events = React.useMemo(() => {
111
+ return Wrapper.getAllEvents(props);
112
+ }, [props]);
193
113
 
194
- return React.cloneElement(container, container.props, newChildren);
114
+ const previousProps = Hooks.usePreviousProps(initialProps);
115
+
116
+ React.useEffect(() => {
117
+ // This is called before dismount to keep state in sync
118
+ return () => {
119
+ if (initialProps.animate) {
120
+ setAnimationState(previousProps, initialProps);
121
+ }
122
+ };
123
+ }, [setAnimationState, previousProps, initialProps]);
124
+
125
+ if (!isEmpty(events)) {
126
+ return (
127
+ <VictorySharedEvents
128
+ container={container}
129
+ eventKey={eventKey}
130
+ events={events}
131
+ externalEventMutations={externalEventMutations}
132
+ >
133
+ {newChildren}
134
+ </VictorySharedEvents>
135
+ );
195
136
  }
196
- }
137
+
138
+ return React.cloneElement(container, container.props, newChildren);
139
+ };
140
+
141
+ VictoryStack.propTypes = {
142
+ ...CommonProps.baseProps,
143
+ bins: PropTypes.oneOfType([
144
+ PropTypes.arrayOf(
145
+ PropTypes.oneOfType([
146
+ CustomPropTypes.nonNegative,
147
+ PropTypes.instanceOf(Date)
148
+ ])
149
+ ),
150
+ CustomPropTypes.nonNegative
151
+ ]),
152
+ categories: PropTypes.oneOfType([
153
+ PropTypes.arrayOf(PropTypes.string),
154
+ PropTypes.shape({
155
+ x: PropTypes.arrayOf(PropTypes.string),
156
+ y: PropTypes.arrayOf(PropTypes.string)
157
+ })
158
+ ]),
159
+ children: PropTypes.oneOfType([
160
+ PropTypes.arrayOf(PropTypes.node),
161
+ PropTypes.node
162
+ ]),
163
+ colorScale: PropTypes.oneOfType([
164
+ PropTypes.arrayOf(PropTypes.string),
165
+ PropTypes.oneOf([
166
+ "grayscale",
167
+ "qualitative",
168
+ "heatmap",
169
+ "warm",
170
+ "cool",
171
+ "red",
172
+ "green",
173
+ "blue"
174
+ ])
175
+ ]),
176
+ fillInMissingData: PropTypes.bool,
177
+ horizontal: PropTypes.bool,
178
+ labelComponent: PropTypes.element,
179
+ labels: PropTypes.oneOfType([PropTypes.func, PropTypes.array]),
180
+ style: PropTypes.shape({
181
+ parent: PropTypes.object,
182
+ data: PropTypes.object,
183
+ labels: PropTypes.object
184
+ }),
185
+ xOffset: PropTypes.number
186
+ };
187
+
188
+ VictoryStack.defaultProps = {
189
+ containerComponent: <VictoryContainer />,
190
+ groupComponent: <g />,
191
+ standalone: true,
192
+ theme: VictoryTheme.grayscale,
193
+ fillInMissingData: true
194
+ };
195
+
196
+ const VictoryStackMemo = React.memo(VictoryStack, isEqual);
197
+
198
+ VictoryStackMemo.displayName = "VictoryStack";
199
+ VictoryStackMemo.role = "stack";
200
+
201
+ VictoryStackMemo.expectedComponents = [
202
+ "groupComponent",
203
+ "containerComponent",
204
+ "labelComponent"
205
+ ];
206
+
207
+ VictoryStackMemo.getChildren = getChildren;
208
+
209
+ export default VictoryStackMemo;