import QueueAnim from 'rc-queue-anim'; import React, { PureComponent } from 'react'; import SRRankList from './styled'; interface RRankListProps { size: Record; [key: string]: any; } interface RRankListState { [key: string]: any; } export default class RRankListComponent extends PureComponent< RRankListProps, RRankListState > { state = { currentIndex: 1, }; timer: number | null = null; componentDidMount() { this.setTimer(this.props.data, this.props.count); } UNSAFE_componentWillReceiveProps(nextProps: Record) { const { data, count } = nextProps; if (data !== this.props.data || count !== this.props.count) { this.setTimer(data, count); } } componentWillUnmount() { if (this.timer) { clearInterval(this.timer); } } setTimer = (data: Record[], count: number) => { if (this.timer) { clearInterval(this.timer); } if (data && data.length > count) { 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', } = this.props; const { currentIndex } = this.state; let total = 0; const dataRender: Record[] = []; 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 => (
NO. {item.index}     {item.label}
{item.value} {unit}
))} ); } } RRankListComponent.themeUpdate = function(params: any) { return { color1: params.primaryColor, color2: params.secondColor, }; };