|
| 1 | +import type { Meta, StoryObj } from '@storybook/react-vite' |
| 2 | +import { RouterTabs, RouterTab } from '../../components/tabs/router-tabs' |
| 3 | +import { expect, within } from 'storybook/test' |
| 4 | + |
| 5 | +// Mock router link component for demonstration |
| 6 | +const MockRouterLink = ({ |
| 7 | + to, |
| 8 | + className, |
| 9 | + children, |
| 10 | + ...props |
| 11 | +}: { |
| 12 | + to: string |
| 13 | + className?: string |
| 14 | + children: React.ReactNode |
| 15 | +}) => ( |
| 16 | + <a href={to} className={className} {...props}> |
| 17 | + {children} |
| 18 | + </a> |
| 19 | +) |
| 20 | + |
| 21 | +const meta: Meta<typeof RouterTabs> = { |
| 22 | + title: 'Components/RouterTabs', |
| 23 | + component: RouterTabs, |
| 24 | + parameters: { |
| 25 | + docs: { |
| 26 | + description: { |
| 27 | + component: |
| 28 | + 'A router-aware tabs component that mimics MUI tabs functionality. Supports routing with custom router link components and scrollable variants.', |
| 29 | + }, |
| 30 | + }, |
| 31 | + layout: 'centered', |
| 32 | + }, |
| 33 | + tags: ['autodocs'], |
| 34 | + argTypes: { |
| 35 | + value: { |
| 36 | + control: 'text', |
| 37 | + description: 'The currently active tab value', |
| 38 | + }, |
| 39 | + variant: { |
| 40 | + control: 'select', |
| 41 | + options: ['default', 'scrollable'], |
| 42 | + description: 'The variant of the tabs', |
| 43 | + }, |
| 44 | + scrollButtons: { |
| 45 | + control: 'boolean', |
| 46 | + description: 'Whether to show scroll buttons in scrollable variant', |
| 47 | + }, |
| 48 | + }, |
| 49 | +} |
| 50 | + |
| 51 | +export default meta |
| 52 | +type Story = StoryObj<typeof meta> |
| 53 | + |
| 54 | +// Sample tabs data similar to MUI example |
| 55 | +const tabs = [ |
| 56 | + { to: '/dashboard', label: 'Dashboard', visible: true }, |
| 57 | + { to: '/analytics', label: 'Analytics', visible: true }, |
| 58 | + { to: '/reports', label: 'Reports', visible: true }, |
| 59 | + { to: '/settings', label: 'Settings', visible: false }, |
| 60 | + { to: '/profile', label: 'Profile', visible: true }, |
| 61 | +] |
| 62 | + |
| 63 | +const targetTab = tabs.find(tab => tab.to === '/analytics') |
| 64 | + |
| 65 | +export const Default: Story = { |
| 66 | + args: { |
| 67 | + value: targetTab?.to, |
| 68 | + children: tabs |
| 69 | + .filter(tab => tab === targetTab || tab.visible !== false) |
| 70 | + .map(tab => ( |
| 71 | + <RouterTab key={tab.to} component={MockRouterLink} value={tab.to} label={tab.label} to={tab.to} /> |
| 72 | + )), |
| 73 | + }, |
| 74 | + play: async ({ canvasElement }) => { |
| 75 | + const canvas = within(canvasElement) |
| 76 | + const tabsList = canvas.getByRole('tablist') |
| 77 | + await expect(tabsList).toBeInTheDocument() |
| 78 | + }, |
| 79 | +} |
| 80 | + |
| 81 | +export const ScrollableVariant: Story = { |
| 82 | + args: { |
| 83 | + value: '/analytics', |
| 84 | + variant: 'scrollable', |
| 85 | + scrollButtons: false, |
| 86 | + children: [ |
| 87 | + { to: '/dashboard', label: 'Dashboard' }, |
| 88 | + { to: '/analytics', label: 'Analytics' }, |
| 89 | + { to: '/reports', label: 'Reports' }, |
| 90 | + { to: '/users', label: 'User Management' }, |
| 91 | + { to: '/billing', label: 'Billing & Payments' }, |
| 92 | + { to: '/integrations', label: 'Integrations' }, |
| 93 | + { to: '/security', label: 'Security Settings' }, |
| 94 | + { to: '/notifications', label: 'Notifications' }, |
| 95 | + ].map(tab => ( |
| 96 | + <RouterTab key={tab.to} component={MockRouterLink} value={tab.to} label={tab.label} to={tab.to} /> |
| 97 | + )), |
| 98 | + }, |
| 99 | +} |
| 100 | + |
| 101 | +export const WithButtonTabs: Story = { |
| 102 | + args: { |
| 103 | + value: '/dashboard', |
| 104 | + children: [ |
| 105 | + <RouterTab key="/dashboard" value="/dashboard" label="Dashboard" to="/dashboard" />, |
| 106 | + <RouterTab key="/analytics" value="/analytics" label="Analytics" to="/analytics" />, |
| 107 | + <RouterTab key="/reports" value="/reports" label="Reports" to="/reports" />, |
| 108 | + ], |
| 109 | + }, |
| 110 | +} |
| 111 | + |
| 112 | +export const MUILikeExample: Story = { |
| 113 | + name: 'MUI-like Implementation', |
| 114 | + args: { |
| 115 | + value: targetTab?.to, |
| 116 | + variant: 'scrollable', |
| 117 | + scrollButtons: false, |
| 118 | + children: tabs |
| 119 | + .filter(tab => tab === targetTab || tab.visible !== false) |
| 120 | + .map(tab => ( |
| 121 | + <RouterTab key={tab.to} component={MockRouterLink} value={tab.to} label={tab.label} to={tab.to} /> |
| 122 | + )), |
| 123 | + }, |
| 124 | + parameters: { |
| 125 | + docs: { |
| 126 | + description: { |
| 127 | + story: `This example demonstrates how to replicate the MUI tabs pattern: |
| 128 | + |
| 129 | +\`\`\`tsx |
| 130 | +<RouterTabs value={targetTab?.to} variant="scrollable" scrollButtons={false}> |
| 131 | + {tabs |
| 132 | + .filter(tab => tab === targetTab || tab.visible !== false) |
| 133 | + .map(tab => ( |
| 134 | + <RouterTab |
| 135 | + key={tab.to} |
| 136 | + component={NonScrollingRouterLink} |
| 137 | + value={tab.to} |
| 138 | + label={tab.label} |
| 139 | + to={tab.to} |
| 140 | + /> |
| 141 | + ))} |
| 142 | +</RouterTabs> |
| 143 | +\`\`\``, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | +} |
0 commit comments