From 8a97df577b1060b9e8f7f0bf01bc76bea7b75c7e Mon Sep 17 00:00:00 2001 From: Edward Purwanto Date: Tue, 7 May 2024 00:25:13 +1000 Subject: [PATCH] Some example stories --- src/stories/v3/ComponentWithLink.stories.tsx | 47 +++++++++++ src/stories/v3/FrameWithOutlet.stories.tsx | 83 ++++++++++++++++++++ src/stories/v3/NavMenu.css | 16 ++++ src/stories/v3/NavMenu.stories.tsx | 74 +++++++++++++++++ 4 files changed, 220 insertions(+) create mode 100644 src/stories/v3/ComponentWithLink.stories.tsx create mode 100644 src/stories/v3/FrameWithOutlet.stories.tsx create mode 100644 src/stories/v3/NavMenu.css create mode 100644 src/stories/v3/NavMenu.stories.tsx diff --git a/src/stories/v3/ComponentWithLink.stories.tsx b/src/stories/v3/ComponentWithLink.stories.tsx new file mode 100644 index 0000000..ae2530a --- /dev/null +++ b/src/stories/v3/ComponentWithLink.stories.tsx @@ -0,0 +1,47 @@ +import { Meta, StoryObj } from '@storybook/react'; +import React from 'react'; +import { Link } from 'react-router-dom'; +import { reactRouterParameters } from '../../features/decorator/utils/routesHelpers/reactRouterParameters'; +import { withRouter } from '../../features/decorator/withRouter'; + +const ComponentWithLinkFn = () => ( +
+ This link takes you away from the component +
+); + +const meta: Meta = { + title: 'v3/ComponentWithLink', + component: ComponentWithLinkFn, + parameters: { + reactRouter: reactRouterParameters({ + location: { path: '/component' }, + routing: [ + { + // The component links to some peer outside it's subtree + path: 'component', + useStoryElement: true, + }, + { + path: 'some-page', + element: ( +
+

This is not the component being storied

+
    +
  • + go back +
  • +
+
+ ), + }, + ], + }), + }, + decorators: [withRouter], +}; +export default meta; + +type Story = StoryObj; + +export const ComponentWithLink: Story = {}; diff --git a/src/stories/v3/FrameWithOutlet.stories.tsx b/src/stories/v3/FrameWithOutlet.stories.tsx new file mode 100644 index 0000000..44e98ce --- /dev/null +++ b/src/stories/v3/FrameWithOutlet.stories.tsx @@ -0,0 +1,83 @@ +import { Meta, StoryObj } from '@storybook/react'; +import React from 'react'; +import { Link, Outlet, useLocation } from 'react-router-dom'; +import { reactRouterParameters } from '../../features/decorator/utils/routesHelpers/reactRouterParameters'; +import { withRouter } from '../../features/decorator/withRouter'; + +const FrameWithOutlet = () => { + const { pathname } = useLocation(); + + return ( +
+

This is some context out of the page.

+

The current path is: {pathname}

+ +
+ ); +}; + +const FrameContent = ({ label, backUrl }: { label: string; backUrl: string }) => ( +
+

{label}

+
    +
  • + go back +
  • +
+
+); + +const meta: Meta = { + component: FrameWithOutlet, + parameters: { + reactRouter: reactRouterParameters({ + routing: { + // Render the story as the root element, with a couple of example children + path: '/', + useStoryElement: true, + children: [ + { + index: true, + element: ( +
+

This is the index content

+
    +
  • + First Child +
  • +
  • + Second Child +
  • +
+
+ ), + }, + { + path: 'first', + element: , + }, + { + path: 'second', + element: , + }, + ], + }, + }), + }, + decorators: [withRouter], +}; +export default meta; + +type Story = StoryObj; + +export const Default: Story = {}; + +export const StartsOnChild: Story = { + parameters: { + reactRouter: reactRouterParameters({ + location: { + path: '/first', + }, + }), + }, +}; diff --git a/src/stories/v3/NavMenu.css b/src/stories/v3/NavMenu.css new file mode 100644 index 0000000..34da600 --- /dev/null +++ b/src/stories/v3/NavMenu.css @@ -0,0 +1,16 @@ +nav { + display: flex; + flex-flow: column; +} + +a { + color: blue; +} + +a.active { + color: red; +} + +.sub-menu { + margin-left: 8px; +} diff --git a/src/stories/v3/NavMenu.stories.tsx b/src/stories/v3/NavMenu.stories.tsx new file mode 100644 index 0000000..34c7bdc --- /dev/null +++ b/src/stories/v3/NavMenu.stories.tsx @@ -0,0 +1,74 @@ +import { Meta, StoryObj } from '@storybook/react'; +import React from 'react'; +import { NavLink, useLocation } from 'react-router-dom'; +import { reactRouterParameters } from '../../features/decorator/utils/routesHelpers/reactRouterParameters'; +import { withRouter } from '../../features/decorator/withRouter'; +import './NavMenu.css'; + +const NavMenu = () => ( + +); + +const meta: Meta = { + component: NavMenu, + render: () => { + // eslint-disable-next-line react-hooks/rules-of-hooks + const { pathname } = useLocation(); + + return ( + <> + +

Current path is: {pathname}

+ + ); + }, + parameters: { + reactRouter: reactRouterParameters({ + routing: { + useStoryElement: true, + path: '*', // Regardless of the current path, render the story + }, + }), + }, + decorators: [withRouter], +}; +export default meta; + +type Story = StoryObj; + +export const Default: Story = {}; + +export const Child: Story = { + parameters: { + reactRouter: reactRouterParameters({ + location: { + path: '/parent/child2', + }, + }), + }, +}; + +export const ExactChild: Story = { + parameters: { + reactRouter: reactRouterParameters({ + location: { + path: '/exact-parent/child', + }, + }), + }, +};