谷歌Chrome浏览器风格的标签组件

选用技术
  • react
  • typescript
  • redux-saga存储本地标签数据
  • umi
实现
  • [x] 支持全部关闭,当前关闭,关闭其他Tab
  • [x] 支持Tab过多的自适应
  • [x] chrome风格
  • [x] 内圆角css
  1. 使用
import MenuTab from '@components/MenuTab'

<MenuTab location={props.location} />
  1. 代码
/**
* 菜单标签组件
* createDate: 2020年04月29日
*/
// menus数据结构:
// {
// title: '资料管理',
// path: '/DataManage',
// subs: [
// {
// title: '商品资料',
// path: '/Goods',
// fullPath: '/DataManage/Goods',
// },
// {
// title: '供应商查询',
// path: '/Suppliers',
// fullPath: '/DataManage/Suppliers',
// },
// ],
// },
import React, { useEffect, useRef, useState, useMemo } from 'react'
import { useHistory } from 'react-router-dom'
import { connect } from 'react-redux'
import classNames from 'classnames'
import { CloseOutlined, MoreOutlined } from '@ant-design/icons'
import menus from '@config/menus'
import { Popover } from 'antd'
import style from './Style.less' interface Iprop {
location: any
MenuTabStore: any
} interface ITab {
name: string
path: string
} /** 可优化点:redux匹配好存本地而不是每次打开页面都遍历,不过性能差距不大
* 根据path匹配name,返回
* @param menu 路由菜单
* @param path 当前路由
* @return ITab
*/
const matchRouterPath = (menu: any[], path: string, menuList: Array) => {
for (let item of menu) {
if (item.fullPath === path) {
menuList.push({
name: item.title,
path: item.fullPath
})
break;
}
// 为编辑页和详情页做补充
if (path.indexOf(item.fullPath) !== -1) {
if (path.indexOf('/Detail') !== -1) {
menuList.push({
name: item.title + '详情页',
path: path
})
}
if (path.indexOf('/Edit') !== -1) {
menuList[0] = {
name: item.title + '编辑/新增页', // 动态路由控制,为避免不和其他地方冲突,暂时写死
path: path
}
}
if (path.indexOf('/New') !== -1) {
menuList.push({
name: item.title + '新增页',
path: path
})
}
} if (item.fullPath !== path && item.subs && item.subs.length > 0) {
matchRouterPath(item.subs, path, menuList)
}
}
if (menuList.length) {
return menuList[0];
}
} const MenuTab = (props: Iprop) => {
const { location, dispatch = () => { }, MenuTabStore } = props
const { menuTabList } = MenuTabStore
const { pathname } = location
const dashboardPath = '/Home'
const history = useHistory()
const cTablimit = 0 // tab控制格式
const menuTabRef: any = useRef(null)
const [currentTabObj, setCurrentTabObj] = useState()
const isTabShow = pathname.match(/[/]/g).length > 1 // 暂定 二级路由才显示tabList // 是否当前tab
const isTabActive = (e: string) => {
return classNames(style.tab, {
[style.menuTabActive]: e === pathname
})
} const updateMenuTab = (tabList: any[]) => {
dispatch({
type: 'MenuTabStore/updateTabList',
payload: tabList
})
} useEffect(() => {
setCurrentTabObj(matchRouterPath(menus, pathname, []) || {})
}, [pathname]) // 路由变化才监听 useEffect(() => {
if (currentTabObj && Object.keys(currentTabObj).length) { // 不匹配的路由则无法更新到store
if (!menuTabList.length) { // tabs为空则直接concat
updateMenuTab(menuTabList.concat([currentTabObj]))
return
}
const isTabExist = menuTabList.filter(e => e.path === pathname)
if (isTabExist.length) { // tab已存在,不处理
return;
} else {
updateMenuTab(menuTabList.concat([currentTabObj]))
}
}
}, [currentTabObj]) // 当前tab存在才监听 // 切换tab
const toggleTab = (e: string) => {
history.replace(e)
} // tab控制是否显示
const isTabControlShow = () => {
if (menuTabList && menuTabList.length > cTablimit) {
return style.menuTabWrap + ' ' + style.showTabcontrol
}
return style.menuTabWrap
} // 关闭tab
const closeTabs: void = (e: any, type: string) => {
e.stopPropagation()
const clearTab = () => {
updateMenuTab([])
history.push(dashboardPath)
}
switch (type) {
case 'current':
if (menuTabList && menuTabList.length) {
const _i = menuTabList.findIndex((v) => v.path === pathname)
if (menuTabList && menuTabList.length > 1) {
const _arr = menuTabList.filter((e, i) => {
return i !== _i
})
updateMenuTab(_arr)
if (_arr.length === 1) { // 如果当前只剩一个tab
history.push(_arr[0].path)
} else {
history.goBack()
}
} else {
clearTab()
}
}
break;
case 'others':
updateMenuTab([
{
name: currentTabObj.name,
path: pathname
}
])
break;
case 'all':
clearTab()
break; default:
break;
}
} // 关闭指定tab
const btnCloseTab = (e: any, path: string) => {
e.stopPropagation()
if (currentTabObj.path === path) { // 当前
closeTabs(e, 'current')
return;
}
const _i = menuTabList.findIndex((v) => v.path === path)
const _arr = menuTabList.filter((e, i) => {
return i !== _i
})
updateMenuTab(_arr)
} const tabControlContent = (
<div className={style.tabControlPop}>
<div
onClick={(e) => {
closeTabs(e, 'current')
}}>
关闭当前标签页
</div>
<div
onClick={(e) => {
closeTabs(e, 'others')
}}>
关闭其他标签页
</div>
<div
onClick={(e) => {
closeTabs(e, 'all')
}}>
关闭全部标签页
</div>
</div>
) // 太多tab自适应
const [rect, setRect] = useState(menuTabRef.current?.getBoundingClientRect())
console.log('rect', rect);
useEffect(() => {
setRect(menuTabRef.current?.getBoundingClientRect())
window.addEventListener('resize', () => {
setRect(menuTabRef.current?.getBoundingClientRect())
})
return () => {
window.removeEventListener('resize', () => {
setRect(menuTabRef.current?.getBoundingClientRect())
})
}
}, [])
const rectWidth = useMemo(() => {
return rect ? rect.width - 18 : '100%'
}, [rect])
const tabWidth = useMemo(() => {
if (typeof rectWidth === 'string') {
return '100%'
}
return rectWidth / menuTabList.length - 3
}, [rectWidth, menuTabList.length])
return isTabShow && menuTabList.length ? (
<div ref={menuTabRef} className={style.menuContainer}>
<div style={{ width: rectWidth }} className={isTabControlShow()}>
{menuTabList.map((e) => {
const { path, name } = e
return (
<div
key={path}
className={`list-tab ${isTabActive(path)}`}
style={{ width: tabWidth }}
onClick={() => {
toggleTab(path)
}}
>
<div key={e} className='ellipsis'>
{name}
</div>
<div className={style.closeTabIcon}>
<CloseOutlined
className={style.tabClose}
onClick={(item) => {
btnCloseTab(item, path)
}}
/>
</div>
</div>
)
})}
</div>
<Popover content={tabControlContent} placement='bottomRight'>
<div className={style.moreOut}>
<MoreOutlined
className={menuTabList && menuTabList.length > cTablimit ? style.showMore : style.hideMore}
/>
</div>
</Popover>
</div>
) : null
} export default connect(({ MenuTabStore }: any) => ({
MenuTabStore
}))(MenuTab)

