forked from deriv-com/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
87 lines (82 loc) · 3.19 KB
/
index.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
import clsx from 'clsx';
import React, { ReactNode, useState } from 'react';
import "./CircularProgressBar.scss"
type TVariant = "clockwise" | "static" | "selectable"
type TCircularProgressProps = {
children?: ReactNode;
className?: string;
danger_limit?: number;
is_clockwise?: boolean;
progress?: number;
radius?: number;
stroke?: number;
warning_limit?: number;
icon?: ReactNode;
variant: TVariant;
onSelect?: () => void;
};
export const CircularProgressBar = ({
children,
className,
danger_limit = 20,
is_clockwise = false,
progress = 0,
radius = 22,
stroke = 3,
warning_limit = 50,
variant = "clockwise",
onSelect // Function to be called when circle is selected
}: TCircularProgressProps) => {
const normalizedRadius = radius - stroke / 2;
const circumference = normalizedRadius * 2 * Math.PI;
const strokeDashoffset = circumference - (progress / 100) * circumference;
const [selected, setSelected] = useState(false);
const handleSelect = () => {
setSelected(!selected);
if (onSelect) {
onSelect();
}
};
return (
<div className={clsx('deriv-circular-progress', className)}>
<svg height={radius * 2} width={radius * 2} onClick={variant === 'selectable' ? handleSelect : undefined}>
{children && (
<foreignObject x="0" y="0" width={radius * 2} height={radius * 2}>
<div className={clsx("deriv-circular-progress__content")}>
{children}
</div>
</foreignObject>
)}
<circle
className={clsx('deriv-circular-progress__bar', {
'deriv-circular-progress--clockwise': is_clockwise,
'deriv-circular-progress__bar--warning': progress <= warning_limit && progress > danger_limit,
'deriv-circular-progress__bar--danger': progress <= danger_limit,
'deriv-circular-progress__bar--selected': selected && variant === 'selectable'
})}
cx={radius}
cy={radius}
fill={variant === 'selectable' && selected ? 'blue' : 'transparent'}
r={normalizedRadius}
strokeDasharray={`${circumference} ${circumference}`}
strokeWidth={stroke}
style={variant != "clockwise" ? { strokeDashoffset: 'none' } : { strokeDashoffset }}
/>
{
variant != "clockwise" && (
<circle
cx={radius}
cy={radius}
r={normalizedRadius}
fill='none'
stroke='#E8EEFC'
strokeDasharray={`${circumference} ${circumference}`}
strokeWidth={stroke}
style={{ strokeDashoffset: circumference - strokeDashoffset }}
/>
)
}
</svg>
</div>
);
};