forked from streamich/react-use
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuseStateValidator.story.tsx
31 lines (28 loc) · 998 Bytes
/
useStateValidator.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
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import useStateValidator from '../src/useStateValidator';
import ShowDocs from './util/ShowDocs';
const DemoStateValidator = (s) =>
[s === '' ? undefined : (s * 1) % 2 === 0] as [boolean | undefined];
const Demo = () => {
const [state, setState] = React.useState<number>(0);
const [[isValid]] = useStateValidator(state, DemoStateValidator);
return (
<div>
<div>Below field is valid only if number is even</div>
<input
type="number"
min="0"
max="10"
value={state}
onChange={(ev: React.ChangeEvent<HTMLInputElement>) => {
setState(ev.target.value as unknown as number);
}}
/>
{isValid !== undefined && <span>{isValid ? 'Valid!' : 'Invalid'}</span>}
</div>
);
};
storiesOf('State/useStateValidator', module)
.add('Docs', () => <ShowDocs md={require('../docs/useStateValidator.md')} />)
.add('Demo', () => <Demo />);