Skip to content

Commit

Permalink
refactor: BarChart component
Browse files Browse the repository at this point in the history
  • Loading branch information
samshara committed Jun 7, 2024
1 parent 5acaddc commit 54f8dfa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
48 changes: 19 additions & 29 deletions packages/go-ui-storybook/src/stories/BarChart.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BarChartProps } from '@ifrc-go/ui';
import type {
Args,
Meta,
StoryObj,
} from '@storybook/react';
Expand All @@ -14,21 +13,19 @@ interface Option {
}

const data: Option[] = [
{ id: 1, label: '2022', value: 10 },
{ id: 2, label: '2023', value: 9 },
{ id: 3, label: '2024', value: 5 },
{ id: 4, label: '2020', value: 2 },
{ id: 1, label: 'Apples', value: 50 },
{ id: 2, label: 'Oranges', value: 30 },
{ id: 3, label: 'Bananas', value: 20 },
{ id: 4, label: 'Grapes', value: 40 },
];

const keySelector = (d: Option) => d.id;
const valueSelector = (d: Option) => d.value;
const labelSelector = (d: Option) => d.label;

const maxValue = Math.max(...data.map(valueSelector));
type BarChartSpecificProps = BarChartProps<Option>;

type Story = StoryObj<typeof BarChart>;

type BarChartSpecificProps = BarChartProps<Option>;
type Story = StoryObj<BarChartSpecificProps>;
const meta: Meta<BarChartSpecificProps> = {
title: 'Components/BarChart',
component: BarChart,
Expand All @@ -44,34 +41,27 @@ const meta: Meta<BarChartSpecificProps> = {

export default meta;

function Template(args:Args) {
return (
<BarChart
// eslint-disable-next-line react/jsx-props-no-spreading
{...args}
data={data}
keySelector={keySelector}
labelSelector={labelSelector}
valueSelector={valueSelector}
maxValue={maxValue}
/>
);
}

export const Default: Story = {
render: Template,
args: {
data,
keySelector,
labelSelector,
valueSelector,
maxValue,
className: 'bar-chart',
},
};

export const MaxRows: Story = {
render: Template,
export const WithMaxRows: Story = {
args: {
maxRows: 5,
...Default.args,
maxRows: 3,
},
};

export const CompactValue: Story = {
render: Template,
export const WithCompactValue: Story = {
args: {
...Default.args,
compactValue: true,
},
};
6 changes: 3 additions & 3 deletions packages/go-ui-storybook/src/stories/BarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {
BarChartProps as PureBarChartProps,
} from '@ifrc-go/ui';

type BarChartProps<D> = PureBarChartProps<D>
export type BarChartProps<D> = PureBarChartProps<D>

function WrappedBarChart<const D>(props: BarChartProps<D>) {
function BarChart<const D>(props: BarChartProps<D>) {
return (
<PureBarChart {...props} />// eslint-disable-line react/jsx-props-no-spreading
);
}

export default WrappedBarChart;
export default BarChart;
4 changes: 4 additions & 0 deletions packages/go-ui-storybook/src/stories/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@
.text-body > p {
font-size: var(--go-ui-font-size-lg);
}

.bar-chart {
width: 30rem;
}

0 comments on commit 54f8dfa

Please sign in to comment.