mirror of
https://github.com/streamich/react-use.git
synced 2026-02-01 14:37:31 +00:00
Also reduce max line width to 100. And remove `lint:types` step for commit sequence, it bothers when committing incomplete (wip) changes.
32 lines
1000 B
TypeScript
32 lines
1000 B
TypeScript
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 />);
|