import QueueAnim from 'rc-queue-anim'; import React, { PureComponent } from 'react'; import SBarCrosswiseList from './styled'; interface BarCrosswiseListProps { size: Record; [key: string]: any; } interface BarCrosswiseListState { [key: string]: any; } export default class BarCrosswiseListComponent extends PureComponent< BarCrosswiseListProps, BarCrosswiseListState > { state = { currentIndex: 1, }; timer: number | null = null; componentDidMount() { this.setTimer(this.props.data, this.props.count, this.props.animate); } UNSAFE_componentWillReceiveProps(nextProps: Record) { const { data, count, animate } = nextProps; if ( data !== this.props.data || count !== this.props.count || animate !== this.props.animate ) { this.setTimer(data, count, animate); } } componentWillUnmount() { if (this.timer) { clearInterval(this.timer); } } setTimer = (data: Record[], count: number, animate: boolean) => { if (this.timer) { clearInterval(this.timer); } if (data && data.length > count && animate) { this.timer = setInterval(() => { this.setState(state => ({ currentIndex: state.currentIndex >= Math.ceil(data.length / count) ? 1 : state.currentIndex + 1, })); }, 3000); } else { this.setState(state => ({ currentIndex: 1, })); } }; render() { const { data = [], // total, // 总数 count = 3, // 默认显示3个 divideHeight = '33%', // 默认高度3等分 size: { height, width }, unit, size, textStyle, color1 = '#806aff', color2 = '#2cb4f9', color = 'rgba(74, 74, 74, 0.45)', bgLineHeight = 5, lineHeight = 5, showLeft = false, animate = false, unitStyle, valueStyle, littleTitle, littleTitleStyle, rightMargin, lineType, rightWidth, lineTotal, } = this.props; const { currentIndex } = this.state; let total = 0; const dataRender: Record[] = []; data && data.length > 0 && data.forEach((item: Record, index: number) => { total += item.value; dataRender.push({ ...item, index: index + 1 }); }); return ( {dataRender .slice((currentIndex - 1) * count, currentIndex * count) .map((item: any, index: number) => (
{showLeft ? ( <>
NO. {item.index}
) : null}
{item.label}
{littleTitle} {item.value} {unit}
))} ); } } BarCrosswiseListComponent.themeUpdate = function(params: any) { return { color1: params.primaryColor, color2: params.secondColor, }; };