2023-07-15 12:57:03 +08:00
|
|
|
'use client';
|
|
|
|
|
2017-08-09 15:45:08 +08:00
|
|
|
import classNames from 'classnames';
|
2022-03-25 14:26:09 +08:00
|
|
|
// eslint-disable-next-line import/no-named-as-default
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-06-05 09:56:19 +08:00
|
|
|
import extendsObject from '../_util/extendsObject';
|
|
|
|
import type { Breakpoint } from '../_util/responsiveObserver';
|
|
|
|
import { responsiveArray } from '../_util/responsiveObserver';
|
2022-05-07 14:31:54 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2023-01-09 10:04:35 +08:00
|
|
|
import DefaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { Row } from '../grid';
|
|
|
|
import useBreakpoint from '../grid/hooks/useBreakpoint';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { PaginationConfig } from '../pagination';
|
|
|
|
import Pagination from '../pagination';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { SpinProps } from '../spin';
|
|
|
|
import Spin from '../spin';
|
2017-08-09 15:45:08 +08:00
|
|
|
import Item from './Item';
|
|
|
|
|
2022-04-01 10:23:34 +08:00
|
|
|
// CSSINJS
|
2023-06-05 09:56:19 +08:00
|
|
|
import { ListContext } from './context';
|
2022-04-01 10:23:34 +08:00
|
|
|
import useStyle from './style';
|
2023-08-17 17:55:46 +08:00
|
|
|
import useSize from '../config-provider/hooks/useSize';
|
2017-08-09 15:45:08 +08:00
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
export type { ListItemMetaProps, ListItemProps } from './Item';
|
2023-06-05 09:56:19 +08:00
|
|
|
export type { ListConsumerProps } from './context';
|
2017-09-25 22:14:49 +08:00
|
|
|
|
2020-04-27 16:59:33 +08:00
|
|
|
export type ColumnCount = number;
|
2017-11-21 19:02:04 +08:00
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
export type ColumnType = 'gutter' | 'column' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl';
|
2017-10-10 10:04:03 +08:00
|
|
|
|
2017-08-14 22:25:17 +08:00
|
|
|
export interface ListGridType {
|
2017-08-14 11:08:19 +08:00
|
|
|
gutter?: number;
|
2017-11-21 19:02:04 +08:00
|
|
|
column?: ColumnCount;
|
|
|
|
xs?: ColumnCount;
|
|
|
|
sm?: ColumnCount;
|
|
|
|
md?: ColumnCount;
|
|
|
|
lg?: ColumnCount;
|
|
|
|
xl?: ColumnCount;
|
2018-01-21 01:03:00 +08:00
|
|
|
xxl?: ColumnCount;
|
2017-08-14 11:08:19 +08:00
|
|
|
}
|
|
|
|
|
2017-09-26 16:11:35 +08:00
|
|
|
export type ListSize = 'small' | 'default' | 'large';
|
|
|
|
|
2019-03-17 14:25:34 +08:00
|
|
|
export type ListItemLayout = 'horizontal' | 'vertical';
|
|
|
|
|
2019-03-28 18:55:52 +08:00
|
|
|
export interface ListProps<T> {
|
2017-08-09 15:45:08 +08:00
|
|
|
bordered?: boolean;
|
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2019-05-08 22:54:17 +08:00
|
|
|
style?: React.CSSProperties;
|
2017-08-09 15:45:08 +08:00
|
|
|
children?: React.ReactNode;
|
2019-05-15 11:12:55 +08:00
|
|
|
dataSource?: T[];
|
2017-08-09 15:45:08 +08:00
|
|
|
extra?: React.ReactNode;
|
2017-09-26 16:11:35 +08:00
|
|
|
grid?: ListGridType;
|
2017-08-09 15:45:08 +08:00
|
|
|
id?: string;
|
2019-03-17 14:25:34 +08:00
|
|
|
itemLayout?: ListItemLayout;
|
2017-12-29 10:09:18 +08:00
|
|
|
loading?: boolean | SpinProps;
|
2017-09-25 15:24:16 +08:00
|
|
|
loadMore?: React.ReactNode;
|
2018-12-22 21:21:42 +08:00
|
|
|
pagination?: PaginationConfig | false;
|
2017-08-09 15:45:08 +08:00
|
|
|
prefixCls?: string;
|
2021-09-07 12:05:59 +08:00
|
|
|
rowKey?: ((item: T) => React.Key) | keyof T;
|
2019-05-15 11:12:55 +08:00
|
|
|
renderItem?: (item: T, index: number) => React.ReactNode;
|
2017-09-26 16:11:35 +08:00
|
|
|
size?: ListSize;
|
|
|
|
split?: boolean;
|
|
|
|
header?: React.ReactNode;
|
|
|
|
footer?: React.ReactNode;
|
2018-12-26 16:01:00 +08:00
|
|
|
locale?: ListLocale;
|
2017-08-09 15:45:08 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 19:02:04 +08:00
|
|
|
export interface ListLocale {
|
2022-04-08 22:55:42 +08:00
|
|
|
emptyText: React.ReactNode;
|
2017-11-21 19:02:04 +08:00
|
|
|
}
|
|
|
|
|
2020-05-19 14:55:46 +08:00
|
|
|
function List<T>({
|
2023-07-31 15:09:21 +08:00
|
|
|
pagination = false as ListProps<T>['pagination'],
|
2020-05-19 14:55:46 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
bordered = false,
|
|
|
|
split = true,
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2023-07-06 09:43:03 +08:00
|
|
|
style,
|
2020-05-19 14:55:46 +08:00
|
|
|
children,
|
|
|
|
itemLayout,
|
|
|
|
loadMore,
|
|
|
|
grid,
|
|
|
|
dataSource = [],
|
2023-08-17 17:55:46 +08:00
|
|
|
size: customizeSize,
|
2020-05-19 14:55:46 +08:00
|
|
|
header,
|
|
|
|
footer,
|
|
|
|
loading = false,
|
|
|
|
rowKey,
|
|
|
|
renderItem,
|
|
|
|
locale,
|
|
|
|
...rest
|
|
|
|
}: ListProps<T>) {
|
2020-04-26 21:23:25 +08:00
|
|
|
const paginationObj = pagination && typeof pagination === 'object' ? pagination : {};
|
|
|
|
|
|
|
|
const [paginationCurrent, setPaginationCurrent] = React.useState(
|
|
|
|
paginationObj.defaultCurrent || 1,
|
|
|
|
);
|
|
|
|
const [paginationSize, setPaginationSize] = React.useState(paginationObj.defaultPageSize || 10);
|
2017-11-17 11:25:29 +08:00
|
|
|
|
2023-07-06 09:43:03 +08:00
|
|
|
const { getPrefixCls, renderEmpty, direction, list } = React.useContext(ConfigContext);
|
2020-04-26 21:23:25 +08:00
|
|
|
|
|
|
|
const defaultPaginationProps = {
|
2018-04-20 15:41:40 +08:00
|
|
|
current: 1,
|
|
|
|
total: 0,
|
|
|
|
};
|
|
|
|
|
2023-07-31 15:09:21 +08:00
|
|
|
const triggerPaginationEvent =
|
|
|
|
(eventName: 'onChange' | 'onShowSizeChange') => (page: number, pageSize: number) => {
|
|
|
|
setPaginationCurrent(page);
|
|
|
|
setPaginationSize(pageSize);
|
|
|
|
if (pagination && pagination[eventName]) {
|
|
|
|
pagination?.[eventName]?.(page, pageSize);
|
|
|
|
}
|
|
|
|
};
|
2020-04-26 21:23:25 +08:00
|
|
|
|
|
|
|
const onPaginationChange = triggerPaginationEvent('onChange');
|
|
|
|
|
|
|
|
const onPaginationShowSizeChange = triggerPaginationEvent('onShowSizeChange');
|
2019-04-05 17:30:24 +08:00
|
|
|
|
2021-09-07 12:05:59 +08:00
|
|
|
const renderInnerItem = (item: T, index: number) => {
|
2020-05-19 14:55:46 +08:00
|
|
|
if (!renderItem) return null;
|
2019-05-15 11:12:55 +08:00
|
|
|
|
2017-09-25 15:24:16 +08:00
|
|
|
let key;
|
2017-09-06 15:53:25 +08:00
|
|
|
|
2017-09-25 15:24:16 +08:00
|
|
|
if (typeof rowKey === 'function') {
|
2019-02-22 10:40:30 +08:00
|
|
|
key = rowKey(item);
|
2021-09-07 12:05:59 +08:00
|
|
|
} else if (rowKey) {
|
2019-02-22 10:40:30 +08:00
|
|
|
key = item[rowKey];
|
2017-09-25 15:24:16 +08:00
|
|
|
} else {
|
2021-09-07 12:05:59 +08:00
|
|
|
key = (item as any).key;
|
2017-09-06 15:53:25 +08:00
|
|
|
}
|
|
|
|
|
2017-09-25 15:24:16 +08:00
|
|
|
if (!key) {
|
|
|
|
key = `list-item-${index}`;
|
2017-09-06 15:53:25 +08:00
|
|
|
}
|
|
|
|
|
2022-11-09 23:13:54 +08:00
|
|
|
return <React.Fragment key={key}>{renderItem(item, index)}</React.Fragment>;
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-09-06 15:53:25 +08:00
|
|
|
|
2020-12-09 17:12:32 +08:00
|
|
|
const isSomethingAfterLastItem = () => !!(loadMore || pagination || footer);
|
2017-10-26 15:36:48 +08:00
|
|
|
|
2020-04-26 21:23:25 +08:00
|
|
|
const prefixCls = getPrefixCls('list', customizePrefixCls);
|
2023-01-09 10:04:35 +08:00
|
|
|
|
2022-04-01 10:23:34 +08:00
|
|
|
// Style
|
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
|
2020-04-26 21:23:25 +08:00
|
|
|
let loadingProp = loading;
|
|
|
|
if (typeof loadingProp === 'boolean') {
|
|
|
|
loadingProp = {
|
|
|
|
spinning: loadingProp,
|
2018-05-04 17:37:31 +08:00
|
|
|
};
|
2020-04-26 21:23:25 +08:00
|
|
|
}
|
|
|
|
const isLoading = loadingProp && loadingProp.spinning;
|
|
|
|
|
2023-08-17 17:55:46 +08:00
|
|
|
const mergedSize = useSize(customizeSize);
|
|
|
|
|
2020-04-26 21:23:25 +08:00
|
|
|
// large => lg
|
|
|
|
// small => sm
|
|
|
|
let sizeCls = '';
|
2023-08-17 17:55:46 +08:00
|
|
|
switch (mergedSize) {
|
2020-04-26 21:23:25 +08:00
|
|
|
case 'large':
|
|
|
|
sizeCls = 'lg';
|
|
|
|
break;
|
|
|
|
case 'small':
|
|
|
|
sizeCls = 'sm';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-05-21 21:52:18 +08:00
|
|
|
|
2020-09-06 13:07:39 +08:00
|
|
|
const classString = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-vertical`]: itemLayout === 'vertical',
|
|
|
|
[`${prefixCls}-${sizeCls}`]: sizeCls,
|
|
|
|
[`${prefixCls}-split`]: split,
|
|
|
|
[`${prefixCls}-bordered`]: bordered,
|
|
|
|
[`${prefixCls}-loading`]: isLoading,
|
2021-04-02 22:37:36 +08:00
|
|
|
[`${prefixCls}-grid`]: !!grid,
|
2020-09-06 13:07:39 +08:00
|
|
|
[`${prefixCls}-something-after-last-item`]: isSomethingAfterLastItem(),
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
2023-07-06 09:43:03 +08:00
|
|
|
list?.className,
|
2020-09-06 13:07:39 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-04-01 10:23:34 +08:00
|
|
|
hashId,
|
2020-09-06 13:07:39 +08:00
|
|
|
);
|
2020-04-26 21:23:25 +08:00
|
|
|
|
2022-12-24 15:05:33 +08:00
|
|
|
const paginationProps = extendsObject<PaginationConfig>(
|
|
|
|
defaultPaginationProps,
|
|
|
|
{
|
|
|
|
total: dataSource.length,
|
|
|
|
current: paginationCurrent,
|
|
|
|
pageSize: paginationSize,
|
|
|
|
},
|
|
|
|
pagination || {},
|
|
|
|
);
|
2018-04-20 15:41:40 +08:00
|
|
|
|
2020-04-26 21:23:25 +08:00
|
|
|
const largestPage = Math.ceil(paginationProps.total / paginationProps.pageSize);
|
|
|
|
if (paginationProps.current > largestPage) {
|
|
|
|
paginationProps.current = largestPage;
|
|
|
|
}
|
|
|
|
const paginationContent = pagination ? (
|
2022-12-31 19:18:09 +08:00
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
`${prefixCls}-pagination`,
|
|
|
|
`${prefixCls}-pagination-align-${paginationProps?.align ?? 'end'}`,
|
|
|
|
)}
|
|
|
|
>
|
2020-04-26 21:23:25 +08:00
|
|
|
<Pagination
|
|
|
|
{...paginationProps}
|
|
|
|
onChange={onPaginationChange}
|
|
|
|
onShowSizeChange={onPaginationShowSizeChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
|
|
|
|
let splitDataSource = [...dataSource];
|
|
|
|
if (pagination) {
|
|
|
|
if (dataSource.length > (paginationProps.current - 1) * paginationProps.pageSize) {
|
|
|
|
splitDataSource = [...dataSource].splice(
|
|
|
|
(paginationProps.current - 1) * paginationProps.pageSize,
|
|
|
|
paginationProps.pageSize,
|
2019-05-06 12:04:39 +08:00
|
|
|
);
|
2017-11-17 11:25:29 +08:00
|
|
|
}
|
2020-04-26 21:23:25 +08:00
|
|
|
}
|
|
|
|
|
2022-11-13 14:33:07 +08:00
|
|
|
const needResponsive = Object.keys(grid || {}).some((key) =>
|
2022-02-21 16:48:39 +08:00
|
|
|
['xs', 'sm', 'md', 'lg', 'xl', 'xxl'].includes(key),
|
|
|
|
);
|
|
|
|
const screens = useBreakpoint(needResponsive);
|
2020-04-27 16:59:33 +08:00
|
|
|
const currentBreakpoint = React.useMemo(() => {
|
|
|
|
for (let i = 0; i < responsiveArray.length; i += 1) {
|
|
|
|
const breakpoint: Breakpoint = responsiveArray[i];
|
|
|
|
if (screens[breakpoint]) {
|
|
|
|
return breakpoint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}, [screens]);
|
2020-04-26 21:23:25 +08:00
|
|
|
|
2020-04-27 16:59:33 +08:00
|
|
|
const colStyle = React.useMemo(() => {
|
|
|
|
if (!grid) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const columnCount =
|
|
|
|
currentBreakpoint && grid[currentBreakpoint] ? grid[currentBreakpoint] : grid.column;
|
|
|
|
if (columnCount) {
|
|
|
|
return {
|
|
|
|
width: `${100 / columnCount}%`,
|
|
|
|
maxWidth: `${100 / columnCount}%`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}, [grid?.column, currentBreakpoint]);
|
2020-04-26 21:23:25 +08:00
|
|
|
|
2023-01-09 10:04:35 +08:00
|
|
|
let childrenContent: React.ReactNode = isLoading && <div style={{ minHeight: 53 }} />;
|
2020-04-27 16:59:33 +08:00
|
|
|
if (splitDataSource.length > 0) {
|
2021-09-07 12:05:59 +08:00
|
|
|
const items = splitDataSource.map((item: T, index: number) => renderInnerItem(item, index));
|
2020-04-26 21:23:25 +08:00
|
|
|
childrenContent = grid ? (
|
2022-11-09 23:13:54 +08:00
|
|
|
<Row gutter={grid.gutter}>
|
2022-11-13 14:33:07 +08:00
|
|
|
{React.Children.map(items, (child) => (
|
2022-11-09 23:13:54 +08:00
|
|
|
<div key={child?.key} style={colStyle}>
|
|
|
|
{child}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</Row>
|
2020-04-26 21:23:25 +08:00
|
|
|
) : (
|
2020-06-12 18:11:29 +08:00
|
|
|
<ul className={`${prefixCls}-items`}>{items}</ul>
|
2020-04-26 21:23:25 +08:00
|
|
|
);
|
|
|
|
} else if (!children && !isLoading) {
|
2023-01-09 10:04:35 +08:00
|
|
|
childrenContent = (
|
|
|
|
<div className={`${prefixCls}-empty-text`}>
|
|
|
|
{(locale && locale.emptyText) || renderEmpty?.('List') || (
|
|
|
|
<DefaultRenderEmpty componentName="List" />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
2020-04-26 21:23:25 +08:00
|
|
|
}
|
2017-08-14 11:08:19 +08:00
|
|
|
|
2020-04-26 21:23:25 +08:00
|
|
|
const paginationPosition = paginationProps.position || 'bottom';
|
2021-11-26 12:18:21 +08:00
|
|
|
const contextValue = React.useMemo(
|
|
|
|
() => ({ grid, itemLayout }),
|
|
|
|
[JSON.stringify(grid), itemLayout],
|
|
|
|
);
|
2018-05-21 21:52:18 +08:00
|
|
|
|
2022-04-01 10:23:34 +08:00
|
|
|
return wrapSSR(
|
2021-11-26 12:18:21 +08:00
|
|
|
<ListContext.Provider value={contextValue}>
|
2023-07-06 09:43:03 +08:00
|
|
|
<div style={{ ...list?.style, ...style }} className={classString} {...rest}>
|
2018-05-21 21:52:18 +08:00
|
|
|
{(paginationPosition === 'top' || paginationPosition === 'both') && paginationContent}
|
2017-09-26 16:11:35 +08:00
|
|
|
{header && <div className={`${prefixCls}-header`}>{header}</div>}
|
2018-04-19 13:48:55 +08:00
|
|
|
<Spin {...loadingProp}>
|
|
|
|
{childrenContent}
|
|
|
|
{children}
|
|
|
|
</Spin>
|
2017-09-26 16:11:35 +08:00
|
|
|
{footer && <div className={`${prefixCls}-footer`}>{footer}</div>}
|
2018-12-07 20:02:01 +08:00
|
|
|
{loadMore ||
|
|
|
|
((paginationPosition === 'bottom' || paginationPosition === 'both') && paginationContent)}
|
2017-08-09 15:45:08 +08:00
|
|
|
</div>
|
2022-04-01 10:23:34 +08:00
|
|
|
</ListContext.Provider>,
|
2020-04-26 21:23:25 +08:00
|
|
|
);
|
2017-08-09 15:45:08 +08:00
|
|
|
}
|
2020-04-26 21:23:25 +08:00
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
List.displayName = 'List';
|
|
|
|
}
|
|
|
|
|
2020-04-26 21:23:25 +08:00
|
|
|
List.Item = Item;
|
|
|
|
|
|
|
|
export default List;
|