• react实现提示消息容器,可以动态添加,删除内部子提示消息


    import React, { useState, useRef, useEffect } from 'react'
    import PropTypes from 'prop-types'
    import _ from 'lodash'
    import uuidv1 from 'uuid/v1'
    
    import './index.less'
    
    let currentTipComponents = []
    let setCurrentTipComponents
    
    function GlobalTipContainer(props) {
      const { style } = props
      const wrapperStyle = _.assign({}, style)
    
      const [tipComponents, setTipComponents] = useState([])
      currentTipComponents = tipComponents
      setCurrentTipComponents = setTipComponents
    
      const [displayedTipComponents, setDisplayedTipComponents] = useState([])
      const [mouseHover, setMouseHover] = useState(false)
    
      const wrapElemRef = useRef(null)
      useEffect(() => {
        if (mouseHover === false) {
          wrapElemRef.current.scrollTop = 10e10
        }
      })
    
      useEffect(() => {
        setDisplayedTipComponents(tipComponents.slice(0, 2))
      }, [tipComponents])
    
      return (
        <div
          className="global-tip-container-component-wrap"
          ref={wrapElemRef}
          onMouseEnter={() => {
            setMouseHover(true)
          }}
          onMouseLeave={() => {
            setMouseHover(false)
          }}
          style={wrapperStyle}
        >
          {displayedTipComponents}
        </div>
      )
    }
    
    GlobalTipContainer.addTip = (tipComponent) => {
      const key = uuidv1()
      currentTipComponents = currentTipComponents.slice(0)
      currentTipComponents.push(React.cloneElement(tipComponent, {
        key,
        onClose: _.flow(tipComponent.props.onClose || _.noop, () => { GlobalTipContainer.removeTip(key) }),
      }))
      setCurrentTipComponents(currentTipComponents)
      return key
    }
    
    GlobalTipContainer.removeTip = (key) => {
      const index = _.findIndex(currentTipComponents, ['key', key])
      if (index !== -1) {
        currentTipComponents.splice(index, 1)
        setCurrentTipComponents(currentTipComponents.slice(0))
      }
    }
    
    GlobalTipContainer.propTypes = {
      style: PropTypes.object,
    }
    
    GlobalTipContainer.defaultProps = {
      style: {},
    }
    
    export default GlobalTipContainer
  • 相关阅读:
    MySQL too many connections
    【MySQL】 清除等待连接
    wmic 获得系统硬件信息
    Linux 修改用户名
    初步了解虚拟化
    MySQL show 语句
    php去除bom
    jq闭包
    git
    地址收藏
  • 原文地址:https://www.cnblogs.com/chenbeibei520/p/11419423.html
Copyright © 2020-2023  润新知