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. Go By Example-值类型

    Go By Example-值类型 Go语言的数据类型可以分为值类型和引用类型,这里先说值类型. 值类型 值类型:在Go语言中int.float.bool和string这些类型都属于值类型,使用这些类 ...

  2. redis5种数据结构讲解及使用场景

    string  list  hash set zset  探究 Redis 4 的 stream 类型 redis提供了5中数据结构,理解每种数据结构的特点对于redis开发运维非常重要.  一.字符 ...

  3. php如何生成 uuid(总结)

    php如何生成 uuid(总结) 一.总结 一句话总结: UUID的全拼为“Universally Unique Identifier”,可以译为“通用唯一识别码”. UUID是指在一台机器上生成的数 ...

  4. qt 设置程序居中

    widget *s =new widget; , ); s->show();

  5. IIS URL Rewriting and ASP.NET Routing

    IIS URL Rewriting and ASP.NET Routing With the release of the URL Rewrite Module for IIS and the inc ...

  6. 转:android DownloadManager: java.lang.SecurityException: Invalid value for visibility: 2

    1.问题描述 今天使用Android系统的DownloadManager进行下载操作时,爆了如下所示的错误: java.lang.RuntimeException: Unable to start s ...

  7. matlab学习——02整数规划(蒙特卡洛法,指派问题,混合整数规划)

    02整数规划 蒙特卡洛法(随机取样法) 编写文件mengte.m,目标函数f和约束向量g function[f,g]=mengte(x); f=x(1)^2+x(2)^2+3*x(3)^2+4*x(4 ...

  8. mysql导入、导出 ( 带视图)

    1创建账号授权 grant all privileges on jenkinsddbes.* to 'jenkinsddbes'@'%' identified by '1iN@Da12tA&* ...

  9. (1) Java实现JDBC连接及事务的方式

    许多数据库的auto-commit默认是ON的,比如MySQL,PostgresSQL等.当然也有默认是OFF的,比如Oracle(Oracle里面执行DML语句是需要手动commit的). 这里我们 ...

  10. 在Eclipse Android中设置模拟器屏幕大小

    在Eclipse Android中设置模拟器屏幕大小是本文要介绍的内容,主要是来了解并学习Eclipse Android中模拟器的设置,具体关于Eclipse Android内容的详解来看本文. 方法 ...