简体   繁体   English

如何在 Ant Design Tab 组件中使用 React Router Link 组件

[英]How to use React Router Link component inside an Ant Design Tab component

it's been since a week that I try to resolve a problem to use React Router Link component with Ant Design Tabs component.一周以来,我试图解决一个问题,将 React Router Link 组件与 Ant Design Tabs 组件一起使用。

First I basically followed the nested route section from React Router documentation to create a Switch component with route according to desired path.首先,我基本上遵循 React Router 文档中的嵌套路由部分,根据所需路径创建一个带有路由的 Switch 组件。 As I will have to reuse the Tab component on other components later, I create a basic custom TabMenu component into which I pass an array with tab name to map dynamicaly the required tabs.由于稍后我将不得不在其他组件上重用 Tab 组件,因此我创建了一个基本的自定义 TabMenu 组件,我将一个带有选项卡名称的数组传递到其中以动态映射所需的选项卡。 I tried to wrap a Link component inside the the TabPane component for each mapped value but it doesn't work.我试图在 TabPane 组件中为每个映射值包装一个 Link 组件,但它不起作用。

I saw many solution trying to resolve the problem, especially by passing the Link inside the tab props of TabPane component or using the onChange Tab prop with history hook to push to desired location.我看到许多解决方案试图解决这个问题,特别是通过在 TabPane 组件的选项卡道具内传递链接或使用带有历史挂钩的 onChange Tab 道具推送到所需位置。 I tried both but these solutions seems a little tricky for me and don't really suit me.我都试过,但这些解决方案对我来说似乎有点棘手,并不适合我。

The first only work if I click on the tab text because of the Link only affect the text inside tab and not the entire tab.第一个只有在我单击选项卡文本时才有效,因为链接只影响选项卡内的文本而不是整个选项卡。 The second work too but the use of history in this case does not seem 'conventional' to me.第二部作品也是如此,但在这种情况下使用历史对我来说似乎并不“传统”。

I really hope there is a basic solution only with Link component.我真的希望只有 Link 组件有一个基本的解决方案。 Here is my code.这是我的代码。 Thank you谢谢

import { Layout, Menu } from 'antd'
import { Link } from 'react-router-dom'
import { AimOutlined, BookOutlined, DashboardOutlined } from '@ant-design/icons'

const { SubMenu } = Menu
const { Sider } = Layout

const SideMenu = () => {
  return (
    <Sider width={208} style={{
      marginTop: 64, overflow: 'auto',
      zIndex: 1,
      height: '100vh',
      position: 'fixed',
      left: 0
    }}>
      <Menu defaultSelectedKeys={['topMenuItem1']} defaultOpenKeys={['sub1']} className={'side-menu'} mode="inline">
        <Menu.Item key={'topMenuItem1'} icon={<DashboardOutlined/>}>
          <Link to={'/'}> Tableau de bord </Link>
        </Menu.Item>
        <Menu.Item key={'topMenuItem2'} icon={<BookOutlined/>}>
          <Link to={'/missions-catalog'}>Catalogue de missions</Link>
        </Menu.Item>
        <SubMenu key="sub1" icon={<AimOutlined/>} title="Vos missions">
          <Menu.Item key="1">
            <Link to={'/company-referent-missions'}> Missions personnelles </Link>
          </Menu.Item>
          <Menu.Item key="2">
            <Link to={'/company-referent-missions/documents'}>Documents</Link>
          </Menu.Item>
          <Menu.Item key="3">
            <Link to={'/company-referent-missions/favoris'}>Favoris</Link>
          </Menu.Item>
        </SubMenu>
      </Menu>
    </Sider>

  )
}
export default SideMenu
import { Layout, PageHeader } from 'antd'
import { Route, Switch } from 'react-router'
import DashboardEmployeesMissions from '../pages/Dashboard/DashboardEmployeesMissions'
import MyAccount from '../pages/MyAccount'
import CompanyReferentMissions from '../pages/CompanyReferentMissions'
import DashboardHome from '../pages/Dashboard/DashboardHome'
import BreadcrumbNavigation from './BreadcrumbNavigation'
import MissionsCatalogHome from '../pages/MissionsCatalog/MissionsCatalogHome'

const { Content } = Layout

