-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact-router-stub.tsx
More file actions
66 lines (56 loc) · 2.1 KB
/
react-router-stub.tsx
File metadata and controls
66 lines (56 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import type { Decorator } from '@storybook/react-vite';
import type { ComponentType } from 'react';
import {
type ActionFunction,
createRoutesStub,
type LinksFunction,
type LoaderFunction,
type MetaFunction,
} from 'react-router';
export interface StubRouteObject {
path?: string;
index?: boolean;
loader?: LoaderFunction;
action?: ActionFunction;
meta?: MetaFunction;
links?: LinksFunction;
// biome-ignore lint/suspicious/noExplicitAny: allow any here for Storybook compatibility
Component?: ComponentType<any>;
children?: StubRouteObject[];
// biome-ignore lint/suspicious/noExplicitAny: allow any here for Storybook compatibility
errorElement?: any;
}
interface RemixStubOptions {
routes: StubRouteObject[];
initialPath?: string;
}
export const withReactRouterStubDecorator = (options: RemixStubOptions): Decorator => {
const { routes, initialPath = '/' } = options;
return (Story, context) => {
// Map routes to include the Story component if no Component is provided
const mappedRoutes = routes.map((route) => ({
...route,
Component: route.Component ?? (() => <Story {...context.args} />),
}));
// Get the base path (without existing query params from options)
const basePath = initialPath.split('?')[0];
// Get the current search string from the actual browser window, if available
// If not available, use a default search string with parameters needed for the data table
const currentWindowSearch = typeof window !== 'undefined' ? window.location.search : '?page=0&pageSize=10';
// Combine them for the initial entry
const actualInitialPath = `${basePath}${currentWindowSearch}`;
// Use React Router's official createRoutesStub
const Stub = createRoutesStub(mappedRoutes);
return <Stub initialEntries={[actualInitialPath]} />;
};
};
/**
* A decorator that provides URL state management for stories
* Use this when you need URL query parameters in your stories
*/
export const withURLState = (initialPath = '/'): Decorator => {
return withReactRouterStubDecorator({
routes: [{ path: '/' }],
initialPath,
});
};