Skip to content

Commit bb1f9df

Browse files
committed
Allow setting a React ref
1 parent f63f70f commit bb1f9df

File tree

5 files changed

+318
-265
lines changed

5 files changed

+318
-265
lines changed

.storybook/main.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ const config: StorybookConfig = {
1616
'@storybook/addon-links',
1717
'storybook-dark-mode',
1818
],
19-
docs: {
20-
autodocs: true,
21-
},
2219
typescript: {
2320
reactDocgen: 'react-docgen',
2421
},

examples/ref.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @ts-nocheck
2+
import { useRef } from "react";
3+
4+
const calendarRef = useRef<HTMLElement>(null);
5+
6+
if (calendar.current) {
7+
console.log(calendarRef.current);
8+
}
9+
10+
<ActivityCalendar data={data} ref={calendarRef} />

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-activity-calendar",
3-
"version": "2.2.11",
3+
"version": "2.3.0",
44
"description": "React component to display activity data in calendar",
55
"author": "Jonathan Gruber <gruberjonathan@gmail.com>",
66
"license": "MIT",

src/component/ActivityCalendar.stories.tsx

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Tooltip as MuiTooltip } from '@mui/material';
22
import LinkTo from '@storybook/addon-links/react';
33
import type { Meta, StoryObj } from '@storybook/react';
44
import { Highlight, themes as prismThemes } from 'prism-react-renderer';
5-
import { cloneElement, useMemo } from 'react';
5+
import { type ForwardedRef, cloneElement, useMemo, useRef } from 'react';
66
import { Tooltip as ReactTooltip } from 'react-tooltip';
77
import 'react-tooltip/dist/react-tooltip.css';
88
import { useDarkMode } from 'storybook-dark-mode';
@@ -13,6 +13,7 @@ import exampleEventHandlersInterface from '../../examples/event-handlers-type?ra
1313
import exampleEventHandlers from '../../examples/event-handlers?raw';
1414
import exampleLabelsShape from '../../examples/labels-shape?raw';
1515
import exampleLabels from '../../examples/labels?raw';
16+
import exampleRef from '../../examples/ref?raw';
1617
import exampleThemeExplicit from '../../examples/themes-explicit?raw';
1718
import exampleTheme from '../../examples/themes?raw';
1819
import exampleTooltipsMui from '../../examples/tooltips-mui?raw';
@@ -25,22 +26,9 @@ type Story = StoryObj<Props>;
2526

2627
/* eslint-disable react-hooks/rules-of-hooks */
2728

28-
const meta: Meta<Props> = {
29+
const meta: Meta<ForwardedRef<Props>> = {
2930
title: 'React Activity Calendar',
3031
component: ActivityCalendar,
31-
decorators: [
32-
(Story, { args }) => {
33-
args.colorScheme = useDarkMode() ? 'dark' : 'light';
34-
35-
return <Story />;
36-
},
37-
],
38-
parameters: {
39-
controls: {
40-
sort: 'requiredFirst',
41-
},
42-
layout: 'centered',
43-
},
4432
argTypes: {
4533
data: {
4634
control: false,
@@ -63,6 +51,9 @@ const meta: Meta<Props> = {
6351
maxLevel: {
6452
control: { type: 'range', min: 1, max: 9 },
6553
},
54+
ref: {
55+
control: false,
56+
},
6657
style: {
6758
control: false,
6859
},
@@ -82,6 +73,21 @@ const meta: Meta<Props> = {
8273
},
8374
},
8475
},
76+
decorators: [
77+
(Story, { args }) => {
78+
// @ts-expect-error unsure if typing forward refs correctly is possible
79+
args.colorScheme = useDarkMode() ? 'dark' : 'light';
80+
81+
return <Story />;
82+
},
83+
],
84+
parameters: {
85+
controls: {
86+
sort: 'requiredFirst',
87+
},
88+
layout: 'centered',
89+
},
90+
tags: ['autodocs'],
8591
};
8692

8793
// Storybook does not initialize the controls for some reason
@@ -559,6 +565,30 @@ export const NarrowScreens: Story = {
559565
},
560566
};
561567

568+
export const ContainerRef: Story = {
569+
args: defaultProps,
570+
parameters: {
571+
docs: {
572+
source: {
573+
code: exampleRef,
574+
},
575+
},
576+
},
577+
render: args => {
578+
const data = useMemo(() => generateTestData({ maxLevel: args.maxLevel }), [args.maxLevel]);
579+
const calendarRef = useRef<HTMLElement>(null);
580+
console.log('calendar ref', calendarRef);
581+
582+
return (
583+
<>
584+
<ActivityCalendar {...args} data={data} ref={calendarRef} />
585+
<br />
586+
<p>Check the JavaScript console to see the ref logged.</p>
587+
</>
588+
);
589+
},
590+
};
591+
562592
const Source = ({
563593
code,
564594
isDarkMode,

0 commit comments

Comments
 (0)