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

refactor: implement abstract factory #1182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
66 changes: 47 additions & 19 deletions stories/Doughnut.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,46 @@ import 'chart.js/auto';
import { Doughnut } from '../src';
import { data } from '../sandboxes/doughnut/default/App';

// Interface for the chart factory
interface ChartFactory {
createChart(args: any): JSX.Element;
}

// Implementation of the Doughnut chart factory
class DoughnutFactory implements ChartFactory {
/**
* Creates a Doughnut chart with the given arguments.
* @param args - The arguments to pass to the Doughnut component.
* @returns A JSX element representing the Doughnut chart.
*/
createChart(args: any): JSX.Element {
return <Doughnut {...args} />;
}
}

// Implementation of the Rotating Doughnut chart factory
class RotatingDoughnutFactory implements ChartFactory {
/**
* Creates a Rotating Doughnut chart with the given arguments.
* @param args - The arguments to pass to the Doughnut component.
* @returns A JSX element representing the Rotating Doughnut chart.
*/
createChart(args: any): JSX.Element {
const [rotation, setRotation] = useState(0);

useEffect(() => {
const interval = setInterval(() => {
setRotation(rotation => rotation + 90);
}, 3000);

return () => clearInterval(interval);
});

return <Doughnut {...args} options={{ rotation }} />;
}
}

// Storybook configuration for the Doughnut component
export default {
title: 'Components/Doughnut',
component: Doughnut,
Expand All @@ -15,26 +55,14 @@ export default {
},
};

export const Default = args => <Doughnut {...args} />;

Default.args = {
data,
// Storybook story for the default Doughnut chart
export const Default = args => {
const factory = new DoughnutFactory();
return factory.createChart({ ...args, data });
};

// Storybook story for the rotating Doughnut chart
export const Rotation = args => {
const [rotation, setRotation] = useState(0);

useEffect(() => {
const interval = setInterval(() => {
setRotation(rotation => rotation + 90);
}, 3000);

return () => clearInterval(interval);
});

return <Doughnut {...args} options={{ rotation }} />;
};

Rotation.args = {
data,
const factory = new RotatingDoughnutFactory();
return factory.createChart({ ...args, data });
};