forked from streamich/react-use
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseStateList.story.tsx
34 lines (30 loc) · 1.19 KB
/
useStateList.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
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import { useRef } from 'react';
import { useStateList } from '../src';
import ShowDocs from './util/ShowDocs';
const stateSet = ['first', 'second', 'third', 'fourth', 'fifth'];
const Demo = () => {
const { state, prev, next, setStateAt, setState, currentIndex } = useStateList(stateSet);
const indexInput = useRef<HTMLInputElement>(null);
const stateInput = useRef<HTMLInputElement>(null);
return (
<div>
<pre>
{state} [index: {currentIndex}]
</pre>
<button onClick={() => prev()}>prev</button>
<br />
<button onClick={() => next()}>next</button>
<br />
<input type="text" ref={indexInput} style={{ width: 120 }} />
<button onClick={() => setStateAt((indexInput.current!.value as unknown) as number)}>set state by index</button>
<br />
<input type="text" ref={stateInput} style={{ width: 120 }} />
<button onClick={() => setState(stateInput.current!.value)}> set state by value</button>
</div>
);
};
storiesOf('State|useStateList', module)
.add('Docs', () => <ShowDocs md={require('../docs/useStateList.md')} />)
.add('Demo', () => <Demo />);