github

react ts redux-saga | 谷歌Chrome浏览器风格的标签组件 | 中台的更多相关文章

  1. 使用谷歌chrome浏览器查看任何标签的固有属性

    查看任何标签的固有属性property: 使用谷歌浏览器:Ctrl+Shift+I 开发者工具----点击Elements----点击a标签----点击Properties属性及其属性值. 自定义属性 ...

  2. Clover 3 --- Windows Explorer 资源管理器的一个扩展,为其增加类似谷歌 Chrome 浏览器的多标签页功能。

    http://cn.ejie.me/ http://cn.ejie.me/uploads/setup_clover@3.4.6.exe  软件下载 默认图标实在比较难看,更换图标 更改图标---选择图 ...

  3. 谷歌Chrome浏览器开发者工具的基础功能

    上一篇我们学习了谷歌Chrome浏览器开发者工具的基础功能,下面介绍的是Chrome开发工具中最有用的面板Sources.Sources面板几乎是最常用到的Chrome功能面板,也是解决一般问题的主要 ...

  4. 谷歌chrome浏览器和火狐firefox浏览器自带http抓包工具和请求模拟插件

    谷歌chrome浏览器自带http抓包工具 chrome://net-internals/ 谷歌chrome浏览器http请求模拟插件:postman 火狐http请求模拟插件:httprequest ...

  5. 在 Ubuntu 16.04 中安装谷歌 Chrome 浏览器

    进入 Ubuntu 16.04 桌面,按下 Ctrl + Alt + t 键盘组合键,启动终端. 也可以按下 Win 键(或叫 Super 键),在 Dash 的搜索框中输入 terminal 或&q ...

  6. Ubuntu小技巧——怎样安装谷歌Chrome浏览器

    对于刚刚开始使用Ubuntu并想安装谷歌Chrome浏览器的新用户来说,本文所介绍的方法是最快捷的.在Ubuntu上安装谷歌Chrome的方法有很多.一些用户喜欢直接在谷歌Chrome下载页面获得 d ...

  7. 关于如何解决谷歌Chrome浏览器空白页的问题

    谷歌Chrome浏览器突然不打开任何网页,无论是任何站点(如http://www.baidu.com), 还是Chrome浏览器的设置页面(chrome://settings/), 扩展页面 ( ch ...

  8. 谷歌Chrome浏览器提示adobe flash player已过期完美解决办法

    最近使用谷歌Chrome浏览器提示adobe flash player已过期,浏览网页时一些flash元素的东西都无法正常显示,在网上尝试寻找很多方法,都不能解决,最后,经测试有效方法如下:一:下载最 ...

  9. Ubuntu 16下安装64位谷歌Chrome浏览器

    Ubuntu 16下安装64位谷歌Chrome浏览器 1.将下载源加入到系统的源列表 在终端中,输入以下命令: sudo wget https://repo.fdzh.org/chrome/googl ...

随机推荐

  1. 记一次实际开发过程中遇到事务报错问题 Transaction synchronization is not active

    一:问题场景 在一次http请求的后台接口中返回结果中出现了这个错误信息“Transaction synchronization is not active”,意思是“事务同步器没有激活”,但是被调用 ...

  2. 漫画:Integer 竟然有 6 种比较方式?

    代码测试 public class IntegerTest { public static void main(String[] args) { Integer i1 = 127; Integer i ...

  3. 成熟度模型:企业规模化推广敏捷和DevOps利器

    摘要: 本文介绍了成熟度模型在软件开发行业的应用,重点阐述了成熟度模型对于敏捷和DevOps在企业中进行规模化推广的价值,探讨了成熟度模型的设计原则,并对于如何明智使用成熟度模型给出了建议. 导言 在 ...

  4. mysql两种重要的引擎

    其中MyISAM:不支持事物,表锁 .frm : 表结构定义文件 .MYD: 表数据 .MYI:索引文件 InnoDB:支持事物,行锁 .frm : 表结构定义文件 .ibd:表空间(数据和索引)

  5. Producter and Consumer

    package pinx.thread; import java.util.LinkedList; import java.util.Queue; public class ProducerConsu ...

  6. ffmpeg常见用法总结

    1. 视频/音频剪切: ffmpeg -i input_file [-ss 00:00:10] [-t 00:00:20] output_file 去掉-ss指令表示从头开始 去掉-t指令表示剪切到结 ...

  7. CAS底层原理与ABA问题

    CAS定义 CAS(Compare And Swap)是一种无锁算法.CAS算法是乐观锁的一种实现.CAS有3个操作数,内存值V,旧的预期值A,要修改的新值B.当预期值A和内存值V相同时,将内存值V修 ...

  8. 【mysql】- 事务隔离级别和MVCC篇

    概念 术语 脏写( Dirty Write ): 如果一个事务修改了另一个未提交事务修改过的数据,那就意味着发了脏写 脏读( Dirty Read ) : 如果一个事务读到了另一个未提交事务修改过的数 ...

  9. PWN头秃之旅 - 4.Retrun-into-libc(攻防世界-level1)

    Retrun-into-libc,也写作Retrun2libc.libc是Linux下的ANSI C的函数库,包含了C语言最基本的库函数. Retrun2libc的前提是NX开启,但ASLR关闭,NX ...

  10. 面试题六十:n个骰子的点数

    把n个骰子扔在地上,求出现和为s的概率 可得n<=s<=6n 方法:定义6n-n+1长度的数组,然后对所有可能出现的组合进行计算,把结果进行计数存进数组:递归 方法二:动态规划,大问题小化 ...