Skip to content

Commit 4c4e34b

Browse files
authored
docs: improve documentation with knobs (#285)
* docs: improve documentation with Storybook knobs * docs: add more knobs to tweak props * docs: add usage with Form inside new stories
1 parent 4d9ffb1 commit 4c4e34b

File tree

8 files changed

+313
-340
lines changed

8 files changed

+313
-340
lines changed

.storybook/addons.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
import '@storybook/addon-knobs/register';
12
import '@storybook/addon-actions/register';
23
import '@storybook/addon-links/register';

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"devDependencies": {
4747
"@babel/core": "7.9.6",
4848
"@storybook/addon-actions": "5.3.19",
49+
"@storybook/addon-knobs": "5.3.19",
4950
"@storybook/addon-links": "5.3.19",
5051
"@storybook/addons": "5.3.19",
5152
"@storybook/react": "5.3.19",

stories/basic.stories.tsx

Lines changed: 0 additions & 177 deletions
This file was deleted.

stories/common.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,24 @@ type ContentProps = {
55
};
66

77
export const Content: React.FC<ContentProps> = ({ children, style }) => (
8-
<div style={{ display: 'flex', flex: 1, justifyContent: 'center', ...style }}>
8+
<div
9+
style={{
10+
display: 'flex',
11+
position: 'absolute',
12+
height: '100%',
13+
width: '100%',
14+
alignItems: 'center',
15+
justifyContent: 'center',
16+
...style,
17+
}}
18+
>
919
{children}
1020
</div>
1121
);
22+
23+
export const onChange = (_: any, data: any) =>
24+
console.log('[react-semantic-ui-datepickers]\n', data);
25+
26+
export const isWeekday = (date: Date) => ![0, 6].includes(date.getDay());
27+
28+
export * from './data';

stories/data.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const types = <const>['basic', 'range'];
2+
const pointing = <const>['left', 'right', 'top left', 'top right'];
3+
const locale = <const>[
4+
'bg-BG',
5+
'ca-ES',
6+
'cs-CZ',
7+
'de-DE',
8+
'en-US',
9+
'es-ES',
10+
'fi-FL',
11+
'fr-FR',
12+
'he-IL',
13+
'it-IT',
14+
'ja-JP',
15+
'nb-NO',
16+
'pl-PL',
17+
'pt-BR',
18+
'ru-RU',
19+
'sv-SE',
20+
'tr-TR',
21+
'zh-CN',
22+
];
23+
24+
function arrayToMap<T extends string>(
25+
arr: readonly T[]
26+
): { [key in typeof arr[number]]: typeof arr[number] } {
27+
return arr.reduce(
28+
(acc, cur) => ({ ...acc, [cur]: cur }),
29+
{} as { [key in T]: T }
30+
);
31+
}
32+
33+
export const typeMap = arrayToMap(types);
34+
35+
export const pointingMap = arrayToMap(pointing);
36+
37+
export const localeMap = arrayToMap([...locale].sort());

stories/index.stories.tsx

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import React from 'react';
2+
import {
3+
withKnobs,
4+
boolean,
5+
date,
6+
number,
7+
select,
8+
text,
9+
} from '@storybook/addon-knobs';
10+
import { storiesOf } from '@storybook/react';
11+
import { Form } from 'semantic-ui-react';
12+
import 'semantic-ui-css/semantic.min.css';
13+
import SemanticDatepicker from '../src';
14+
import {
15+
Content,
16+
isWeekday,
17+
localeMap,
18+
onChange,
19+
pointingMap,
20+
typeMap,
21+
} from './common';
22+
// import { parseISO } from 'date-fns';
23+
24+
const stories = storiesOf('Datepickers', module);
25+
26+
stories.addDecorator(withKnobs);
27+
stories.addParameters({
28+
info: {
29+
disable: true,
30+
},
31+
options: {
32+
panelPosition: 'right',
33+
},
34+
});
35+
36+
stories.add('Basic usage', () => {
37+
const type = select('Type', typeMap, typeMap.basic);
38+
const allowOnlyNumbers = boolean('Allow only numbers', false);
39+
const clearOnSameDateClick = boolean('Clear on same date click', true);
40+
const datePickerOnly = boolean('Datepicker only', false);
41+
const firstDayOfWeek = number('First day of week', 0, { max: 6, min: 0 });
42+
const format = text('Format', 'YYYY-MM-DD');
43+
const keepOpenOnClear = boolean('Keep open on clear', false);
44+
const keepOpenOnSelect = boolean('Keep open on select', false);
45+
const locale = select('Locale', localeMap, localeMap['en-US']);
46+
const pointing = select('Pointing', pointingMap, pointingMap.left);
47+
const clearable = boolean('Clearable', true);
48+
const readOnly = boolean('Read-only', false);
49+
const showOutsideDays = boolean('Show outside days', false);
50+
const minDate = new Date(date('Min date', new Date('2018-01-01')));
51+
const maxDate = new Date(date('Max date', new Date('2030-01-01')));
52+
const onlyWeekdays = boolean('Only weekdays (filterDate example)', false);
53+
const controlValue = boolean('Control value', false);
54+
const initialValue = controlValue
55+
? type === 'basic'
56+
? new Date(date('Initial value'))
57+
: [new Date(date('Initial value')), new Date(date('Final value'))]
58+
: undefined;
59+
const filterDate = onlyWeekdays ? isWeekday : undefined;
60+
const key = type + format + readOnly;
61+
62+
return (
63+
<Content>
64+
<SemanticDatepicker
65+
key={key}
66+
allowOnlyNumbers={allowOnlyNumbers}
67+
clearOnSameDateClick={clearOnSameDateClick}
68+
clearable={clearable}
69+
datePickerOnly={datePickerOnly}
70+
filterDate={filterDate}
71+
firstDayOfWeek={firstDayOfWeek}
72+
format={format}
73+
keepOpenOnClear={keepOpenOnClear}
74+
keepOpenOnSelect={keepOpenOnSelect}
75+
locale={locale}
76+
maxDate={maxDate}
77+
minDate={minDate}
78+
onChange={onChange}
79+
pointing={pointing}
80+
readOnly={readOnly}
81+
showOutsideDays={showOutsideDays}
82+
type={type}
83+
value={initialValue}
84+
/>
85+
</Content>
86+
);
87+
});
88+
89+
stories.add('Usage with Form', () => {
90+
return (
91+
<Content>
92+
<Form>
93+
<Form.Group width="equals">
94+
<SemanticDatepicker
95+
label="Initial date"
96+
id="initialDate"
97+
onChange={onChange}
98+
required
99+
/>
100+
<SemanticDatepicker
101+
label="Final date"
102+
id="finalDate"
103+
onChange={onChange}
104+
/>
105+
</Form.Group>
106+
</Form>
107+
</Content>
108+
);
109+
});

0 commit comments

Comments
 (0)