/* @flow */ import withLanguage from 'Components/utils/withLanguage' import React, { Component, PureComponent } from 'react' import ReactCSSTransitionGroup from 'react-addons-css-transition-group' import './AnimatedTargetValue.css' type Props = { value: ?number, language: string } type State = { difference: number } export default withLanguage( class AnimatedTargetValue extends Component { previousValue: ?number = null timeoutId: ?TimeoutID = null state = { difference: 0 } componentDidUpdate(prevProps) { if (prevProps.value === this.props.value) { return } if (this.timeoutId) { clearTimeout(this.timeoutId) } this.previousValue = this.previousValue === null ? prevProps.value : this.previousValue this.timeoutId = setTimeout(() => { this.setState({ difference: (this.props.value || 0) - (this.previousValue || 0) }) this.previousValue = null this.timeoutId = null }, 200) } format = value => { return value == null ? '' : Intl.NumberFormat(this.props.language, { style: 'currency', currency: 'EUR', maximumFractionDigits: 0, minimumFractionDigits: 0 }).format(value) } render() { const formattedValue = this.format(this.props.value) const formattedDifference = this.format(this.state.difference) return ( <> {Math.abs(this.state.difference) > 1 && formattedDifference !== formattedValue && ( 0 ? 'chartreuse' : 'red' }}> {(this.state.difference > 0 ? '+' : '') + formattedDifference} )}{' '} {this.format(this.props.value)} ) } } ) class Evaporate extends PureComponent<{ children: string, style: Object }> { render() { return ( {this.props.children} ) } }