Skip to content

Commit ef761dc

Browse files
committed
add stories
1 parent 062a7ec commit ef761dc

6 files changed

Lines changed: 124 additions & 24 deletions

File tree

packages/charts/src/components/DonutChart/DonutChart.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ControlsWithNote, DocsHeader, Footer } from '@sb/components';
33
import TooltipStory from '../../resources/TooltipConfig.mdx';
44
import * as ComponentStories from './DonutChart.stories';
55
import LegendStory from '../../resources/LegendConfig.mdx';
6+
import KeyboardNavigationStory from '../../resources/KeyboardNavigation.mdx';
67

78
<Meta of={ComponentStories} />
89

@@ -45,6 +46,8 @@ import LegendStory from '../../resources/LegendConfig.mdx';
4546

4647
<Canvas of={ComponentStories.WithActiveShape} />
4748

49+
<KeyboardNavigationStory of={ComponentStories.KeyboardNavigation} />
50+
4851
### Hide labels
4952

5053
<Canvas of={ComponentStories.HideLabels} />

packages/charts/src/components/DonutChart/DonutChart.stories.tsx

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import type { Meta, StoryObj } from '@storybook/react-vite';
22
import { useEffect, useState } from 'react';
3-
import { legendConfig, simpleDataSet, simpleDataSetWithSmallValues, tooltipConfig } from '../../resources/DemoProps.js';
3+
import {
4+
legendConfig,
5+
simpleDataSet,
6+
simpleDataSetWithSmallValues,
7+
tooltipConfig,
8+
keyboardNavigationStory,
9+
} from '../../resources/DemoProps.js';
410
import { DonutChart } from './index.js';
511

612
const meta = {
@@ -75,28 +81,6 @@ export const WithFormatter: Story = {
7581
},
7682
};
7783

