From 1b0d50525d709413517a2575cb76e9956368abb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Benitte?= Date: Sat, 12 Aug 2017 14:04:52 +0900 Subject: [PATCH] feat(line): avoid re-rendering tooltip on mouse move for line chart --- src/components/charts/line/LineSlicesItem.js | 107 ++++++++++--------- 1 file changed, 57 insertions(+), 50 deletions(-) diff --git a/src/components/charts/line/LineSlicesItem.js b/src/components/charts/line/LineSlicesItem.js index 180712cab..7b7793990 100644 --- a/src/components/charts/line/LineSlicesItem.js +++ b/src/components/charts/line/LineSlicesItem.js @@ -6,61 +6,68 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ -import React, { PureComponent } from 'react' +import React from 'react' +import PropTypes from 'prop-types' +import compose from 'recompose/compose' +import pure from 'recompose/pure' +import withState from 'recompose/withState' +import withHandlers from 'recompose/withHandlers' +import withPropsOnChange from 'recompose/withPropsOnChange' import TableTooltip from '../../tooltip/TableTooltip' const Chip = ({ color }) => -export default class LineSlicesItem extends PureComponent { - state = { - isHover: false, - } +const LineSlicesItem = ({ slice, height, showTooltip, hideTooltip, isHover }) => + + {isHover && + } + + - handleMouseEnter = e => { - this.setState({ isHover: true }) - - const { slice, showTooltip } = this.props - showTooltip( - [, p.id, p.value])} - />, - e - ) - } - - handleMouseLeave = () => { - this.setState({ isHover: false }) - this.props.hideTooltip() - } +LineSlicesItem.propTypes = { + slice: PropTypes.object.isRequired, + height: PropTypes.number.isRequired, + showTooltip: PropTypes.func.isRequired, + hideTooltip: PropTypes.func.isRequired, + isHover: PropTypes.bool.isRequired, +} - render() { - const { slice, height } = this.props - const { isHover } = this.state +const enhance = compose( + withState('isHover', 'setIsHover', false), + withPropsOnChange(['slice'], ({ slice }) => ({ + tooltip: ( + [, p.id, p.value])} /> + ), + })), + withHandlers({ + showTooltip: ({ showTooltip, setIsHover, tooltip }) => e => { + setIsHover(true) + showTooltip(tooltip, e) + }, + hideTooltip: ({ hideTooltip, setIsHover }) => () => { + setIsHover(false) + hideTooltip() + }, + }), + pure +) - return ( - - {isHover && - } - - - ) - } -} +export default enhance(LineSlicesItem)