Skip to content

Commit

Permalink
Merge pull request #1049 from oasisprotocol/mz/hierarchical-tabs
Browse files Browse the repository at this point in the history
Add support for handling child tab routes
  • Loading branch information
buberdds authored Nov 29, 2023
2 parents 2033c2e + 6f47aa1 commit 05b0445
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions .changelog/1049.trivial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for handling direct child routes in RouterTabs
15 changes: 11 additions & 4 deletions src/app/components/RouterTabs/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useLocation, Outlet } from 'react-router-dom'
import { useLocation, Outlet, useMatches } from 'react-router-dom'
import { NonScrollingRouterLink } from '../NonScrollingRouterLink'
import Tabs from '@mui/material/Tabs'
import Tab from '@mui/material/Tab'
Expand All @@ -18,13 +18,20 @@ function getPathname(tab: { to: string }) {

export function RouterTabs<Context>({ tabs, context }: RouterTabsProps<Context>) {
const { pathname } = useLocation()
const currentTab = tabs.find(tab => getPathname(tab) === pathname)
let targetTab = tabs.find(tab => getPathname(tab) === pathname)
const matches = useMatches()

if (!targetTab) {
/// the last index is the current route, -2 is the first parent in route hierarchy
const parentPathname = matches?.at(-2)?.pathname
targetTab = tabs.find(tab => getPathname(tab) === parentPathname)
}

return (
<>
<Tabs value={currentTab?.to} variant="scrollable" scrollButtons={false}>
<Tabs value={targetTab?.to} variant="scrollable" scrollButtons={false}>
{tabs
.filter(tab => tab === currentTab || tab.visible !== false)
.filter(tab => tab === targetTab || tab.visible !== false)
.map(tab => (
<Tab
key={tab.to}
Expand Down
7 changes: 6 additions & 1 deletion src/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ export const routes: RouteObject[] = [
},
{
path: 'tokens/erc-721',
Component: () => <AccountTokensCard {...useAccountDetailsProps()} type="ERC721" />,
children: [
{
path: '',
Component: () => <AccountTokensCard {...useAccountDetailsProps()} type="ERC721" />,
},
],
},
{
path: 'code',
Expand Down

0 comments on commit 05b0445

Please sign in to comment.