image.png

    1. import React from 'react';
    2. import PropTypes from 'prop-types';
    3. import { List, Badge, Typography, Tooltip } from 'antd'
    4. const { Item } = List;
    5. const { Text } = Typography
    6. const colorStyle = { marginRight: 8, backgroundColor: '#314759'}
    7. const whiteStyle = {
    8. ...colorStyle,
    9. backgroundColor: '#fff',
    10. color: '#999',
    11. boxShadow: '0 0 0 1px #d8d8d8 inset',
    12. }
    13. function SwitchText({value}) {
    14. if(value.length > 8) {
    15. return (
    16. <Tooltip title={value}>
    17. <Text ellipsis>{value}</Text>
    18. </Tooltip>
    19. )
    20. }
    21. return <Text ellipsis>{value}</Text>
    22. }
    23. function ListItem({label, value, index}) {
    24. const style = (index < 3) ? colorStyle : whiteStyle
    25. return (
    26. <Item>
    27. <Badge count={index+1} style={style}/>
    28. <SwitchText value={label}/>
    29. <span className='number'>{value}</span>
    30. </Item>
    31. )
    32. }
    33. TopList.propTypes = {
    34. dataSource: PropTypes.array.isRequired,
    35. title: PropTypes.string,
    36. loading: PropTypes.bool,
    37. };
    38. TopList.defaultProps = {
    39. title: '',
    40. loading: false,
    41. }
    42. function TopList({title, dataSource, loading}) {
    43. const header = title && <header><b>{title}</b></header>
    44. const attrs = {
    45. className: 'top-list',
    46. size: 'small',
    47. split: false,
    48. header,
    49. loading,
    50. dataSource,
    51. renderItem: (item, i) => <ListItem {...item} index={i} key={i}/>,
    52. }
    53. return (
    54. <List {...attrs}/>
    55. );
    56. }
    57. export default TopList;

    .top-list 样式

    1. .top-list {
    2. &.ant-list-sm .ant-list-item {
    3. justify-content: flex-start;
    4. padding: 8px 32px 8px 8px;
    5. }
    6. .number {
    7. position: absolute;
    8. right: 0;
    9. }
    10. }