-
Notifications
You must be signed in to change notification settings - Fork 0
/
NonReactiveCounter.stories.tsx
57 lines (48 loc) · 1.32 KB
/
NonReactiveCounter.stories.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
import { useRef } from 'react';
import { Meta, Story } from '@storybook/react';
import { useRefValue, ref } from '../src';
interface Props {
step: number;
count: any; // TODO: Need a way to properly export & import types
}
const Counter = function Counter(props: Props) {
const count = useRefValue(props.count);
return (
<div>
<div>Count: {count}</div>
<div>
<button
type="button"
onClick={() => (props.count.current += props.step)}
>
Increment
</button>
<button
type="button"
onClick={() => (props.count.current -= props.step)}
>
Decrement
</button>
</div>
</div>
);
};
function App(props: Props) {
const count = useRef(ref(0)); // little bit of inception here... :)
return <Counter count={count.current} step={props.step} />;
}
const meta: Meta<Props> = {
title: 'NonReactiveCounter',
component: App,
parameters: {
controls: { expanded: true },
},
};
export default meta;
const Template: Story<Props> = args => <App {...args} />;
// By passing using the Args format for exported stories, you can control the props for a component for reuse in a test
// https://storybook.js.org/docs/react/workflows/unit-testing
export const Default = Template.bind({});
Default.args = {
step: 1,
};