victory-group 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.
@@ -6,10 +6,11 @@ import {
6
6
  VictoryContainer,
7
7
  VictoryTheme,
8
8
  CommonProps,
9
- Wrapper
9
+ Wrapper,
10
+ Hooks
10
11
  } from "victory-core";
11
12
  import { VictorySharedEvents } from "victory-shared-events";
12
- import { getChildren, getCalculatedProps } from "./helper-methods";
13
+ import { getChildren, useMemoizedProps } from "./helper-methods";
13
14
  import isEqual from "react-fast-compare";
14
15
 
15
16
  const fallbackProps = {
@@ -19,150 +20,163 @@ const fallbackProps = {
19
20
  offset: 0
20
21
  };
21
22
 
22
- export default class VictoryGroup extends React.Component {
23
- static displayName = "VictoryGroup";
24
-
25
- static role = "group";
26
-
27
- static propTypes = {
28
- ...CommonProps.baseProps,
29
- ...CommonProps.dataProps,
30
- children: PropTypes.oneOfType([
31
- PropTypes.arrayOf(PropTypes.node),
32
- PropTypes.node
33
- ]),
34
- color: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
35
- colorScale: PropTypes.oneOfType([
36
- PropTypes.arrayOf(PropTypes.string),
37
- PropTypes.oneOf([
38
- "grayscale",
39
- "qualitative",
40
- "heatmap",
41
- "warm",
42
- "cool",
43
- "red",
44
- "green",
45
- "blue"
46
- ])
47
- ]),
48
- horizontal: PropTypes.bool,
49
- offset: PropTypes.number
50
- };
51
-
52
- static defaultProps = {
53
- containerComponent: <VictoryContainer />,
54
- groupComponent: <g />,
55
- samples: 50,
56
- sortOrder: "ascending",
57
- standalone: true,
58
- theme: VictoryTheme.grayscale
59
- };
60
-
61
- static expectedComponents = [
62
- "groupComponent",
63
- "containerComponent",
64
- "labelComponent"
65
- ];
66
-
67
- static getChildren = getChildren;
68
-
69
- constructor(props) {
70
- super(props);
71
- if (props.animate) {
72
- this.state = {
73
- nodesShouldLoad: false,
74
- nodesDoneLoad: false,
75
- animating: true
76
- };
77
- this.setAnimationState = Wrapper.setAnimationState.bind(this);
78
- }
79
- }
23
+ const VictoryGroup = (initialProps) => {
24
+ // eslint-disable-next-line no-use-before-define
25
+ const { role } = VictoryGroupMemo;
26
+ const { getAnimationProps, setAnimationState, getProps } =
27
+ Hooks.useAnimationState();
28
+ const props = getProps(initialProps);
80
29
 
81
- shouldComponentUpdate(nextProps) {
82
- if (this.props.animate) {
83
- if (!isEqual(this.props, nextProps)) {
84
- this.setAnimationState(this.props, nextProps);
85
- return false;
86
- }
87
- }
88
- return true;
89
- }
30
+ const modifiedProps = Helpers.modifyProps(props, fallbackProps, role);
31
+ const {
32
+ eventKey,
33
+ containerComponent,
34
+ standalone,
35
+ groupComponent,
36
+ externalEventMutations,
37
+ width,
38
+ height,
39
+ theme,
40
+ polar,
41
+ horizontal,
42
+ name
43
+ } = modifiedProps;
90
44
 
91
- getNewChildren(props, childComponents, calculatedProps) {
45
+ const childComponents = React.Children.toArray(modifiedProps.children);
46
+ const calculatedProps = useMemoizedProps(modifiedProps);
47
+ const { domain, scale, style, origin } = calculatedProps;
48
+
49
+ const newChildren = React.useMemo(() => {
92
50
  const children = getChildren(props, childComponents, calculatedProps);
93
- const getAnimationProps = Wrapper.getAnimationProps.bind(this);
94
51
  return children.map((child, index) => {
95
52
  const childProps = assign(
96
- { animate: getAnimationProps(props, child, index) },
53
+ { animate: getAnimationProps(props, child, index, "victory-group") },
97
54
  child.props
98
55
  );
99
56
  return React.cloneElement(child, childProps);
100
57
  });
101
- }
58
+ }, [props, childComponents, calculatedProps, getAnimationProps]);
102
59
 
103
- renderContainer(containerComponent, props) {
104
- const containerProps = defaults({}, containerComponent.props, props);
105
- return React.cloneElement(containerComponent, containerProps);
106
- }
60
+ const containerProps = React.useMemo(() => {
61
+ if (standalone) {
62
+ return {
63
+ domain,
64
+ scale,
65
+ width,
66
+ height,
67
+ standalone,
68
+ theme,
69
+ style: style.parent,
70
+ horizontal,
71
+ polar,
72
+ origin,
73
+ name
74
+ };
75
+ }
76
+ return {};
77
+ }, [
78
+ standalone,
79
+ domain,
80
+ scale,
81
+ width,
82
+ height,
83
+ theme,
84
+ style,
85
+ horizontal,
86
+ polar,
87
+ origin,
88
+ name
89
+ ]);
90
+
91
+ const container = React.useMemo(() => {
92
+ if (standalone) {
93
+ const defaultContainerProps = defaults(
94
+ {},
95
+ containerComponent.props,
96
+ containerProps
97
+ );
98
+ return React.cloneElement(containerComponent, defaultContainerProps);
99
+ }
100
+ return groupComponent;
101
+ }, [groupComponent, standalone, containerComponent, containerProps]);
102
+
103
+ const events = React.useMemo(() => {
104
+ return Wrapper.getAllEvents(props);
105
+ }, [props]);
107
106
 
108
- getContainerProps(props, calculatedProps) {
109
- const { width, height, standalone, theme, polar, horizontal, name } = props;
110
- const { domain, scale, style, origin } = calculatedProps;
111
- return {
112
- domain,
113
- scale,
114
- width,
115
- height,
116
- standalone,
117
- theme,
118
- style: style.parent,
119
- horizontal,
120
- polar,
121
- origin,
122
- name
107
+ const previousProps = Hooks.usePreviousProps(initialProps);
108
+
109
+ React.useEffect(() => {
110
+ // This is called before dismount to keep state in sync
111
+ return () => {
112
+ if (initialProps.animate) {
113
+ setAnimationState(previousProps, props);
114
+ }
123
115
  };
124
- }
116
+ }, [setAnimationState, previousProps, initialProps, props]);
125
117
 
126
- render() {
127
- const { role } = this.constructor;
128
- const props =
129
- this.state && this.state.nodesWillExit
130
- ? this.state.oldProps || this.props
131
- : this.props;
132
- const modifiedProps = Helpers.modifyProps(props, fallbackProps, role);
133
- const {
134
- eventKey,
135
- containerComponent,
136
- standalone,
137
- groupComponent,
138
- externalEventMutations
139
- } = modifiedProps;
140
- const childComponents = React.Children.toArray(modifiedProps.children);
141
- const calculatedProps = getCalculatedProps(modifiedProps, childComponents);
142
- const newChildren = this.getNewChildren(
143
- modifiedProps,
144
- childComponents,
145
- calculatedProps
118
+ if (!isEmpty(events)) {
119
+ return (
120
+ <VictorySharedEvents
121
+ container={container}
122
+ eventKey={eventKey}
123
+ events={events}
124
+ externalEventMutations={externalEventMutations}
125
+ >
126
+ {newChildren}
127
+ </VictorySharedEvents>
146
128
  );
147
- const containerProps = standalone
148
- ? this.getContainerProps(modifiedProps, calculatedProps)
149
- : {};
150
- const container = standalone
151
- ? this.renderContainer(containerComponent, containerProps)
152
- : groupComponent;
153
- const events = Wrapper.getAllEvents(props);
154
- if (!isEmpty(events)) {
155
- return (
156
- <VictorySharedEvents
157
- container={container}
158
- eventKey={eventKey}
159
- events={events}
160
- externalEventMutations={externalEventMutations}
161
- >
162
- {newChildren}
163
- </VictorySharedEvents>
164
- );
165
- }
166
- return React.cloneElement(container, container.props, newChildren);
167
129
  }
168
- }
130
+ return React.cloneElement(container, container.props, newChildren);
131
+ };
132
+
133
+ VictoryGroup.propTypes = {
134
+ ...CommonProps.baseProps,
135
+ ...CommonProps.dataProps,
136
+ children: PropTypes.oneOfType([
137
+ PropTypes.arrayOf(PropTypes.node),
138
+ PropTypes.node
139
+ ]),
140
+ color: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
141
+ colorScale: PropTypes.oneOfType([
142
+ PropTypes.arrayOf(PropTypes.string),
143
+ PropTypes.oneOf([
144
+ "grayscale",
145
+ "qualitative",
146
+ "heatmap",
147
+ "warm",
148
+ "cool",
149
+ "red",
150
+ "green",
151
+ "blue"
152
+ ])
153
+ ]),
154
+ horizontal: PropTypes.bool,
155
+ offset: PropTypes.number
156
+ };
157
+
158
+ VictoryGroup.defaultProps = {
159
+ containerComponent: <VictoryContainer />,
160
+ groupComponent: <g />,
161
+ samples: 50,
162
+ sortOrder: "ascending",
163
+ standalone: true,
164
+ theme: VictoryTheme.grayscale
165
+ };
166
+
167
+ // We need to attatch the static properties to the memoized version, or else
168
+ // VictoryChart will not be able to get this component's role type
169
+ const VictoryGroupMemo = React.memo(VictoryGroup, isEqual);
170
+
171
+ VictoryGroupMemo.displayName = "VictoryGroup";
172
+ VictoryGroupMemo.role = "group";
173
+
174
+ VictoryGroupMemo.expectedComponents = [
175
+ "groupComponent",
176
+ "containerComponent",
177
+ "labelComponent"
178
+ ];
179
+
180
+ VictoryGroupMemo.getChildren = getChildren;
181
+
182
+ export default VictoryGroupMemo;