victory-cursor-container 37.0.2 → 37.1.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.
@@ -3,159 +3,176 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.VictoryCursorContainer = void 0;
7
- exports.cursorContainerMixin = cursorContainerMixin;
6
+ exports.useVictoryCursorContainer = exports.VictoryCursorContainer = exports.VICTORY_CURSOR_CONTAINER_DEFAULT_PROPS = void 0;
8
7
  var _isObject2 = _interopRequireDefault(require("lodash/isObject"));
9
8
  var _defaults2 = _interopRequireDefault(require("lodash/defaults"));
10
9
  var _react = _interopRequireDefault(require("react"));
11
10
  var _victoryCore = require("victory-core");
12
11
  var _cursorHelpers = require("./cursor-helpers");
13
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
- function cursorContainerMixin(Base) {
15
- // @ts-expect-error "TS2545: A mixin class must have a constructor with a single rest parameter of type 'any[]'."
16
- return class VictoryCursorContainer extends Base {
17
- static displayName = "VictoryCursorContainer";
18
- static defaultProps = {
19
- ..._victoryCore.VictoryContainer.defaultProps,
20
- cursorLabelComponent: /*#__PURE__*/_react.default.createElement(_victoryCore.VictoryLabel, null),
21
- cursorLabelOffset: {
22
- x: 5,
23
- y: -10
24
- },
25
- cursorComponent: /*#__PURE__*/_react.default.createElement(_victoryCore.LineSegment, null)
26
- };
27
- static defaultEvents(props) {
28
- return [{
29
- target: "parent",
30
- eventHandlers: {
31
- onMouseLeave: (evt, targetProps) => {
32
- return props.disable ? {} : _cursorHelpers.CursorHelpers.onMouseLeave(evt, targetProps);
33
- },
34
- onTouchCancel: () => {
35
- return [];
36
- },
37
- onMouseMove: (evt, targetProps) => {
38
- return props.disable ? {} : _cursorHelpers.CursorHelpers.onMouseMove(evt, targetProps);
39
- },
40
- onTouchMove: (evt, targetProps) => {
41
- return props.disable ? {} : _cursorHelpers.CursorHelpers.onMouseMove(evt, targetProps);
42
- }
43
- }
44
- }];
13
+ /* eslint-disable complexity */
14
+
15
+ const VICTORY_CURSOR_CONTAINER_DEFAULT_PROPS = exports.VICTORY_CURSOR_CONTAINER_DEFAULT_PROPS = {
16
+ cursorLabelComponent: /*#__PURE__*/_react.default.createElement(_victoryCore.VictoryLabel, null),
17
+ cursorLabelOffset: {
18
+ x: 5,
19
+ y: -10
20
+ },
21
+ cursorComponent: /*#__PURE__*/_react.default.createElement(_victoryCore.LineSegment, null)
22
+ };
23
+ const useVictoryCursorContainer = initialProps => {
24
+ const props = {
25
+ ...VICTORY_CURSOR_CONTAINER_DEFAULT_PROPS,
26
+ ...initialProps
27
+ };
28
+ const {
29
+ children
30
+ } = props;
31
+ const getCursorPosition = () => {
32
+ const {
33
+ cursorValue,
34
+ defaultCursorValue,
35
+ domain,
36
+ cursorDimension
37
+ } = props;
38
+ if (cursorValue) {
39
+ return cursorValue;
45
40
  }
46
- getCursorPosition(props) {
47
- const {
48
- cursorValue,
49
- defaultCursorValue,
50
- domain,
51
- cursorDimension
52
- } = props;
53
- if (cursorValue) {
54
- return cursorValue;
55
- }
56
- if (typeof defaultCursorValue === "number") {
57
- return {
58
- x: (domain.x[0] + domain.x[1]) / 2,
59
- y: (domain.y[0] + domain.y[1]) / 2,
41
+ if (typeof defaultCursorValue === "number") {
42
+ return {
43
+ x: (domain.x[0] + domain.x[1]) / 2,
44
+ y: (domain.y[0] + domain.y[1]) / 2,
45
+ ...(cursorDimension ? {
60
46
  [cursorDimension]: defaultCursorValue
61
- };
62
- }
63
- return defaultCursorValue;
47
+ } : {})
48
+ };
64
49
  }
65
- getCursorLabelOffset(props) {
66
- const {
67
- cursorLabelOffset
68
- } = props;
69
- if (typeof cursorLabelOffset === "number") {
70
- return {
71
- x: cursorLabelOffset,
72
- y: cursorLabelOffset
73
- };
74
- }
75
- return cursorLabelOffset;
50
+ return defaultCursorValue;
51
+ };
52
+ const getCursorLabelOffset = () => {
53
+ const {
54
+ cursorLabelOffset
55
+ } = props;
56
+ if (typeof cursorLabelOffset === "number") {
57
+ return {
58
+ x: cursorLabelOffset,
59
+ y: cursorLabelOffset
60
+ };
76
61
  }
77
- getPadding(props) {
78
- if (props.padding === undefined) {
79
- const child = props.children.find(c => {
80
- return (0, _isObject2.default)(c.props) && c.props.padding !== undefined;
81
- });
82
- return _victoryCore.Helpers.getPadding(child.props);
83
- }
84
- return _victoryCore.Helpers.getPadding(props);
62
+ return cursorLabelOffset;
63
+ };
64
+ const getPadding = () => {
65
+ if (props.padding === undefined) {
66
+ const child = Array.isArray(props.children) ? props.children.find(c => {
67
+ return (0, _isObject2.default)(c.props) && c.props.padding !== undefined;
68
+ }) : props.children;
69
+ return _victoryCore.Helpers.getPadding(child?.props);
85
70
  }
86
- getCursorElements(props) {
87
- // eslint-disable-line max-statements
88
- const {
89
- scale,
90
- cursorLabelComponent,
91
- cursorLabel,
92
- cursorComponent,
93
- width,
94
- height,
95
- name,
96
- horizontal,
97
- theme
98
- } = props;
99
- const cursorDimension = _cursorHelpers.CursorHelpers.getDimension(props);
100
- const cursorValue = this.getCursorPosition(props);
101
- const cursorLabelOffset = this.getCursorLabelOffset(props);
102
- if (!cursorValue) {
103
- return [];
104
- }
105
- const newElements = [];
106
- const padding = this.getPadding(props);
107
- const cursorCoordinates = {
108
- x: horizontal ? scale.y(cursorValue.y) : scale.x(cursorValue.x),
109
- y: horizontal ? scale.x(cursorValue.x) : scale.y(cursorValue.y)
110
- };
111
- if (cursorLabel) {
112
- let labelProps = (0, _defaults2.default)({
113
- active: true
114
- }, cursorLabelComponent.props, {
115
- x: cursorCoordinates.x + cursorLabelOffset.x,
116
- y: cursorCoordinates.y + cursorLabelOffset.y,
117
- datum: cursorValue,
118
- active: true,
119
- key: `${name}-cursor-label`
120
- });
121
- if (_victoryCore.Helpers.isTooltip(cursorLabelComponent)) {
122
- const tooltipTheme = theme && theme.tooltip || {};
123
- labelProps = (0, _defaults2.default)({}, labelProps, tooltipTheme);
124
- }
125
- newElements.push( /*#__PURE__*/_react.default.cloneElement(cursorLabelComponent, (0, _defaults2.default)({}, labelProps, {
126
- text: _victoryCore.Helpers.evaluateProp(cursorLabel, labelProps)
127
- })));
128
- }
129
- const cursorStyle = Object.assign({
130
- stroke: "black"
131
- }, cursorComponent.props.style);
132
- if (cursorDimension === "x" || cursorDimension === undefined) {
133
- newElements.push( /*#__PURE__*/_react.default.cloneElement(cursorComponent, {
134
- key: `${name}-x-cursor`,
135
- x1: cursorCoordinates.x,
136
- x2: cursorCoordinates.x,
137
- y1: padding.top,
138
- y2: height - padding.bottom,
139
- style: cursorStyle
140
- }));
141
- }
142
- if (cursorDimension === "y" || cursorDimension === undefined) {
143
- newElements.push( /*#__PURE__*/_react.default.cloneElement(cursorComponent, {
144
- key: `${name}-y-cursor`,
145
- x1: padding.left,
146
- x2: width - padding.right,
147
- y1: cursorCoordinates.y,
148
- y2: cursorCoordinates.y,
149
- style: cursorStyle
150
- }));
71
+ return _victoryCore.Helpers.getPadding(props);
72
+ };
73
+ const getCursorElements = () => {
74
+ // eslint-disable-line max-statements
75
+ const {
76
+ scale,
77
+ cursorLabelComponent,
78
+ cursorLabel,
79
+ cursorComponent,
80
+ width,
81
+ height,
82
+ name,
83
+ horizontal,
84
+ theme
85
+ } = props;
86
+ const cursorDimension = _cursorHelpers.CursorHelpers.getDimension(props);
87
+ const cursorValue = getCursorPosition();
88
+ const cursorLabelOffset = getCursorLabelOffset();
89
+ if (!cursorValue) {
90
+ return [];
91
+ }
92
+ const newElements = [];
93
+ const padding = getPadding();
94
+ const cursorCoordinates = scale && "x" in scale && "y" in scale && typeof scale.y === "function" && typeof scale.x === "function" ? {
95
+ x: horizontal ? scale.y(cursorValue.y) : scale.x(cursorValue.x),
96
+ y: horizontal ? scale.x(cursorValue.x) : scale.y(cursorValue.y)
97
+ } : {
98
+ x: cursorValue.x,
99
+ y: cursorValue.y
100
+ };
101
+ if (cursorLabel) {
102
+ let labelProps = (0, _defaults2.default)({
103
+ active: true
104
+ }, cursorLabelComponent.props, {
105
+ x: cursorCoordinates.x + cursorLabelOffset.x,
106
+ y: cursorCoordinates.y + cursorLabelOffset.y,
107
+ datum: cursorValue,
108
+ active: true,
109
+ key: `${name}-cursor-label`
110
+ });
111
+ if (_victoryCore.Helpers.isTooltip(cursorLabelComponent)) {
112
+ const tooltipTheme = theme && theme.tooltip || {};
113
+ labelProps = (0, _defaults2.default)({}, labelProps, tooltipTheme);
151
114
  }
152
- return newElements;
115
+ newElements.push( /*#__PURE__*/_react.default.cloneElement(cursorLabelComponent, (0, _defaults2.default)({}, labelProps, {
116
+ text: _victoryCore.Helpers.evaluateProp(cursorLabel, labelProps)
117
+ })));
153
118
  }
154
-
155
- // Overrides method in VictoryContainer
156
- getChildren(props) {
157
- return [..._react.default.Children.toArray(props.children), ...this.getCursorElements(props)];
119
+ const cursorStyle = Object.assign({
120
+ stroke: "black"
121
+ }, cursorComponent.props.style);
122
+ if (cursorDimension === "x" || cursorDimension === undefined) {
123
+ newElements.push( /*#__PURE__*/_react.default.cloneElement(cursorComponent, {
124
+ key: `${name}-x-cursor`,
125
+ x1: cursorCoordinates.x,
126
+ x2: cursorCoordinates.x,
127
+ y1: padding.top,
128
+ y2: (typeof height === "number" ? height : 0) - padding.bottom,
129
+ style: cursorStyle
130
+ }));
131
+ }
132
+ if (cursorDimension === "y" || cursorDimension === undefined) {
133
+ newElements.push( /*#__PURE__*/_react.default.cloneElement(cursorComponent, {
134
+ key: `${name}-y-cursor`,
135
+ x1: padding.left,
136
+ x2: (typeof width === "number" ? width : 0) - padding.right,
137
+ y1: cursorCoordinates.y,
138
+ y2: cursorCoordinates.y,
139
+ style: cursorStyle
140
+ }));
158
141
  }
142
+ return newElements;
143
+ };
144
+ return {
145
+ props,
146
+ children: [..._react.default.Children.toArray(children), ...getCursorElements()]
159
147
  };
160
- }
161
- const VictoryCursorContainer = exports.VictoryCursorContainer = cursorContainerMixin(_victoryCore.VictoryContainer);
148
+ };
149
+ exports.useVictoryCursorContainer = useVictoryCursorContainer;
150
+ const VictoryCursorContainer = initialProps => {
151
+ const {
152
+ props,
153
+ children
154
+ } = useVictoryCursorContainer(initialProps);
155
+ return /*#__PURE__*/_react.default.createElement(_victoryCore.VictoryContainer, props, children);
156
+ };
157
+ exports.VictoryCursorContainer = VictoryCursorContainer;
158
+ VictoryCursorContainer.role = "container";
159
+ VictoryCursorContainer.defaultEvents = initialProps => {
160
+ const props = {
161
+ ...VICTORY_CURSOR_CONTAINER_DEFAULT_PROPS,
162
+ ...initialProps
163
+ };
164
+ const createEventHandler = (handler, disabled) =>
165
+ // eslint-disable-next-line max-params
166
+ (event, targetProps, eventKey, context) => disabled || props.disable ? {} : handler(event, {
167
+ ...props,
168
+ ...targetProps
169
+ }, eventKey, context);
170
+ return [{
171
+ target: "parent",
172
+ eventHandlers: {
173
+ onMouseLeave: createEventHandler(_cursorHelpers.CursorHelpers.onMouseLeave),
174
+ onMouseMove: createEventHandler(_cursorHelpers.CursorHelpers.onMouseMove),
175
+ onTouchMove: createEventHandler(_cursorHelpers.CursorHelpers.onMouseMove)
176
+ }
177
+ }];
178
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "victory-cursor-container",
3
- "version": "37.0.2",
3
+ "version": "37.1.0",
4
4
  "description": "Interactive Cursor Component for Victory",
5
5
  "keywords": [
6
6
  "data visualization",
@@ -21,7 +21,7 @@
21
21
  "license": "MIT",
22
22
  "dependencies": {
23
23
  "lodash": "^4.17.19",
24
- "victory-core": "^37.0.2"
24
+ "victory-core": "37.1.0"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "react": ">=16.6.0"