1
- import React from 'react' ;
1
+ import React , { CSSProperties } from 'react' ;
2
2
import { Story , Meta } from '@storybook/react' ;
3
3
import ReactTooltip from 'react-tooltip' ;
4
4
import eachDayOfInterval from 'date-fns/eachDayOfInterval' ;
@@ -9,6 +9,17 @@ import ActivityCalendar, { Props } from './ActivityCalendar';
9
9
import { Day , Level , Theme } from '../types' ;
10
10
import { DEFAULT_MONTH_LABELS , DEFAULT_WEEKDAY_LABELS } from '../util' ;
11
11
12
+ const styles : {
13
+ [ elem : string ] : CSSProperties ;
14
+ } = {
15
+ code : {
16
+ fontSize : '0.9rem' ,
17
+ } ,
18
+ p : {
19
+ maxWidth : '68ch' ,
20
+ } ,
21
+ } ;
22
+
12
23
export default {
13
24
title : 'Activity Calendar' ,
14
25
component : ActivityCalendar ,
@@ -63,8 +74,9 @@ const Template: Story<Props> = args => <ActivityCalendar {...args} />;
63
74
const TemplateLocalized : Story < Props > = args => (
64
75
< >
65
76
< h1 > Localization</ h1 >
77
+ < p > (Example in German)</ p >
66
78
< ActivityCalendar { ...args } style = { { margin : '2rem 0' } } />
67
- < pre >
79
+ < pre style = { styles . code } >
68
80
{ `
69
81
// Shape of \`labels\` property (default values).
70
82
// All properties are optional.
@@ -104,6 +116,40 @@ const labels = {
104
116
</ >
105
117
) ;
106
118
119
+ const TemplateEventHandlers : Story < Props > = args => (
120
+ < >
121
+ < h1 > Event Handlers</ h1 >
122
+ < p style = { styles . p } >
123
+ You can register event handlers for the SVG < code style = { styles . code } > <rect/></ code > { ' ' }
124
+ elements that are used to render the calendar days. This way you can control the behaviour on
125
+ click, hover, etc.
126
+ </ p >
127
+ < p style = { styles . p } >
128
+ All event listeners have the following signature, so you are able to use the shown data inside
129
+ the handler:
130
+ </ p >
131
+ < p style = { styles . p } >
132
+ < code style = { styles . code } > (event: React.SyntheticEvent) => (data: Day) => void</ code >
133
+ </ p >
134
+ < p style = { styles . p } > Click on any block below to see it in action:</ p >
135
+ < ActivityCalendar { ...args } style = { { margin : '2rem 0' } } />
136
+ < pre style = { styles . code } >
137
+ { `
138
+ <ActivityCalendar
139
+ data={data}
140
+ eventHandlers: {
141
+ onClick: event => data => {
142
+ console.log({ event, data });
143
+ alert(JSON.stringify(data, null, 4));
144
+ },
145
+ onMouseEnter: event => data => console.log('mouseEnter'),
146
+ }
147
+ />
148
+ ` }
149
+ </ pre >
150
+ </ >
151
+ ) ;
152
+
107
153
const theme : Theme = {
108
154
level0 : '#F0F0F0' ,
109
155
level1 : '#C4EDDE' ,
@@ -211,6 +257,18 @@ WithLocalizedLabels.args = {
211
257
} ,
212
258
} ;
213
259
260
+ const eventHandlerData = generateData ( ) ;
261
+ export const EventHandlers = TemplateEventHandlers . bind ( { } ) ;
262
+ EventHandlers . args = {
263
+ data : eventHandlerData ,
264
+ eventHandlers : {
265
+ onClick : event => data => {
266
+ console . log ( { event, data } ) ;
267
+ alert ( JSON . stringify ( data , null , 4 ) ) ;
268
+ } ,
269
+ } ,
270
+ } ;
271
+
214
272
function generateData ( monthStart = 0 , monthEnd = 11 ) : Array < Day > {
215
273
const MAX = 10 ;
216
274
const LEVELS = 5 ;
0 commit comments