react对antd的Tooltip文字提示进行二次封装实现全局调用,想在哪调用就在哪调用,

Tooltip文字提示的二次封装

import React from 'react'

import { Tooltip } from 'antd';

import './index.less';`


 
`class Tooltip extends React.Component {

   constructor(props){

       super(props);

       this.state={

           visible: false,

           DtStyle:{},

           DtClass:'',

       }

    }

   componentDidMount(){ this.getItemWidth() }

   getItemWidth=()=>{

       if(this.container){//当这个节点存在的时候

           this.setState({

                DtStyle:{width:`${this.container.clientWidth}px`},//宽等于该节点的宽

                DtClass:'NowrapAndTitle'

           })

       }

    }

 render () {

   const {DtStyle,DtClass}=this.state;

   const {center,title,children}=this.props;

   return (

       <div className="PublicTableTooltip" style={{textAlign:center?'center':'left'}}>//设置是居中还是左对齐

           <Tooltip placement="topLeft" title={title}overlayClassName="ItemPublicTableTooltip">

                  //把这个节点赋给这个组件的this.constiner属性

                <div ref={node =>this.container = node} style={DtStyle}className={DtClass}>{children}</div>

           </Tooltip>

       </div>

    )

  }

}

export default Tooltip

CSS样式

`.PublicTableTooltip{

   .NowrapAndTitle{

   // 不换行显示。。。

   overflow: hidden;

   -o-text-overflow: ellipsis;

   text-overflow: ellipsis;

   white-space: nowrap;
  }
}
.ItemPublicTableTooltip{

 .ant-tooltip-inner{

   background-color: rgba();

  }

 .ant-tooltip-arrow{

   border-top-color: rgba();

  }

}

调用的时候

import Tooltip from './index';

<Tooltip title={text} center>

       {text}//直接包裹要提示的内容即可

</Tooltip>

效果图:
在这里插入图片描述
请关注我的公众号:前端工程师从0到1(微信搜索)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值