ScrollableList: add placeholder footer, fix "empty" state
This commit is contained in:
parent
0d463bbbd1
commit
a8c306e62b
|
@ -6,6 +6,7 @@ import PullToRefresh from 'soapbox/components/pull-to-refresh';
|
||||||
|
|
||||||
import { Spinner, Text } from './ui';
|
import { Spinner, Text } from './ui';
|
||||||
|
|
||||||
|
// Ensure the className winds up here
|
||||||
const List = React.forwardRef((props: any, ref: React.ForwardedRef<HTMLDivElement>) => {
|
const List = React.forwardRef((props: any, ref: React.ForwardedRef<HTMLDivElement>) => {
|
||||||
const { context, ...rest } = props;
|
const { context, ...rest } = props;
|
||||||
return <div ref={ref} className={context.listClassName} {...rest} />;
|
return <div ref={ref} className={context.listClassName} {...rest} />;
|
||||||
|
@ -29,8 +30,10 @@ interface IScrollableList {
|
||||||
className?: string,
|
className?: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Legacy ScrollableList with Virtuoso for backwards-compatibility */
|
||||||
const ScrollableList: React.FC<IScrollableList> = ({
|
const ScrollableList: React.FC<IScrollableList> = ({
|
||||||
prepend = null,
|
prepend = null,
|
||||||
|
alwaysPrepend,
|
||||||
children,
|
children,
|
||||||
isLoading,
|
isLoading,
|
||||||
emptyMessage,
|
emptyMessage,
|
||||||
|
@ -40,6 +43,7 @@ const ScrollableList: React.FC<IScrollableList> = ({
|
||||||
onScrollToTop,
|
onScrollToTop,
|
||||||
onLoadMore,
|
onLoadMore,
|
||||||
className,
|
className,
|
||||||
|
hasMore,
|
||||||
placeholderComponent: Placeholder,
|
placeholderComponent: Placeholder,
|
||||||
placeholderCount = 0,
|
placeholderCount = 0,
|
||||||
}) => {
|
}) => {
|
||||||
|
@ -47,7 +51,26 @@ const ScrollableList: React.FC<IScrollableList> = ({
|
||||||
// const autoload = settings.get('autoloadMore');
|
// const autoload = settings.get('autoloadMore');
|
||||||
|
|
||||||
const showPlaceholder = showLoading && Placeholder && placeholderCount > 0;
|
const showPlaceholder = showLoading && Placeholder && placeholderCount > 0;
|
||||||
|
const data = showPlaceholder ? Array(placeholderCount).fill('') : Array.from(children || []);
|
||||||
|
|
||||||
|
/* Render an empty state instead of the scrollable list */
|
||||||
|
const renderEmpty = () => {
|
||||||
|
return (
|
||||||
|
<div className='mt-2'>
|
||||||
|
{alwaysPrepend && prepend}
|
||||||
|
|
||||||
|
<div className='bg-primary-50 dark:bg-slate-700 mt-2 rounded-lg text-center p-8'>
|
||||||
|
{isLoading ? (
|
||||||
|
<Spinner />
|
||||||
|
) : (
|
||||||
|
<Text>{emptyMessage}</Text>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Render the actual item */
|
||||||
const renderItem = (_i: number, element: JSX.Element) => {
|
const renderItem = (_i: number, element: JSX.Element) => {
|
||||||
if (showPlaceholder) {
|
if (showPlaceholder) {
|
||||||
return <Placeholder />;
|
return <Placeholder />;
|
||||||
|
@ -56,12 +79,22 @@ const ScrollableList: React.FC<IScrollableList> = ({
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Don't use Virtuoso's EmptyPlaceholder component so it preserves height
|
||||||
|
if (data.length === 0) {
|
||||||
|
return renderEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't use Virtuoso's Footer component so it preserves spacing
|
||||||
|
if (hasMore && Placeholder) {
|
||||||
|
data.push(<Placeholder />);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PullToRefresh onRefresh={onRefresh}>
|
<PullToRefresh onRefresh={onRefresh}>
|
||||||
<Virtuoso
|
<Virtuoso
|
||||||
useWindowScroll
|
useWindowScroll
|
||||||
className={className}
|
className={className}
|
||||||
data={showPlaceholder ? Array(placeholderCount).fill('') : Array.from(children || [])}
|
data={data}
|
||||||
startReached={onScrollToTop}
|
startReached={onScrollToTop}
|
||||||
endReached={onLoadMore}
|
endReached={onLoadMore}
|
||||||
isScrolling={isScrolling => isScrolling && onScroll && onScroll()}
|
isScrolling={isScrolling => isScrolling && onScroll && onScroll()}
|
||||||
|
@ -72,11 +105,7 @@ const ScrollableList: React.FC<IScrollableList> = ({
|
||||||
components={{
|
components={{
|
||||||
Header: () => prepend,
|
Header: () => prepend,
|
||||||
ScrollSeekPlaceholder: Placeholder as any,
|
ScrollSeekPlaceholder: Placeholder as any,
|
||||||
EmptyPlaceholder: () => isLoading ? (
|
EmptyPlaceholder: () => renderEmpty(),
|
||||||
<Spinner />
|
|
||||||
) : (
|
|
||||||
<Text>{emptyMessage}</Text>
|
|
||||||
),
|
|
||||||
List,
|
List,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
Loading…
Reference in New Issue