【问题标题】:Tooltip positioning in table cell表格单元格中的工具提示定位
【发布时间】:2018-06-12 09:10:07
【问题描述】:

我正在使用 React,并且在单元格中有一个包含一些操作(删除、编辑等)的表格。而且我需要在每个操作上添加一个工具提示。我没有使用 jquery,也不打算使用,也没有标题道具(我需要将此工具提示升级到某些特定数据甚至其他组件)。

所以问题是我无法正确定位工具提示(例如在顶部或底部的中间)。我的组件应该接收 Witch 参数以及如何使用 css 接收?

const Tooltip = ({position = 'top', display, style, children}) => {
  let displayClass = display ? `fade ${position} in` : `tooltip-${position}`
  return (
    <div className={`tooltip ${displayClass} `} role='tooltip'>
      <div className='tooltip-arrow' />
      <div className='tooltip-inner'>
        {children}
      </div>
    </div>

  )
}

const ActionLinkItem = ({page, action, data, onMouseEnter, onMouseLeave, display, tooltipText, id}) => {
  const {buttonClass, icon} = actionsStyles[action]
  return (
    <Link to={`/${page}/${action.toLowerCase()}/${data.id}`}>
      <a
        className={`btn btn-xs ${buttonClass}`}
        id={id}
        onMouseEnter={onMouseEnter}
        onMouseLeave={onMouseLeave}
            ><i className={`fa fa-${icon}`} />
        <Tooltip display={display} action={action}>{tooltipText}</Tooltip>
      </a>
    </Link>
  )
}

export default class Actions extends Component {
  constructor (props) {
    super(props)
    this.state = {
      tooltipActive: ''
    }
  }

  handleHover (event) {
    this.setState({
      tooltipActive: event.target.id
    })
  }

  handleBlur (event) {
    this.setState({
      tooltipActive: ''
    })
  }

  getActionsTemplate () {
    const {actions, data, page} = this.props
    return actions.map(action => {
      let display = this.state.tooltipActive === `${action.action}-${data.id}`
      let id = `${action.action}-${data.id}`
      let tooltip = tooltipText[action.action].replace(/{type}/g, page).replace(/{item}/g, data.name
      return <ActionLinkItem
            key={`${data.id}-${action.action}`}
            page={page}
            action={action.action}
            data={data}
            id={id}
            tooltipText={tooltip}
            display={display}
            onMouseEnter={(e) => this.handleHover(e)}
            onMouseLeave={(e) => this.handleBlur(e)}
          />    
     })
   }

  render () {
    return (
      <div className='row'>
        {this.getActionsTemplate()}
      </div>
    )
  }
}

【问题讨论】:

    标签: javascript html css reactjs twitter-bootstrap-tooltip


    【解决方案1】:

    在最简单的情况下,工具提示应该绝对定位在定位元素内。

    所以,在 ActionLinkItem 中添加 position: relative 样式 并在 Tooltip 的外部添加 `position: absolute 的样式。

    为了增加功劳,您需要在工具提示上设置宽度,并使用 bottom: 100% 之类的样式将其定位或居中在 ActionLinkItem 中。

    如果包含分别位于右侧或左侧,您还可以通过左右移动工具提示来确保工具提示不会跑出页面。

    【讨论】:

    • 感谢您的回答。默认情况下,引导工具提示是绝对定位的,而我的 ActionLinkItem 是相对定位的。但如果工具提示有很多文本,它会扩展我的表格。我以为我需要设置相对较高的父级,但后来我不知道如何计算位置。
    • 我的 CSS 工具提示:.tooltip { display: none; position: absolute; z-index: 1070; } .tooltip.fade.in { display: flex; top: -40px; left: -60px; bottom: 100%; }
    猜你喜欢
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 2011-02-16
    • 2017-01-18
    • 2014-05-04
    • 1970-01-01
    相关资源
    最近更新 更多