import React, { Component } from 'react'
import PropTypes from 'prop-types'
import _ from 'lodash'
import classNames from 'classnames' import {
CloseButton,
} from '@dby-h5-clients/pc-1vn-components' import './index.less' class NormalTab extends Component {
static propTypes = {
style: PropTypes.object,
tagIcon: PropTypes.element,
name: PropTypes.string,
onClick: PropTypes.func,
closable: PropTypes.bool,
onClose: PropTypes.func,
isActive: PropTypes.bool,
} static defaultProps = {
style: {},
tagIcon: null,
name: 'normal-tab',
onClick: _.noop,
closable: true,
onClose: _.noop,
isActive: false,
} state = {
tabInfoWrapWidth: 100,
} componentDidMount() {
const tabInfoWrapElem = this.normalTabWrapElemRef.current
this.tabInfoWrapElem = tabInfoWrapElem this.resizeObserver = new ResizeObserver(_.throttle(() => {
if (tabInfoWrapElem !== undefined) {
this.setState({
tabInfoWrapWidth: tabInfoWrapElem.clientWidth,
})
}
}, 200)) this.resizeObserver.observe(tabInfoWrapElem)
} componentWillUnmount() {
this.resizeObserver.unobserve(this.tabInfoWrapElem)
} resizeObserver normalTabWrapElemRef = React.createRef() tabInfoWrapElem render() {
const closeBtnStyle = {
height: '20px',
width: '20px',
flex: 'none',
margin: 8,
} const tabInfoWrapStyle = {
// 当显示区域足够大或当前标签被选中的时候显示完整关闭按钮
flex: (this.state.tabInfoWrapWidth > 84 || this.props.isActive === true) ? 1 : 'none',
} return (
<span
className={classNames({
'normal-tab-component-wrap': true,
selected: this.props.isActive === true,
})}
ref={this.normalTabWrapElemRef}
style={this.props.style}
title={this.props.name}
>
<span
className="normal-tab-info-wrap"
tabIndex={0}
role="button"
style={tabInfoWrapStyle}
onClick={this.props.onClick}
>
{this.props.tagIcon}
{this.props.name}
</span>
{
this.props.closable === true && (
<CloseButton
style={closeBtnStyle}
onClick={this.props.onClose}
/>
)
}
</span>
)
}
} export default NormalTab
.normal-tab-component-wrap {
min-width: 36px;
height: 100%;
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
border-radius: 4px;
background-color: rgba(54, 65, 82, 0.1);;
margin-right: 1px;
overflow: hidden; &:last-child {
margin-right: 0px;
} &:hover {
background-color: #fff;
} &.selected {
background-color: #fff;
min-width: 64px; .normal-tab-info-wrap {
flex:;
}
} .normal-tab-info-wrap {
height: 100%;
max-width: 180px;
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
flex: none;
font-size: 12px;
color: #616e80;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
cursor: pointer;
outline: none;
}
}

react封装通用tab组件的更多相关文章

  1. ReactJS实现的通用分页组件

    大家多少都自己写过各种版本的分页工具条吧,像纯服务版的,纯jsWeb板的,Angular版的,因为这个基础得不能再基础的功能太多地方都会用到,下面我给出以个用ReactJS实现的版本,首先上图看下效果 ...

  2. 使用react context实现一个支持组件组合和嵌套的React Tab组件

    纵观react的tab组件中,即使是github上star数多的tab组件,实现原理都非常冗余. 例如Github上star数超四百星的react-tab,其在render的时候都会动态计算哪个tab ...

  3. 基于 React 封装的高德地图组件,帮助你轻松的接入地图到 React 项目中。

    react-amap 这是一个基于 React 封装的高德地图组件,帮助你轻松的接入地图到 React 项目中. 文档实例预览: Github Web | Gitee Web 特性 ️ 自动加载高德地 ...

  4. 如何基于 React 封装一个组件

    如何基于 React 封装一个组件 前言 很多小伙伴在第一次尝试封装组件时会和我一样碰到许多问题,比如人家的组件会有 color 属性,我们在使用组件时传入组件文档中说明的属性值如 primary , ...

  5. react hooks 如何自定义组件(react函数组件的封装)

    前言 这里写一下如何封装可复用组件.首先技术栈 react hooks + props-type + jsx封装纯函数组件.类组件和typeScript在这不做讨论,大家别白跑一趟. 接下来会说一下封 ...

  6. react实战系列 —— react 的第一个组件

    react 的第一个组件 写了 react 有一个半月,现在又有半个月没写了,感觉对其仍旧比较陌生. 本文分两部分,首先聊一下 react 的相关概念,然后不使用任何语法糖(包括 jsx)或可能隐藏底 ...

  7. JGUI源码:Tab组件实现(9)

    程序界面效果如下 Tab组件由多个TabItem组成,超出部分隐藏,可以通过左右按钮滑动显示出来 1.封装 // 初始化内容 $(function () { J.JTab($(".jgui- ...

  8. 继续封装个 Volley 组件

    本篇文章已授权微信公众号 dasu_Android(大苏)独家发布 前面已经封装了很多常用.基础的组件了:base-module, 包括了: crash 处理 常用工具类 apk 升级处理 log 组 ...

  9. [转]angular2封装material2对话框组件

    本文转自:https://www.jianshu.com/p/da9978e25566 1. 说明 angular-material2自身文档不详,控件不齐,使用上造成了很大的障碍.这里提供一个方案用 ...

随机推荐

  1. 教你阅读 Cpython 的源码(二)

    第二部分:Python解释器进程 在上节教你阅读 Cpython 的源码(一)中,我们从编写Python到执行代码的过程中看到Python语法和其内存管理机制. 在本节,我们将从代码层面去讨论 ,Py ...

  2. JavaScript数字精度丢失的一些问题

    本文分为三个部分 JS 数字精度丢失的一些典型问题 JS 数字精度丢失的原因 解决方案(一个对象+一个函数) 一.JS数字精度丢失的一些典型问题 1. 两个简单的浮点数相加 1 0.1 + 0.2 ! ...

  3. Tomcat中配置URIEncoding="UTF-8"来处理中文的处理

    Tomcat中配置URIEncoding="UTF-8"来处理中文的处理 打开 server.xml 文件,更改两个地方. 配置一:添加 URIEncoding="UTF ...

  4. 连接池大小调优 原创: ImportNew ImportNew 2017-06-07

    连接池大小调优 原创: ImportNew ImportNew 2017-06-07

  5. web框架性能点

    awesome-go-web-frameworks/README.md at master · speedwheel/awesome-go-web-frameworkshttps://github.c ...

  6. SVG-变换

    transform变换 translate平移 <svg width="200" height="50"> <rect x="0&q ...

  7. 总结解决 Android-Studio 编译耗时(好久、太长)问题

    首先通过搜索有关Android-Studio 编译耗时(好久.太长)问题的博客,速度确实有所改善. 一.暂时解决 Android-Studio 编译耗时(好久.太长)问题 本文链接:https://b ...

  8. JAVA关于静态static的面试题

    JAVA关于静态static的面试题     版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net ...

  9. Row_Number() and Rank() in SQL

    1. 数据表实例数据 2. 使用Row_Number()方法给每一行数据添加一个唯一编号, 可以按照某一列进行排序. 3. 使用Partition by Column在一个Partition内进行编号 ...

  10. LeetCode_21. Merge Two Sorted Lists

    21. Merge Two Sorted Lists Easy Merge two sorted linked lists and return it as a new list. The new l ...