78-
export const HideLabels: Story = {
79-
args: {
80-
measure: {
81-
accessor: 'users',
82-
hideDataLabel: (chartConfig) => {
83-
if (chartConfig.percent < 0.01) {
84-
return true;
85-
}
86-
},
87-
},
88-
dataset: simpleDataSetWithSmallValues,
89-
},
90-
};
91-
92-
export const WithCustomTooltipConfig: Story = {
93-
args: tooltipConfig,
94-
};
95-
96-
export const WithCustomLegendConfig: Story = {
97-
args: legendConfig,
98-
};
99-
10084
export const WithActiveShape: Story = {
10185
args: {
10286
chartConfig: {
@@ -120,3 +104,27 @@ export const WithActiveShape: Story = {
120104
return <DonutChart {...args} chartConfig={{ ...args.chartConfig, activeSegment }} onClick={handleChartClick} />;
121105
},
122106
};
107+
108+
export const KeyboardNavigation: Story = keyboardNavigationStory(DonutChart);
109+
110+
export const HideLabels: Story = {
111+
args: {
112+
measure: {
113+
accessor: 'users',
114+
hideDataLabel: (chartConfig) => {
115+
if (chartConfig.percent < 0.01) {
116+
return true;
117+
}
118+
},
119+
},
120+
dataset: simpleDataSetWithSmallValues,
121+
},
122+
};
123+
124+
export const WithCustomTooltipConfig: Story = {
125+
args: tooltipConfig,
126+
};
127+
128+
export const WithCustomLegendConfig: Story = {
129+
args: legendConfig,
130+
};

packages/charts/src/components/PieChart/PieChart.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Canvas, Meta } from '@storybook/addon-docs/blocks';
33
import TooltipStory from '../../resources/TooltipConfig.mdx';
44
import * as ComponentStories from './PieChart.stories';
55
import LegendStory from '../../resources/LegendConfig.mdx';
6+
import KeyboardNavigationStory from '../../resources/KeyboardNavigation.mdx';
67

78
<Meta of={ComponentStories} />
89

@@ -33,6 +34,12 @@ import LegendStory from '../../resources/LegendConfig.mdx';
3334

3435
<Canvas of={ComponentStories.WithFormatter} />
3536

37+
### With highlighted active segment
38+
39+
<Canvas of={ComponentStories.WithActiveShape} />
40+
41+
<KeyboardNavigationStory of={ComponentStories.KeyboardNavigation} />
42+
3643
### Hide labels
3744

3845
<Canvas of={ComponentStories.HideLabels} />

packages/charts/src/components/PieChart/PieChart.stories.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import type { Meta, StoryObj } from '@storybook/react-vite';
22
import { useEffect, useState } from 'react';
3-
import { legendConfig, simpleDataSet, simpleDataSetWithSmallValues, tooltipConfig } from '../../resources/DemoProps.js';
3+
import {
4+
legendConfig,
5+
simpleDataSet,
6+
simpleDataSetWithSmallValues,
7+
tooltipConfig,
8+
keyboardNavigationStory,
9+
} from '../../resources/DemoProps.js';
410
import { PieChart } from './index.js';
511

612
const meta = {
@@ -91,6 +97,8 @@ export const WithActiveShape: Story = {
9197
},
9298
};
9399

100+
export const KeyboardNavigation: Story = keyboardNavigationStory(PieChart);
101+
94102
export const HideLabels: Story = {
95103
args: {
96104
measure: {

packages/charts/src/resources/DemoProps.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { ThemingParameters } from '@ui5/webcomponents-react-base/ThemingParameters';
2+
import type { ComponentType } from 'react';
3+
import { useEffect, useState } from 'react';
24
import { DefaultTooltipContent } from 'recharts';
35
import type { TooltipProps } from 'recharts';
46
import type { IChartBaseProps } from '../interfaces/IChartBaseProps.js';
@@ -667,3 +669,34 @@ export const CustomTooltipContent = ({ payload, ...rest }: TooltipProps<number,
667669
};
668670

669671
CustomTooltipContent.displayName = 'CustomTooltipContent';
672+
673+
export function keyboardNavigationStory(Chart: ComponentType<any>) {
674+
return {
675+
args: {
676+
chartConfig: {
677+
accessibilityLayer: true,
678+
activeSegment: 0,
679+
showActiveSegmentDataLabel: true,
680+
},
681+
},
682+
render(args) {
683+
// eslint-disable-next-line react-hooks/rules-of-hooks
684+
const [activeSegment, setActiveSegment] = useState(args.chartConfig.activeSegment);
685+
const handleDataPointClick = (e) => {
686+
const { dataIndex } = e.detail;
687+
if (dataIndex != null) {
688+
setActiveSegment(dataIndex);
689+
}
690+
};
691+
692+
// eslint-disable-next-line react-hooks/rules-of-hooks
693+
useEffect(() => {
694+
setActiveSegment(args.chartConfig.activeSegment);
695+
}, [args.chartConfig.activeSegment]);
696+
697+
return (
698+
<Chart {...args} chartConfig={{ ...args.chartConfig, activeSegment }} onDataPointClick={handleDataPointClick} />
699+
);
700+
},
701+
};
702+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Canvas } from '@storybook/addon-docs/blocks';
2+
3+
### Keyboard Navigation
4+
5+
Enable keyboard navigation for chart sectors via `chartConfig.accessibilityLayer`. When enabled, users can Tab into the chart, use arrow keys to navigate between sectors, and press Enter or Space to select a sector.
6+
7+
Use `chartConfig.activeSegment` to highlight the selected sector. Space activates on key release, allowing users to hold Space, navigate with arrow keys, and release to select the final sector.
8+
9+
<Canvas of={props.of} />
10+
11+
<details>
12+
13+
<summary>Show Code</summary>
14+
15+
```tsx
16+
function ChartComponent() {
17+
const [activeSegment, setActiveSegment] = useState(0);
18+
const handleDataPointClick = (e) => {
19+
const { dataIndex } = e.detail;
20+
if (dataIndex != null) {
21+
setActiveSegment(dataIndex);
22+
}
23+
};
24+
25+
return (
26+
<PieChart
27+
dataset={dataset}
28+
dimension={{ accessor: 'name' }}
29+
measure={{ accessor: 'users' }}
30+
chartConfig={{
31+
accessibilityLayer: true,
32+
activeSegment,
33+
showActiveSegmentDataLabel: true,
34+
}}
35+
onDataPointClick={handleDataPointClick}
36+
/>
37+
);
38+
}
39+
```
40+
41+
</details>

0 commit comments

Comments
 (0)