带有Redux的ReactJS在SVG组件上显示引导工具提示

时间:2019-02-11 04:52:13

标签: javascript reactjs redux react-redux reactstrap

我正在尝试显示有关SVG组件的工具提示。对于工具提示,我尝试使用reactstap组件。

如果我在SVG内使用普通组件,则不会渲染SVG组件。

getServiceStatus = (serviceStatus, serviceName) => {
        return Object.keys(serviceStatus).map((date, index) => {
            const downTime = serviceStatus[date].filter(value => { return value === 0 }).length
            const color = downTime ? "#bcbe2a" : "#36b37e"
            const toolTipText = downTime ? date + "No DownTime report for this date" : date + " service was down for " + downTime

            return (
                <span key={index}>
                <rect key={date} id={serviceName+'-'+ index}  height="34" width="3" x={(index + 1) * 5} y="0" fill={color} />
                    <Tooltip placement="top" isOpen={this.props.toolTipOpen} target={serviceName+'-'+ index} toggle={this.toggleToolTip}>
                        {toolTipText}
                    </Tooltip>
                </span>
            )
        })
    }

如果我为SVGTooltip创建一个单独的组件,那么问题就出在工具提示中,显示了两个不同的组件。

getServiceStatus = (serviceStatus, serviceName) => {
    return Object.keys(serviceStatus).map((date, index) => {
        const downTime = serviceStatus[date].filter(value => { return value === 0 }).length
        const color = downTime ? "#bcbe2a" : "#36b37e"
        const toolTipText = downTime ? date + "No DownTime report for this date" : date + " service was down for " + downTime

        return (
            <rect key={date} id={serviceName+'-'+ index}  height="34" width="3" x={(index + 1) * 5} y="0" fill={color} />
       )

    })
}

getToolTip = (serviceStatus, serviceName) => {
    console.log(serviceStatus.toString())
    return Object.keys(serviceStatus).map((date, index) => {
        const downTime = serviceStatus[date].filter(value => { return value === 0 }).length
        const toolTipText = downTime ? date + "No DownTime report for this date" : date + " service was down for " + downTime

        return (
            <span key={index}>
                <Tooltip placement="top" isOpen={this.props.toolTipOpen} target={serviceName+'-'+ index} toggle={this.toggleToolTip}>
                    {toolTipText}
                </Tooltip>
            </span>
        )
    })
}

https://codesandbox.io/s/8zzk6qq662

1 个答案:

答案 0 :(得分:0)

主要问题是您使用相同的状态打开所有工具提示。解决此问题的一种方法是使用工具提示ID。

完整代码:https://codesandbox.io/s/2vv782j6z0

动作:

export function toogleToolTipAction(id) {
  console.log("toogleToolTipAction", id);
  return {
    type: "TOOGLE_TOOLTIP",
    id // here i'm using the tooltip id
  };
}

减速器:

case "TOOGLE_TOOLTIP":
  return {
    ...state,
    toolTipOpen: action.id // here i'm using the tooltip id
  };

容器:

const mapDispatchToProps = dispatch => {
  return {
    fetchServiceStatus: () => dispatch(getServiceStatusAction()),
    toogleToolTip: id => dispatch(toogleToolTipAction(id)), // here i'm using the tooltip id
    dummy: () => dispatch(toogleToolTipAction())
  };
};

组件:

  toggleToolTip = (id, e) => {
    if (e.type === "mouseout") {
      this.props.toogleTooltip(null);
    } else {
      this.props.toogleTooltip(id);
    }
  };

  return (
    <span key={index}>
      <Tooltip
        placement="top"
        isOpen={this.props.toolTipOpen === serviceName + "-" + index}
        target={serviceName + "-" + index}
        toggle={this.toggleToolTip.bind(this, serviceName + "-" + index)}
      >
        {toolTipText}
      </Tooltip>
    </span>
  );