import React, { useState, useEffect } from 'react'; import { Select, Empty } from '@sensoro/sensoro-design'; import { Spin } from 'antd'; import { LoadingOutlined } from '@ant-design/icons'; import VisibilitySensor from 'react-visibility-sensor'; import classNames from '@pansy/classnames'; import IconFont from '@sensoro/library/lib/components/icon-font'; import styles from './index.less'; const antIcon = ; enum State { NORMAL, LOADING, FAILED, FINISH, } export interface ListProps { className?: string; style?: React.CSSProperties; loading?: boolean; total?: number; onLoadMore?: () => Promise; empty?: React.ReactNode; } const List: React.FC = props => { const { className, children, total, onLoadMore, empty = ( } /> ), ...rest } = props; const count = React.Children.count(children); const [state, setState] = React.useState( count === total ? State.FINISH : State.NORMAL, ); function onChange(isVisible: boolean) { onEnd?.(); } const onEnd = async () => { if (state !== State.LOADING && typeof onLoadMore === 'function') { if (count === total) { setState(State.FINISH); } else { setState(State.LOADING); const success = await onLoadMore?.(); if (success) { if (count === total) { setState(State.FINISH); } else { setState(State.NORMAL); } } else { setState(State.FAILED); } } } }; return (
{children}
{total === 0 ? ( empty ) : (
{state === State.LOADING && (
{' '} 加载中...
)} {state === State.FAILED && (
加载失败, onEnd()}>重新加载
)} {state === State.FINISH &&
没有更多了
}
)}
); }; export default List;