Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEQ]Yaswanth/FEQ-1858_Implemented Circular ProgressBar component #133

Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a78639a
feat: implemented circular linear progress bar component and stories
yaswanth-deriv Mar 19, 2024
8de8fa2
feat : made changes in styling
yaswanth-deriv Mar 20, 2024
3a81166
feat: added styling for children components
yaswanth-deriv Mar 20, 2024
8d3e4c5
feat: added variant and select styling
yaswanth-deriv Mar 21, 2024
8f11adf
Merge branch 'deriv-com:main' into yaswanth/FEQ-1858_Implement_circul…
yaswanth-deriv Mar 22, 2024
1039b89
Merge branch 'deriv-com:main' into yaswanth/FEQ-1858_Implement_circul…
yaswanth-deriv Mar 25, 2024
c230cbd
story: added stories for circular progressbar
yaswanth-deriv Mar 25, 2024
ab2b762
feat: added test cases for circular progressbar
yaswanth-deriv Mar 26, 2024
58de5e9
Merge branch 'deriv-com:main' into yaswanth/FEQ-1858_Implement_circul…
yaswanth-deriv Mar 26, 2024
49a49d2
feat: to fix EOF error
yaswanth-deriv Mar 26, 2024
0653e01
feat: added prev state in useState hook
yaswanth-deriv Mar 26, 2024
12fa567
feat: added inset in styling
yaswanth-deriv Mar 26, 2024
ee2a5de
feat: made variant as optional
yaswanth-deriv Mar 26, 2024
6d05dd4
Merge branch 'deriv-com:main' into yaswanth/FEQ-1858_Implement_circul…
yaswanth-deriv Mar 26, 2024
c1b45d7
Merge branch 'deriv-com:main' into yaswanth/FEQ-1858_Implement_circul…
yaswanth-deriv Apr 1, 2024
f15878b
story: added docs to storybook for circular component
yaswanth-deriv Apr 1, 2024
e248ec7
Merge branch 'deriv-com:main' into yaswanth/FEQ-1858_Implement_circul…
yaswanth-deriv Apr 1, 2024
3b81738
Merge branch 'deriv-com:main' into yaswanth/FEQ-1858_Implement_circul…
yaswanth-deriv Apr 2, 2024
0126ca3
Merge branch 'deriv-com:main' into yaswanth/FEQ-1858_Implement_circul…
yaswanth-deriv Apr 3, 2024
069a5fb
feat: removed variant from circular progress bar component
yaswanth-deriv Apr 3, 2024
49b41d9
test: added new test cases in circular component
yaswanth-deriv Apr 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/components/CircularProgressBar/CircularProgressBar.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.deriv-circular-progress {
position: relative;
line-height: 0;
width: fit-content;

&__content {
position: absolute;
inset:0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}

&__bar {
transform: scaleX(-1) rotate(-90deg);
transform-origin: 50% 50%;
transition: stroke-dashoffset 1s;
stroke: var(--du-brand-secondary, #85acb0);

&--warning {
stroke: var(--du-status-warning, #ffad3a);
}

&--danger {
stroke: var(--du-status-danger, #ec3f3f)
}
}

&--clockwise {
transform: rotate(-90deg);
}

&__icon {
position: absolute;
width: 1.6rem;
height: 100%;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import { CircularProgressBar } from '..';

describe('CircularProgressBar Component', () => {
it('renders correctly with default props', () => {
const { container } = render(<CircularProgressBar variant="clockwise" />);
expect(container.querySelector('.deriv-circular-progress__bar')).toBeInTheDocument();
});

it('renders children correctly', () => {
const { getByText } = render(
<CircularProgressBar variant="clockwise">
<div className="circular-progress-content">Child content</div>
</CircularProgressBar>
);
expect(getByText('Child content')).toBeInTheDocument();
});

it('handles selection correctly for selectable variant', () => {
const onSelectMock = jest.fn();
const { container } = render(
<CircularProgressBar variant="selectable" onSelect={onSelectMock} />
);
const progressBar = container.querySelector('.deriv-circular-progress__bar');
if (progressBar) {
fireEvent.click(progressBar);
expect(onSelectMock).toHaveBeenCalled();
} else {
fail('Progress bar not found');
}

});
});
87 changes: 87 additions & 0 deletions src/components/CircularProgressBar/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,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(prev=>!prev);
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>
);
};
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { ActionScreen } from "./components/ActionScreen";
export { Button } from "./components/Button";
export {Badge} from "./components/Badge";
export { Checkbox } from "./components/Checkbox";
export { CircularProgressBar } from "./components/CircularProgressBar"
export { Divider } from "./components/Divider";
export { Dialog } from "./components/Dialog"
export { Dropdown } from "./components/Dropdown";
Expand Down
80 changes: 80 additions & 0 deletions stories/CircularProgressBar.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import type { Meta, StoryObj } from "@storybook/react";
import { CircularProgressBar } from "../src/components/CircularProgressBar";

const meta = {
title: "Components/CircularProgressBar",
component: CircularProgressBar,
parameters: {
layout: "centered",
},
args: {
progress: 50,
radius: 22,
stroke: 3,
danger_limit: 20,
warning_limit: 50,
variant:"clockwise"
},
argTypes: {
progress: {
control: { type: "range", min: 0, max: 100, step: 1 },
},
},
} as Meta<typeof CircularProgressBar>;
export default meta;
type Story = StoryObj<typeof meta>;

export const DefaultClockWise: Story = {
args: {
progress: 50,
}
}
export const StaticProgressBar:Story = {
args:{
progress: 50,
variant: 'static',
}
}

export const SelectableProgressBar:Story = {
args:{
progress: 30,
variant: 'selectable',
}
}

export const CustomRadiusAndStrokeClockwise: Story = {
args: {
progress: 70,
radius: 30,
stroke: 5,
}
}

export const ClockwiseProgress: Story = {
args: {
progress: 60,
is_clockwise: true,
}
}

export const WithIconSelectable: Story = {
args: {
progress: 80,
children: <span>🔄</span>,
variant:"selectable"
}
}

export const WarningLimitClockWise = {
args: {
progress: 40,
warning_limit: 30,
}
}
export const DangerLimitClockWise: Story = {
args: {
progress: 10,
danger_limit: 20,
}
}