const MainContent = () => {
  return (
    <Content style={{ marginTop: 64, marginLeft: 208, minHeight: '100vh' }} className="main-content">
      <PageHeader title="Tableau de bord" breadcrumb={<BreadcrumbNavigation/>} subTitle="Accueil"/>
      <div className={'main-container'} style={{ backgroundColor: '#F7FBFC', padding: '24px' }}>
        <Switch>
          <Route exact={true} path={'/'}>
            <DashboardHome/>
          </Route>
          <Route path={'/missions-catalog'}>
            <MissionsCatalogHome/>
          </Route>
          <Route path={'/company-referent-missions'}>
            <CompanyReferentMissions/>
          </Route>
          <Route path={'/employees-missions'}>
            <DashboardEmployeesMissions/>
          </Route>
          <Route path={'/my-account'}>
            <MyAccount/>
          </Route>
        </Switch>
      </div>
    </Content>
  )
}
export default MainContent
import React from 'react'
import MissionListItem from '../components/MissionsListItem'
import { Space } from 'antd'
import Title from 'antd/es/typography/Title'
import { Route, Switch, useRouteMatch } from 'react-router-dom'
import MissionCatalogList from './MissionsCatalog/MissionCatalogList'
import { missionApplicationData, missionCatalogData } from '../helpers/DataSeed'
import TabMenu from '../components/TabMenu'

const referentMissionsTabsName = ['Missions', 'Documents', 'Favoris']

const CompanyReferentMissions = () => {
  let { url, path } = useRouteMatch()

  return (
    <Space direction={'vertical'} style={{ width: '100%' }}>
      <Space direction={'vertical'}>
        <Title level={4}>Mission personnelles</Title>
        <TabMenu tabName={referentMissionsTabsName} tabRouterUrl={url}/>
      </Space>
      <Switch>
        <Route exact={true} path={`${path}`}>
          <Space direction={'vertical'} size={'large'} style={{ width: '100%' }}>
            <MissionListItem headerListTitle={<Title level={3}>Candidatures</Title>}
                             dataSource={missionApplicationData()}/>
            <MissionListItem headerListTitle={<Title level={3}>Missions acceptées</Title>}/>
            <MissionListItem headerListTitle={<Title level={3}>Missions terminées</Title>}/>
          </Space>
        </Route>
        <Route path={`${path}/documents`}>
          <Space direction={'vertical'} size={'large'} style={{ width: '100%' }}>
            <MissionListItem headerListTitle={<Title level={3}>Lettres de missions</Title>}/>
            <MissionListItem headerListTitle={<Title level={3}>Attestations de temps passé</Title>}/>
            <MissionListItem headerListTitle={<Title level={3}>Ressources</Title>}/>
          </Space>
        </Route>
        <Route path={`${path}/favoris`}>
          <Space direction={'vertical'} size={'large'} style={{ width: '100%' }}>
            <MissionCatalogList missionsCatalogData={missionCatalogData}/>
          </Space>
        </Route>
      </Switch>
    </Space>
  )
}

export default CompanyReferentMissions
import { Tabs } from 'antd'
import React from 'react'

const { TabPane } = Tabs

const TabMenu = ({ tabName }) => {
  return (
    <Tabs defaultActiveKey={1}>
      {tabName.map((name, index) => {
        return (
          <TabPane key={index + 1} tab={name}/>
        )
      })}
    </Tabs>

  )
}

export default TabMenu

If you want to show the active tab depending on the location, you can do something like this如果要根据位置显示活动选项卡,可以执行以下操作

import { Tabs } from 'antd'
import React from 'react'
import { useLocation } from 'react-router-dom';

const { TabPane } = Tabs

const TabMenu = ({ tabName }) => {
  const location = useLocation();

  return (
    <Tabs 
        defaultActiveKey={1} 
        activeKey={tabName.find((name) => name === location.pathname)}
    >
      {tabName.map((name, index) => {
        return (
          <TabPane key={index + 1} tab={name}/>
        )
      })}
    </Tabs>

  )
}

export default TabMenu

If you wanted to do something different, please describe in more detail and provide a link to the https://codesandbox.io with a minimal example如果你想做一些不同的事情,请更详细地描述,并提供一个链接到https://codesandbox.io和一个最小的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ANT设计组件,如何在Table组件内部而不是复选框中使用Radio? - ANT Design Component, how to use Radio inside Table component instead of the checkbox? 如何在每个组件中具有“链接”的反应路由器中使用“CSSTransition”? - How to use 'CSSTransition' in react-router that has 'Link' in each component? 如何仅在一个路由器链接中使用组件 - How to use a component in only one router link 如何使用 Javascript 更新 Ant Design React 组件中的样式元素 - How to update style element in Ant Design React component using Javascript 如何在 React 中移除 Ant 设计 Tabs 组件的焦点边框? - How to remove focus border for Ant design Tabs component in React? 如何在 Ant Design (Antd), React js 中获取 Dropdown 组件的值 - How to get value of Dropdown component in Ant Design (Antd), React js 如何在 VueJs 组件中使用路由器推送? - How to use router push inside a VueJs component? wrapperComponentRef未定义-React Ant Design,Form Component - wrappedComponentRef is undefined - React Ant Design, Form Component React Router-在嵌套组件内部安装组件 - React Router - Mounting component inside a nested component 如何在不重定向的 react-router 链接内创建按钮组件? - How to create a button component inside of a react-router Link that does not redirect?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM