-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathuseKey.story.tsx
41 lines (34 loc) · 1.22 KB
/
useKey.story.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
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import { useKey } from '../src';
import { CenterStory } from './util/CenterStory';
import ShowDocs from './util/ShowDocs';
const Demo = () => {
const [count, setCount] = React.useState(0);
const increment = () => setCount((currentCount) => ++currentCount);
const decrement = () => setCount((currentCount) => --currentCount);
const reset = () => setCount(() => 0);
useKey(']', increment);
useKey('[', decrement);
useKey('r', reset);
return (
<CenterStory>
<style dangerouslySetInnerHTML={{ __html: `code {color: red}` }} />
<p>
Try pressing <code>[</code>, <code>]</code>, and <code>r</code> to see the count incremented
and decremented.
</p>
<p>Count: {count}</p>
</CenterStory>
);
};
const CounterDemo = () => {
const [count, setCount] = React.useState(0);
const increment = () => setCount((currentCount) => ++currentCount);
useKey('ArrowUp', increment);
return <div>Press arrow up: {count}</div>;
};
storiesOf('Sensors/useKey', module)
.add('Docs', () => <ShowDocs md={require('../docs/useKey.md')} />)
.add('Demo', () => <Demo />)
.add('Simple counter', () => <CounterDemo />);