-
Notifications
You must be signed in to change notification settings - Fork 79
/
InteractionTabStory.tsx
58 lines (53 loc) · 1.52 KB
/
InteractionTabStory.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
import React, { useState, ComponentProps } from 'react';
import { action } from '@storybook/addon-actions';
import { PieChart } from '../src';
type Props = {
data: ComponentProps<typeof PieChart>['data'];
};
function DemoInteractionTab(props: Props) {
const [selected, setSelected] = useState<undefined | number>(0);
const [focused, setFocused] = useState<undefined | number>(undefined);
const data = props.data.map((entry, i) => {
let result = entry;
if (focused === i) {
result = {
...result,
color: 'grey',
};
}
return result;
});
const segmentsStyle = { transition: 'stroke .3s', cursor: 'pointer' };
return (
<>
<p>
Press Tab until focus reaches the Chart or click on the yellow sector
and press Tab and then Enter.
</p>
<PieChart
data={data}
radius={40}
lineWidth={75}
segmentsStyle={(index) => {
return index === selected
? { ...segmentsStyle, strokeWidth: 35 }
: segmentsStyle;
}}
segmentsTabIndex={1}
onKeyDown={(event, index) => {
// Enter keypress
if (event.keyCode === 13) {
action('CLICK')(event, index);
console.log('CLICK', { event, index });
setSelected(selected === index ? undefined : index);
}
}}
onFocus={(_, index) => {
setFocused(index);
}}
onBlur={() => setFocused(undefined)}
/>
</>
);
}
export default DemoInteractionTab;