|
| 1 | +import * as React from 'react'; |
| 2 | +import type { Meta, StoryObj } from '@storybook/react'; |
| 3 | +import { Breadcrumb, BreadcrumbPath, Button, Heading } from '@textkernel/oneui'; |
| 4 | +import { MemoryRouter, Navigate, NavLink, Route, Routes, useLocation } from 'react-router-dom'; |
| 5 | +import { MdHome } from 'react-icons/md'; |
| 6 | + |
| 7 | +const meta: Meta<typeof Breadcrumb> = { |
| 8 | + title: 'Atoms/Breadcrumb', |
| 9 | + component: Breadcrumb, |
| 10 | +}; |
| 11 | + |
| 12 | +export default meta; |
| 13 | + |
| 14 | +type Story = StoryObj<typeof Breadcrumb>; |
| 15 | + |
| 16 | +export const _Breadcrumb: Story = { |
| 17 | + name: 'Breadcrumb', |
| 18 | + decorators: [ |
| 19 | + (Story) => ( |
| 20 | + <MemoryRouter initialIndex={0}> |
| 21 | + <Story /> |
| 22 | + </MemoryRouter> |
| 23 | + ), |
| 24 | + ], |
| 25 | + render: () => { |
| 26 | + const location = useLocation(); |
| 27 | + const pathnames = location.pathname.split('/').filter((p) => p); |
| 28 | + const paths: BreadcrumbPath[] = [ |
| 29 | + { href: '/', label: 'Home', icon: <MdHome /> }, |
| 30 | + ...pathnames.map((name, index) => ({ |
| 31 | + href: `/${pathnames.slice(0, index + 1).join('/')}`, |
| 32 | + label: name.charAt(0).toUpperCase() + name.slice(1), |
| 33 | + })), |
| 34 | + ]; |
| 35 | + |
| 36 | + return ( |
| 37 | + <div> |
| 38 | + <Breadcrumb |
| 39 | + paths={paths} |
| 40 | + linkRenderer={(path) => <NavLink to={path.href}>{path.label}</NavLink>} |
| 41 | + /> |
| 42 | + <div style={{ display: 'flex', flexDirection: 'column', gap: 8, marginTop: 48 }}> |
| 43 | + <Heading level="h3">Navigation helpers</Heading> |
| 44 | + <div style={{ display: 'flex', gap: 8 }}> |
| 45 | + <NavLink to="/"> |
| 46 | + <Button>Go to Home</Button> |
| 47 | + </NavLink> |
| 48 | + <NavLink to="/compare"> |
| 49 | + <Button>Go to Compare</Button> |
| 50 | + </NavLink> |
| 51 | + <NavLink to="/compare/details"> |
| 52 | + <Button>Go to Compare Details</Button> |
| 53 | + </NavLink> |
| 54 | + </div> |
| 55 | + <div style={{ display: 'flex', gap: 8 }}> |
| 56 | + <span>Current route:</span> |
| 57 | + <Routes> |
| 58 | + <Route |
| 59 | + path="/compare/details" |
| 60 | + element={<span>/compare/details</span>} |
| 61 | + /> |
| 62 | + <Route path="/compare" element={<span>/compare</span>} /> |
| 63 | + <Route path="/" element={<span>/</span>} /> |
| 64 | + <Route path="*" element={<Navigate to="/" replace />} /> |
| 65 | + </Routes> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + ); |
| 70 | + }, |
| 71 | +}; |
0 commit comments