victory-cursor-container 36.9.2-next.1 → 36.9.2

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,5 +1,6 @@
1
1
  import React from "react";
2
2
  import {
3
+ VictoryContainer,
3
4
  Helpers,
4
5
  VictoryContainerProps,
5
6
  CoordinatesPropType,
@@ -7,10 +8,6 @@ import {
7
8
  ValueOrAccessor,
8
9
  VictoryLabel,
9
10
  LineSegment,
10
- VictoryContainer,
11
- VictoryEventHandler,
12
- DomainTuple,
13
- PaddingProps,
14
11
  } from "victory-core";
15
12
  import { defaults, isObject } from "lodash";
16
13
  import { CursorHelpers } from "./cursor-helpers";
@@ -25,198 +22,191 @@ export interface VictoryCursorContainerProps extends VictoryContainerProps {
25
22
  cursorLabelOffset?: CursorCoordinatesPropType;
26
23
  defaultCursorValue?: CursorCoordinatesPropType;
27
24
  disable?: boolean;
28
- horizontal?: boolean;
29
- padding?: PaddingProps;
30
25
  onCursorChange?: (
31
26
  value: CursorCoordinatesPropType,
32
27
  props: VictoryCursorContainerProps,
33
28
  ) => void;
34
29
  }
35
30
 
36
- interface VictoryCursorContainerMutatedProps
37
- extends VictoryCursorContainerProps {
38
- cursorValue: CoordinatesPropType | null;
39
- domain: { x: DomainTuple; y: DomainTuple };
40
- }
31
+ type ComponentClass<TProps> = { new (props: TProps): React.Component<TProps> };
32
+
33
+ export function cursorContainerMixin<
34
+ TBase extends ComponentClass<TProps>,
35
+ TProps extends VictoryCursorContainerProps,
36
+ >(Base: TBase) {
37
+ // @ts-expect-error "TS2545: A mixin class must have a constructor with a single rest parameter of type 'any[]'."
38
+ return class VictoryCursorContainer extends Base {
39
+ static displayName = "VictoryCursorContainer";
40
+ static defaultProps = {
41
+ ...VictoryContainer.defaultProps,
42
+ cursorLabelComponent: <VictoryLabel />,
43
+ cursorLabelOffset: {
44
+ x: 5,
45
+ y: -10,
46
+ },
47
+ cursorComponent: <LineSegment />,
48
+ };
49
+ static defaultEvents = (props) => {
50
+ return [
51
+ {
52
+ target: "parent",
53
+ eventHandlers: {
54
+ onMouseLeave: (evt, targetProps) => {
55
+ return props.disable
56
+ ? {}
57
+ : CursorHelpers.onMouseLeave(evt, targetProps);
58
+ },
59
+ onTouchCancel: () => {
60
+ return [];
61
+ },
62
+ onMouseMove: (evt, targetProps) => {
63
+ return props.disable
64
+ ? {}
65
+ : CursorHelpers.onMouseMove(evt, targetProps);
66
+ },
67
+ onTouchMove: (evt, targetProps) => {
68
+ return props.disable
69
+ ? {}
70
+ : CursorHelpers.onMouseMove(evt, targetProps);
71
+ },
72
+ },
73
+ },
74
+ ];
75
+ };
41
76
 
42
- export const VICTORY_CURSOR_CONTAINER_DEFAULT_PROPS = {
43
- cursorLabelComponent: <VictoryLabel />,
44
- cursorLabelOffset: {
45
- x: 5,
46
- y: -10,
47
- },
48
- cursorComponent: <LineSegment />,
49
- };
50
-
51
- export const useVictoryCursorContainer = (
52
- initialProps: VictoryCursorContainerProps,
53
- ) => {
54
- const props = {
55
- ...VICTORY_CURSOR_CONTAINER_DEFAULT_PROPS,
56
- ...(initialProps as VictoryCursorContainerMutatedProps),
57
- };
58
- const { children } = props;
77
+ getCursorPosition(props) {
78
+ const { cursorValue, defaultCursorValue, domain, cursorDimension } =
79
+ props;
80
+ if (cursorValue) {
81
+ return cursorValue;
82
+ }
59
83
 
60
- const getCursorPosition = () => {
61
- const { cursorValue, defaultCursorValue, domain, cursorDimension } = props;
62
- if (cursorValue) {
63
- return cursorValue;
64
- }
84
+ if (typeof defaultCursorValue === "number") {
85
+ return {
86
+ x: (domain.x[0] + domain.x[1]) / 2,
87
+ y: (domain.y[0] + domain.y[1]) / 2,
88
+ [cursorDimension]: defaultCursorValue,
89
+ };
90
+ }
65
91
 
66
- if (typeof defaultCursorValue === "number") {
67
- return {
68
- x: ((domain.x[0] as number) + (domain.x[1] as number)) / 2,
69
- y: ((domain.y[0] as number) + (domain.y[1] as number)) / 2,
70
- ...(cursorDimension ? { [cursorDimension]: defaultCursorValue } : {}),
71
- };
92
+ return defaultCursorValue;
72
93
  }
73
94
 
74
- return defaultCursorValue;
75
- };
95
+ getCursorLabelOffset(props) {
96
+ const { cursorLabelOffset } = props;
76
97
 
77
- const getCursorLabelOffset = () => {
78
- const { cursorLabelOffset } = props;
98
+ if (typeof cursorLabelOffset === "number") {
99
+ return {
100
+ x: cursorLabelOffset,
101
+ y: cursorLabelOffset,
102
+ };
103
+ }
79
104
 
80
- if (typeof cursorLabelOffset === "number") {
81
- return {
82
- x: cursorLabelOffset,
83
- y: cursorLabelOffset,
84
- };
105
+ return cursorLabelOffset;
85
106
  }
86
107
 
87
- return cursorLabelOffset;
88
- };
89
-
90
- const getPadding = () => {
91
- if (props.padding === undefined) {
92
- const child = Array.isArray(props.children)
93
- ? props.children.find((c: any) => {
94
- return isObject(c.props) && c.props.padding !== undefined;
95
- })
96
- : props.children;
97
- return Helpers.getPadding(child?.props);
108
+ getPadding(props) {
109
+ if (props.padding === undefined) {
110
+ const child = props.children.find((c) => {
111
+ return isObject(c.props) && c.props.padding !== undefined;
112
+ });
113
+ return Helpers.getPadding(child.props);
114
+ }
115
+ return Helpers.getPadding(props);
98
116
  }
99
- return Helpers.getPadding(props);
100
- };
101
117
 
102
- const getCursorElements = () => {
103
- // eslint-disable-line max-statements
104
- const {
105
- scale,
106
- cursorLabelComponent,
107
- cursorLabel,
108
- cursorComponent,
109
- width,
110
- height,
111
- name,
112
- horizontal,
113
- theme,
114
- } = props;
115
- const cursorDimension = CursorHelpers.getDimension(props);
116
- const cursorValue = getCursorPosition();
117
- const cursorLabelOffset = getCursorLabelOffset();
118
-
119
- if (!cursorValue) {
120
- return [];
121
- }
118
+ getCursorElements(props) {
119
+ // eslint-disable-line max-statements
120
+ const {
121
+ scale,
122
+ cursorLabelComponent,
123
+ cursorLabel,
124
+ cursorComponent,
125
+ width,
126
+ height,
127
+ name,
128
+ horizontal,
129
+ theme,
130
+ } = props;
131
+ const cursorDimension = CursorHelpers.getDimension(props);
132
+ const cursorValue = this.getCursorPosition(props);
133
+ const cursorLabelOffset = this.getCursorLabelOffset(props);
134
+
135
+ if (!cursorValue) {
136
+ return [];
137
+ }
122
138
 
123
- const newElements: React.ReactElement[] = [];
124
- const padding = getPadding();
125
- const cursorCoordinates = {
126
- x: horizontal ? scale?.y?.(cursorValue.y) : scale?.x?.(cursorValue.x),
127
- y: horizontal ? scale?.x?.(cursorValue.x) : scale?.y?.(cursorValue.y),
128
- };
129
- if (cursorLabel) {
130
- let labelProps = defaults({ active: true }, cursorLabelComponent.props, {
131
- x: cursorCoordinates.x || 0 + cursorLabelOffset.x,
132
- y: cursorCoordinates.y || 0 + cursorLabelOffset.y,
133
- datum: cursorValue,
134
- active: true,
135
- key: `${name}-cursor-label`,
136
- });
137
- if (Helpers.isTooltip(cursorLabelComponent)) {
138
- const tooltipTheme = (theme && theme.tooltip) || {};
139
- labelProps = defaults({}, labelProps, tooltipTheme);
139
+ const newElements: React.ReactElement[] = [];
140
+ const padding = this.getPadding(props);
141
+ const cursorCoordinates = {
142
+ x: horizontal ? scale.y(cursorValue.y) : scale.x(cursorValue.x),
143
+ y: horizontal ? scale.x(cursorValue.x) : scale.y(cursorValue.y),
144
+ };
145
+ if (cursorLabel) {
146
+ let labelProps = defaults(
147
+ { active: true },
148
+ cursorLabelComponent.props,
149
+ {
150
+ x: cursorCoordinates.x + cursorLabelOffset.x,
151
+ y: cursorCoordinates.y + cursorLabelOffset.y,
152
+ datum: cursorValue,
153
+ active: true,
154
+ key: `${name}-cursor-label`,
155
+ },
156
+ );
157
+ if (Helpers.isTooltip(cursorLabelComponent)) {
158
+ const tooltipTheme = (theme && theme.tooltip) || {};
159
+ labelProps = defaults({}, labelProps, tooltipTheme);
160
+ }
161
+ newElements.push(
162
+ React.cloneElement(
163
+ cursorLabelComponent,
164
+ defaults({}, labelProps, {
165
+ text: Helpers.evaluateProp(cursorLabel, labelProps),
166
+ }),
167
+ ),
168
+ );
140
169
  }
141
- newElements.push(
142
- React.cloneElement(
143
- cursorLabelComponent,
144
- defaults({}, labelProps, {
145
- text: Helpers.evaluateProp(cursorLabel, labelProps),
146
- }),
147
- ),
148
- );
149
- }
150
170
 
151
- const cursorStyle = Object.assign(
152
- { stroke: "black" },
153
- cursorComponent.props.style,
154
- );
155
- if (cursorDimension === "x" || cursorDimension === undefined) {
156
- newElements.push(
157
- React.cloneElement(cursorComponent, {
158
- key: `${name}-x-cursor`,
159
- x1: cursorCoordinates.x,
160
- x2: cursorCoordinates.x,
161
- y1: padding.top,
162
- y2: height || 0 - padding.bottom,
163
- style: cursorStyle,
164
- }),
171
+ const cursorStyle = Object.assign(
172
+ { stroke: "black" },
173
+ cursorComponent.props.style,
165
174
  );
175
+ if (cursorDimension === "x" || cursorDimension === undefined) {
176
+ newElements.push(
177
+ React.cloneElement(cursorComponent, {
178
+ key: `${name}-x-cursor`,
179
+ x1: cursorCoordinates.x,
180
+ x2: cursorCoordinates.x,
181
+ y1: padding.top,
182
+ y2: height - padding.bottom,
183
+ style: cursorStyle,
184
+ }),
185
+ );
186
+ }
187
+ if (cursorDimension === "y" || cursorDimension === undefined) {
188
+ newElements.push(
189
+ React.cloneElement(cursorComponent, {
190
+ key: `${name}-y-cursor`,
191
+ x1: padding.left,
192
+ x2: width - padding.right,
193
+ y1: cursorCoordinates.y,
194
+ y2: cursorCoordinates.y,
195
+ style: cursorStyle,
196
+ }),
197
+ );
198
+ }
199
+ return newElements;
166
200
  }
167
- if (cursorDimension === "y" || cursorDimension === undefined) {
168
- newElements.push(
169
- React.cloneElement(cursorComponent, {
170
- key: `${name}-y-cursor`,
171
- x1: padding.left,
172
- x2: width || 0 - padding.right,
173
- y1: cursorCoordinates.y,
174
- y2: cursorCoordinates.y,
175
- style: cursorStyle,
176
- }),
177
- );
201
+
202
+ // Overrides method in VictoryContainer
203
+ getChildren(props) {
204
+ return [
205
+ ...React.Children.toArray(props.children),
206
+ ...this.getCursorElements(props),
207
+ ];
178
208
  }
179
- return newElements;
180
209
  };
210
+ }
181
211
 
182
- return {
183
- props,
184
- children: [
185
- ...React.Children.toArray(children),
186
- ...getCursorElements(),
187
- ] as React.ReactElement[],
188
- };
189
- };
190
-
191
- export const VictoryCursorContainer = (
192
- initialProps: VictoryCursorContainerProps,
193
- ) => {
194
- const { props, children } = useVictoryCursorContainer(initialProps);
195
- return <VictoryContainer {...props}>{children}</VictoryContainer>;
196
- };
197
-
198
- VictoryCursorContainer.role = "container";
199
-
200
- VictoryCursorContainer.defaultEvents = (
201
- initialProps: VictoryCursorContainerProps,
202
- ) => {
203
- const props = { ...VICTORY_CURSOR_CONTAINER_DEFAULT_PROPS, ...initialProps };
204
- const createEventHandler =
205
- (handler: VictoryEventHandler, disabled?: boolean): VictoryEventHandler =>
206
- // eslint-disable-next-line max-params
207
- (event, targetProps, eventKey, context) =>
208
- disabled || props.disable
209
- ? {}
210
- : handler(event, { ...props, ...targetProps }, eventKey, context);
211
-
212
- return [
213
- {
214
- target: "parent",
215
- eventHandlers: {
216
- onMouseLeave: createEventHandler(CursorHelpers.onMouseLeave),
217
- onMouseMove: createEventHandler(CursorHelpers.onMouseMove),
218
- onTouchMove: createEventHandler(CursorHelpers.onMouseMove),
219
- },
220
- },
221
- ];
222
- };
212
+ export const VictoryCursorContainer = cursorContainerMixin(VictoryContainer);
@@ -1,19 +0,0 @@
1
- /**
2
- * @license React
3
- * react-dom.production.min.js
4
- *
5
- * Copyright (c) Facebook, Inc. and its affiliates.
6
- *
7
- * This source code is licensed under the MIT license found in the
8
- * LICENSE file in the root directory of this source tree.
9
- */
10
-
11
- /**
12
- * @license React
13
- * scheduler.production.min.js
14
- *
15
- * Copyright (c) Facebook, Inc. and its affiliates.
16
- *
17
- * This source code is licensed under the MIT license found in the
18
- * LICENSE file in the root directory of this source tree.
19
- */