/
bar.tsx
110 lines (101 loc) · 2.83 KB
/
bar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import React, { forwardRef } from "react";
import { defaults } from "lodash";
import {
Helpers,
NumberOrCallback,
Path,
VictoryCommonPrimitiveProps,
} from "victory-core";
import { getStyle, getBarWidth, getCornerRadius } from "./bar-helper-methods";
import { getPolarBarPath, getBarPath } from "./path-helper-methods";
import {
VictoryBarAlignmentType,
VictoryBarCornerRadiusObject,
} from "./victory-bar";
export interface BarProps extends VictoryCommonPrimitiveProps {
alignment?: VictoryBarAlignmentType;
barOffset?: number[];
barRatio?: number;
barWidth?: NumberOrCallback;
cornerRadius?: NumberOrCallback | VictoryBarCornerRadiusObject;
datum?: any;
getPath?: (props: BarProps) => string;
horizontal?: boolean;
pathComponent?: React.ReactElement;
width?: number;
x?: number;
y?: number;
y0?: number;
}
const evaluateProps = (props: BarProps) => {
/**
* Potential evaluated props of following must be evaluated in this order:
* 1) `style`
* 2) `barWidth`
* 3) `cornerRadius`
*
* Everything else does not have to be evaluated in a particular order:
* `ariaLabel`
* `desc`
* `id`
* `tabIndex`
*/
const style = getStyle(props.style, props);
const barWidth = getBarWidth(
props.barWidth,
Object.assign({}, props, { style }),
);
const cornerRadius = getCornerRadius(
props.cornerRadius,
Object.assign({}, props, { style, barWidth }),
);
const ariaLabel = Helpers.evaluateProp(props.ariaLabel, props);
const desc = Helpers.evaluateProp(props.desc, props);
const id = Helpers.evaluateProp(props.id, props);
const tabIndex = Helpers.evaluateProp(props.tabIndex, props);
return Object.assign({}, props, {
ariaLabel,
style,
barWidth,
cornerRadius,
desc,
id,
tabIndex,
});
};
const defaultProps: Partial<BarProps> = {
pathComponent: <Path />,
role: "presentation",
shapeRendering: "auto",
};
// eslint-disable-next-line prefer-arrow-callback
export const Bar = forwardRef<SVGPathElement, BarProps>(function Bar(
initialProps,
ref,
) {
const props = evaluateProps(defaults({}, initialProps, defaultProps));
const { polar, origin, style, barWidth, cornerRadius } = props;
const path = polar
? getPolarBarPath(props, cornerRadius)
: getBarPath(props, barWidth, cornerRadius);
const defaultTransform =
polar && origin ? `translate(${origin.x}, ${origin.y})` : undefined;
if (!props.pathComponent) {
return null;
}
return React.cloneElement(props.pathComponent, {
...props.events,
"aria-label": props.ariaLabel,
style,
d: path,
className: props.className,
clipPath: props.clipPath,
desc: props.desc,
index: props.index,
role: props.role,
shapeRendering: props.shapeRendering,
transform: props.transform || defaultTransform,
tabIndex: props.tabIndex,
ref,
});
});