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.
@@ -1,153 +1,168 @@
1
1
  import _isObject from "lodash/isObject";
2
2
  import _defaults from "lodash/defaults";
3
+ /* eslint-disable complexity */
3
4
  import React from "react";
4
- import { VictoryContainer, Helpers, VictoryLabel, LineSegment } from "victory-core";
5
+ import { Helpers, VictoryLabel, LineSegment, VictoryContainer } from "victory-core";
5
6
  import { CursorHelpers } from "./cursor-helpers";
6
- export function cursorContainerMixin(Base) {
7
- // @ts-expect-error "TS2545: A mixin class must have a constructor with a single rest parameter of type 'any[]'."
8
- return class VictoryCursorContainer extends Base {
9
- static displayName = "VictoryCursorContainer";
10
- static defaultProps = {
11
- ...VictoryContainer.defaultProps,
12
- cursorLabelComponent: /*#__PURE__*/React.createElement(VictoryLabel, null),
13
- cursorLabelOffset: {
14
- x: 5,
15
- y: -10
16
- },
17
- cursorComponent: /*#__PURE__*/React.createElement(LineSegment, null)
18
- };
19
- static defaultEvents(props) {
20
- return [{
21
- target: "parent",
22
- eventHandlers: {
23
- onMouseLeave: (evt, targetProps) => {
24
- return props.disable ? {} : CursorHelpers.onMouseLeave(evt, targetProps);
25
- },
26
- onTouchCancel: () => {
27
- return [];
28
- },
29
- onMouseMove: (evt, targetProps) => {
30
- return props.disable ? {} : CursorHelpers.onMouseMove(evt, targetProps);
31
- },
32
- onTouchMove: (evt, targetProps) => {
33
- return props.disable ? {} : CursorHelpers.onMouseMove(evt, targetProps);
34
- }
35
- }
36
- }];
7
+ export const VICTORY_CURSOR_CONTAINER_DEFAULT_PROPS = {
8
+ cursorLabelComponent: /*#__PURE__*/React.createElement(VictoryLabel, null),
9
+ cursorLabelOffset: {
10
+ x: 5,
11
+ y: -10
12
+ },
13
+ cursorComponent: /*#__PURE__*/React.createElement(LineSegment, null)
14
+ };
15
+ export const useVictoryCursorContainer = initialProps => {
16
+ const props = {
17
+ ...VICTORY_CURSOR_CONTAINER_DEFAULT_PROPS,
18
+ ...initialProps
19
+ };
20
+ const {
21
+ children
22
+ } = props;
23
+ const getCursorPosition = () => {
24
+ const {
25
+ cursorValue,
26
+ defaultCursorValue,
27
+ domain,
28
+ cursorDimension
29
+ } = props;
30
+ if (cursorValue) {
31
+ return cursorValue;
37
32
  }
38
- getCursorPosition(props) {
39
- const {
40
- cursorValue,
41
- defaultCursorValue,
42
- domain,
43
- cursorDimension
44
- } = props;
45
- if (cursorValue) {
46
- return cursorValue;
47
- }
48
- if (typeof defaultCursorValue === "number") {
49
- return {
50
- x: (domain.x[0] + domain.x[1]) / 2,
51
- y: (domain.y[0] + domain.y[1]) / 2,
33
+ if (typeof defaultCursorValue === "number") {
34
+ return {
35
+ x: (domain.x[0] + domain.x[1]) / 2,
36
+ y: (domain.y[0] + domain.y[1]) / 2,
37
+ ...(cursorDimension ? {
52
38
  [cursorDimension]: defaultCursorValue
53
- };
54
- }
55
- return defaultCursorValue;
39
+ } : {})
40
+ };
56
41
  }
57
- getCursorLabelOffset(props) {
58
- const {
59
- cursorLabelOffset
60
- } = props;
61
- if (typeof cursorLabelOffset === "number") {
62
- return {
63
- x: cursorLabelOffset,
64
- y: cursorLabelOffset
65
- };
66
- }
67
- return cursorLabelOffset;
42
+ return defaultCursorValue;
43
+ };
44
+ const getCursorLabelOffset = () => {
45
+ const {
46
+ cursorLabelOffset
47
+ } = props;
48
+ if (typeof cursorLabelOffset === "number") {
49
+ return {
50
+ x: cursorLabelOffset,
51
+ y: cursorLabelOffset
52
+ };
68
53
  }
69
- getPadding(props) {
70
- if (props.padding === undefined) {
71
- const child = props.children.find(c => {
72
- return _isObject(c.props) && c.props.padding !== undefined;
73
- });
74
- return Helpers.getPadding(child.props);
75
- }
76
- return Helpers.getPadding(props);
54
+ return cursorLabelOffset;
55
+ };
56
+ const getPadding = () => {
57
+ if (props.padding === undefined) {
58
+ const child = Array.isArray(props.children) ? props.children.find(c => {
59
+ return _isObject(c.props) && c.props.padding !== undefined;
60
+ }) : props.children;
61
+ return Helpers.getPadding(child?.props);
77
62
  }
78
- getCursorElements(props) {
79
- // eslint-disable-line max-statements
80
- const {
81
- scale,
82
- cursorLabelComponent,
83
- cursorLabel,
84
- cursorComponent,
85
- width,
86
- height,
87
- name,
88
- horizontal,
89
- theme
90
- } = props;
91
- const cursorDimension = CursorHelpers.getDimension(props);
92
- const cursorValue = this.getCursorPosition(props);
93
- const cursorLabelOffset = this.getCursorLabelOffset(props);
94
- if (!cursorValue) {
95
- return [];
96
- }
97
- const newElements = [];
98
- const padding = this.getPadding(props);
99
- const cursorCoordinates = {
100
- x: horizontal ? scale.y(cursorValue.y) : scale.x(cursorValue.x),
101
- y: horizontal ? scale.x(cursorValue.x) : scale.y(cursorValue.y)
102
- };
103
- if (cursorLabel) {
104
- let labelProps = _defaults({
105
- active: true
106
- }, cursorLabelComponent.props, {
107
- x: cursorCoordinates.x + cursorLabelOffset.x,
108
- y: cursorCoordinates.y + cursorLabelOffset.y,
109
- datum: cursorValue,
110
- active: true,
111
- key: `${name}-cursor-label`
112
- });
113
- if (Helpers.isTooltip(cursorLabelComponent)) {
114
- const tooltipTheme = theme && theme.tooltip || {};
115
- labelProps = _defaults({}, labelProps, tooltipTheme);
116
- }
117
- newElements.push( /*#__PURE__*/React.cloneElement(cursorLabelComponent, _defaults({}, labelProps, {
118
- text: Helpers.evaluateProp(cursorLabel, labelProps)
119
- })));
120
- }
121
- const cursorStyle = Object.assign({
122
- stroke: "black"
123
- }, cursorComponent.props.style);
124
- if (cursorDimension === "x" || cursorDimension === undefined) {
125
- newElements.push( /*#__PURE__*/React.cloneElement(cursorComponent, {
126
- key: `${name}-x-cursor`,
127
- x1: cursorCoordinates.x,
128
- x2: cursorCoordinates.x,
129
- y1: padding.top,
130
- y2: height - padding.bottom,
131
- style: cursorStyle
132
- }));
133
- }
134
- if (cursorDimension === "y" || cursorDimension === undefined) {
135
- newElements.push( /*#__PURE__*/React.cloneElement(cursorComponent, {
136
- key: `${name}-y-cursor`,
137
- x1: padding.left,
138
- x2: width - padding.right,
139
- y1: cursorCoordinates.y,
140
- y2: cursorCoordinates.y,
141
- style: cursorStyle
142
- }));
63
+ return Helpers.getPadding(props);
64
+ };
65
+ const getCursorElements = () => {
66
+ // eslint-disable-line max-statements
67
+ const {
68
+ scale,
69
+ cursorLabelComponent,
70
+ cursorLabel,
71
+ cursorComponent,
72
+ width,
73
+ height,
74
+ name,
75
+ horizontal,
76
+ theme
77
+ } = props;
78
+ const cursorDimension = CursorHelpers.getDimension(props);
79
+ const cursorValue = getCursorPosition();
80
+ const cursorLabelOffset = getCursorLabelOffset();
81
+ if (!cursorValue) {
82
+ return [];
83
+ }
84
+ const newElements = [];
85
+ const padding = getPadding();
86
+ const cursorCoordinates = scale && "x" in scale && "y" in scale && typeof scale.y === "function" && typeof scale.x === "function" ? {
87
+ x: horizontal ? scale.y(cursorValue.y) : scale.x(cursorValue.x),
88
+ y: horizontal ? scale.x(cursorValue.x) : scale.y(cursorValue.y)
89
+ } : {
90
+ x: cursorValue.x,
91
+ y: cursorValue.y
92
+ };
93
+ if (cursorLabel) {
94
+ let labelProps = _defaults({
95
+ active: true
96
+ }, cursorLabelComponent.props, {
97
+ x: cursorCoordinates.x + cursorLabelOffset.x,
98
+ y: cursorCoordinates.y + cursorLabelOffset.y,
99
+ datum: cursorValue,
100
+ active: true,
101
+ key: `${name}-cursor-label`
102
+ });
103
+ if (Helpers.isTooltip(cursorLabelComponent)) {
104
+ const tooltipTheme = theme && theme.tooltip || {};
105
+ labelProps = _defaults({}, labelProps, tooltipTheme);
143
106
  }
144
- return newElements;
107
+ newElements.push( /*#__PURE__*/React.cloneElement(cursorLabelComponent, _defaults({}, labelProps, {
108
+ text: Helpers.evaluateProp(cursorLabel, labelProps)
109
+ })));
110
+ }
111
+ const cursorStyle = Object.assign({
112
+ stroke: "black"
113
+ }, cursorComponent.props.style);
114
+ if (cursorDimension === "x" || cursorDimension === undefined) {
115
+ newElements.push( /*#__PURE__*/React.cloneElement(cursorComponent, {
116
+ key: `${name}-x-cursor`,
117
+ x1: cursorCoordinates.x,
118
+ x2: cursorCoordinates.x,
119
+ y1: padding.top,
120
+ y2: (typeof height === "number" ? height : 0) - padding.bottom,
121
+ style: cursorStyle
122
+ }));
145
123
  }
146
-
147
- // Overrides method in VictoryContainer
148
- getChildren(props) {
149
- return [...React.Children.toArray(props.children), ...this.getCursorElements(props)];
124
+ if (cursorDimension === "y" || cursorDimension === undefined) {
125
+ newElements.push( /*#__PURE__*/React.cloneElement(cursorComponent, {
126
+ key: `${name}-y-cursor`,
127
+ x1: padding.left,
128
+ x2: (typeof width === "number" ? width : 0) - padding.right,
129
+ y1: cursorCoordinates.y,
130
+ y2: cursorCoordinates.y,
131
+ style: cursorStyle
132
+ }));
150
133
  }
134
+ return newElements;
135
+ };
136
+ return {
137
+ props,
138
+ children: [...React.Children.toArray(children), ...getCursorElements()]
151
139
  };
152
- }
153
- export const VictoryCursorContainer = cursorContainerMixin(VictoryContainer);
140
+ };
141
+ export const VictoryCursorContainer = initialProps => {
142
+ const {
143
+ props,
144
+ children
145
+ } = useVictoryCursorContainer(initialProps);
146
+ return /*#__PURE__*/React.createElement(VictoryContainer, props, children);
147
+ };
148
+ VictoryCursorContainer.role = "container";
149
+ VictoryCursorContainer.defaultEvents = initialProps => {
150
+ const props = {
151
+ ...VICTORY_CURSOR_CONTAINER_DEFAULT_PROPS,
152
+ ...initialProps
153
+ };
154
+ const createEventHandler = (handler, disabled) =>
155
+ // eslint-disable-next-line max-params
156
+ (event, targetProps, eventKey, context) => disabled || props.disable ? {} : handler(event, {
157
+ ...props,
158
+ ...targetProps
159
+ }, eventKey, context);
160
+ return [{
161
+ target: "parent",
162
+ eventHandlers: {
163
+ onMouseLeave: createEventHandler(CursorHelpers.onMouseLeave),
164
+ onMouseMove: createEventHandler(CursorHelpers.onMouseMove),
165
+ onTouchMove: createEventHandler(CursorHelpers.onMouseMove)
166
+ }
167
+ }];
168
+ };
@@ -1,5 +1,5 @@
1
1
  import React from "react";
2
- import { VictoryContainer, VictoryContainerProps, CoordinatesPropType, VictoryLabelProps, ValueOrAccessor } from "victory-core";
2
+ import { VictoryContainerProps, CoordinatesPropType, VictoryLabelProps, ValueOrAccessor, VictoryEventHandler, DomainTuple, PaddingProps } from "victory-core";
3
3
  export declare type CursorCoordinatesPropType = CoordinatesPropType | number;
4
4
  export interface VictoryCursorContainerProps extends VictoryContainerProps {
5
5
  cursorComponent?: React.ReactElement;
@@ -9,126 +9,77 @@ export interface VictoryCursorContainerProps extends VictoryContainerProps {
9
9
  cursorLabelOffset?: CursorCoordinatesPropType;
10
10
  defaultCursorValue?: CursorCoordinatesPropType;
11
11
  disable?: boolean;
12
+ horizontal?: boolean;
13
+ padding?: PaddingProps;
12
14
  onCursorChange?: (value: CursorCoordinatesPropType, props: VictoryCursorContainerProps) => void;
13
15
  }
14
- declare type ComponentClass<TProps> = {
15
- new (props: TProps): React.Component<TProps>;
16
- };
17
- export declare function cursorContainerMixin<TBase extends ComponentClass<TProps>, TProps extends VictoryCursorContainerProps>(Base: TBase): {
18
- new (props: TProps): {
19
- getCursorPosition(props: any): any;
20
- getCursorLabelOffset(props: any): any;
21
- getPadding(props: any): {
22
- top: any;
23
- bottom: any;
24
- left: any;
25
- right: any;
26
- };
27
- getCursorElements(props: any): React.ReactElement<any, string | React.JSXElementConstructor<any>>[];
28
- getChildren(props: any): (string | number | React.ReactElement<any, string | React.JSXElementConstructor<any>> | React.ReactFragment)[];
29
- context: unknown;
30
- setState<K extends never>(state: {} | ((prevState: Readonly<{}>, props: Readonly<TProps>) => {} | Pick<{}, K> | null) | Pick<{}, K> | null, callback?: (() => void) | undefined): void;
31
- forceUpdate(callback?: (() => void) | undefined): void;
32
- render(): React.ReactNode;
33
- readonly props: Readonly<TProps>;
34
- state: Readonly<{}>;
35
- refs: {
36
- [key: string]: React.ReactInstance;
37
- };
38
- componentDidMount?(): void;
39
- shouldComponentUpdate?(nextProps: Readonly<TProps>, nextState: Readonly<{}>, nextContext: any): boolean;
40
- componentWillUnmount?(): void;
41
- componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void;
42
- getSnapshotBeforeUpdate?(prevProps: Readonly<TProps>, prevState: Readonly<{}>): any;
43
- componentDidUpdate?(prevProps: Readonly<TProps>, prevState: Readonly<{}>, snapshot?: any): void;
44
- componentWillMount?(): void;
45
- UNSAFE_componentWillMount?(): void;
46
- componentWillReceiveProps?(nextProps: Readonly<TProps>, nextContext: any): void;
47
- UNSAFE_componentWillReceiveProps?(nextProps: Readonly<TProps>, nextContext: any): void;
48
- componentWillUpdate?(nextProps: Readonly<TProps>, nextState: Readonly<{}>, nextContext: any): void;
49
- UNSAFE_componentWillUpdate?(nextProps: Readonly<TProps>, nextState: Readonly<{}>, nextContext: any): void;
16
+ export declare const VICTORY_CURSOR_CONTAINER_DEFAULT_PROPS: {
17
+ cursorLabelComponent: JSX.Element;
18
+ cursorLabelOffset: {
19
+ x: number;
20
+ y: number;
50
21
  };
51
- displayName: string;
52
- defaultProps: {
53
- cursorLabelComponent: JSX.Element;
54
- cursorLabelOffset: {
55
- x: number;
56
- y: number;
22
+ cursorComponent: JSX.Element;
23
+ };
24
+ export declare const useVictoryCursorContainer: (initialProps: VictoryCursorContainerProps) => {
25
+ props: {
26
+ cursorValue: CoordinatesPropType | null;
27
+ domain: {
28
+ x: DomainTuple;
29
+ y: DomainTuple;
57
30
  };
58
31
  cursorComponent: JSX.Element;
59
- className: string;
60
- portalComponent: JSX.Element;
61
- portalZIndex: number;
62
- responsive: boolean;
63
- role: string;
64
- };
65
- defaultEvents(props: any): {
66
- target: string;
67
- eventHandlers: {
68
- onMouseLeave: (evt: any, targetProps: any) => {};
69
- onTouchCancel: () => never[];
70
- onMouseMove: (evt: any, targetProps: any) => {} | undefined;
71
- onTouchMove: (evt: any, targetProps: any) => {} | undefined;
72
- };
73
- }[];
74
- } & TBase;
75
- export declare const VictoryCursorContainer: {
76
- new (props: VictoryCursorContainerProps): {
77
- getCursorPosition(props: any): any;
78
- getCursorLabelOffset(props: any): any;
79
- getPadding(props: any): {
80
- top: any;
81
- bottom: any;
82
- left: any;
83
- right: any;
84
- };
85
- getCursorElements(props: any): React.ReactElement<any, string | React.JSXElementConstructor<any>>[];
86
- getChildren(props: any): (string | number | React.ReactElement<any, string | React.JSXElementConstructor<any>> | React.ReactFragment)[];
87
- context: unknown;
88
- setState<K extends never>(state: {} | ((prevState: Readonly<{}>, props: Readonly<VictoryCursorContainerProps>) => {} | Pick<{}, K> | null) | Pick<{}, K> | null, callback?: (() => void) | undefined): void;
89
- forceUpdate(callback?: (() => void) | undefined): void;
90
- render(): React.ReactNode;
91
- readonly props: Readonly<VictoryCursorContainerProps>;
92
- state: Readonly<{}>;
93
- refs: {
94
- [key: string]: React.ReactInstance;
95
- };
96
- componentDidMount?(): void;
97
- shouldComponentUpdate?(nextProps: Readonly<VictoryCursorContainerProps>, nextState: Readonly<{}>, nextContext: any): boolean;
98
- componentWillUnmount?(): void;
99
- componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void;
100
- getSnapshotBeforeUpdate?(prevProps: Readonly<VictoryCursorContainerProps>, prevState: Readonly<{}>): any;
101
- componentDidUpdate?(prevProps: Readonly<VictoryCursorContainerProps>, prevState: Readonly<{}>, snapshot?: any): void;
102
- componentWillMount?(): void;
103
- UNSAFE_componentWillMount?(): void;
104
- componentWillReceiveProps?(nextProps: Readonly<VictoryCursorContainerProps>, nextContext: any): void;
105
- UNSAFE_componentWillReceiveProps?(nextProps: Readonly<VictoryCursorContainerProps>, nextContext: any): void;
106
- componentWillUpdate?(nextProps: Readonly<VictoryCursorContainerProps>, nextState: Readonly<{}>, nextContext: any): void;
107
- UNSAFE_componentWillUpdate?(nextProps: Readonly<VictoryCursorContainerProps>, nextState: Readonly<{}>, nextContext: any): void;
108
- };
109
- displayName: string;
110
- defaultProps: {
32
+ cursorDimension?: "x" | "y" | undefined;
33
+ cursorLabel?: ValueOrAccessor<VictoryLabelProps["text"]>;
111
34
  cursorLabelComponent: JSX.Element;
112
- cursorLabelOffset: {
113
- x: number;
114
- y: number;
115
- };
116
- cursorComponent: JSX.Element;
117
- className: string;
118
- portalComponent: JSX.Element;
119
- portalZIndex: number;
120
- responsive: boolean;
121
- role: string;
35
+ cursorLabelOffset: CursorCoordinatesPropType;
36
+ defaultCursorValue?: CursorCoordinatesPropType | undefined;
37
+ disable?: boolean | undefined;
38
+ horizontal?: boolean | undefined;
39
+ padding?: PaddingProps | undefined;
40
+ onCursorChange?: ((value: CursorCoordinatesPropType, props: VictoryCursorContainerProps) => void) | undefined;
41
+ "aria-describedby"?: string | undefined;
42
+ "aria-labelledby"?: string | undefined;
43
+ children?: React.ReactElement<any, string | React.JSXElementConstructor<any>> | React.ReactElement<any, string | React.JSXElementConstructor<any>>[] | undefined;
44
+ className?: string | undefined;
45
+ containerId?: string | number | undefined;
46
+ containerRef?: React.Ref<HTMLElement> | undefined;
47
+ desc?: string | undefined;
48
+ events?: React.DOMAttributes<any> | undefined;
49
+ height?: number | undefined;
50
+ name?: string | undefined;
51
+ origin?: import("victory-core").OriginType | undefined;
52
+ polar?: boolean | undefined;
53
+ portalComponent?: React.ReactElement<any, string | React.JSXElementConstructor<any>> | undefined;
54
+ portalZIndex?: number | undefined;
55
+ preserveAspectRatio?: string | undefined;
56
+ responsive?: boolean | undefined;
57
+ role?: string | undefined;
58
+ scale?: {
59
+ x?: import("victory-core").D3Scale<any> | undefined;
60
+ y?: import("victory-core").D3Scale<any> | undefined;
61
+ } | undefined;
62
+ style?: React.CSSProperties | undefined;
63
+ tabIndex?: number | undefined;
64
+ theme?: import("victory-core").VictoryThemeDefinition | undefined;
65
+ title?: string | undefined;
66
+ width?: number | undefined;
67
+ ouiaId?: string | number | undefined;
68
+ ouiaSafe?: boolean | undefined;
69
+ ouiaType?: string | undefined;
122
70
  };
123
- defaultEvents(props: any): {
71
+ children: React.ReactElement<any, string | React.JSXElementConstructor<any>>[];
72
+ };
73
+ export declare const VictoryCursorContainer: {
74
+ (initialProps: VictoryCursorContainerProps): JSX.Element;
75
+ role: string;
76
+ defaultEvents(initialProps: VictoryCursorContainerProps): {
124
77
  target: string;
125
78
  eventHandlers: {
126
- onMouseLeave: (evt: any, targetProps: any) => {};
127
- onTouchCancel: () => never[];
128
- onMouseMove: (evt: any, targetProps: any) => {} | undefined;
129
- onTouchMove: (evt: any, targetProps: any) => {} | undefined;
79
+ onMouseLeave: VictoryEventHandler;
80
+ onMouseMove: VictoryEventHandler;
81
+ onTouchMove: VictoryEventHandler;
130
82
  };
131
83
  }[];
132
- } & typeof VictoryContainer;
133
- export {};
84
+ };
134
85
  //# sourceMappingURL=victory-cursor-container.d.ts.map