2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import omit from 'omit.js';
|
2019-03-19 02:02:14 +00:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2018-06-16 13:30:41 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
import Spin from '../spin';
|
|
|
|
import Pagination, { PaginationConfig } from '../pagination';
|
|
|
|
import { Row } from '../grid';
|
2018-06-16 13:30:41 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
import Item from './Item';
|
|
|
|
import { initDefaultProps, getComponentFromProp, filterEmpty } from '../_util/props-util';
|
|
|
|
import { cloneElement } from '../_util/vnode';
|
2019-08-28 02:50:19 +00:00
|
|
|
import Base from '../base';
|
2018-06-16 13:30:41 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export { ListItemProps, ListItemMetaProps } from './Item';
|
2018-06-16 13:30:41 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export const ColumnCount = ['', 1, 2, 3, 4, 6, 8, 12, 24];
|
2018-06-16 13:30:41 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export const ColumnType = ['gutter', 'column', 'xs', 'sm', 'md', 'lg', 'xl', 'xxl'];
|
2018-06-16 13:30:41 +00:00
|
|
|
|
|
|
|
export const ListGridType = {
|
|
|
|
gutter: PropTypes.number,
|
|
|
|
column: PropTypes.oneOf(ColumnCount),
|
|
|
|
xs: PropTypes.oneOf(ColumnCount),
|
|
|
|
sm: PropTypes.oneOf(ColumnCount),
|
|
|
|
md: PropTypes.oneOf(ColumnCount),
|
|
|
|
lg: PropTypes.oneOf(ColumnCount),
|
|
|
|
xl: PropTypes.oneOf(ColumnCount),
|
|
|
|
xxl: PropTypes.oneOf(ColumnCount),
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-06-16 13:30:41 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export const ListSize = ['small', 'default', 'large'];
|
2018-06-16 13:30:41 +00:00
|
|
|
|
|
|
|
export const ListProps = () => ({
|
|
|
|
bordered: PropTypes.bool,
|
|
|
|
dataSource: PropTypes.any,
|
|
|
|
extra: PropTypes.any,
|
|
|
|
grid: PropTypes.shape(ListGridType).loose,
|
|
|
|
itemLayout: PropTypes.string,
|
2018-06-16 14:57:11 +00:00
|
|
|
loading: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
|
2018-06-16 13:30:41 +00:00
|
|
|
loadMore: PropTypes.any,
|
2019-01-12 03:33:27 +00:00
|
|
|
pagination: PropTypes.oneOfType([PropTypes.shape(PaginationConfig()).loose, PropTypes.bool]),
|
2018-06-16 13:30:41 +00:00
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
rowKey: PropTypes.any,
|
|
|
|
renderItem: PropTypes.any,
|
|
|
|
size: PropTypes.oneOf(ListSize),
|
|
|
|
split: PropTypes.bool,
|
|
|
|
header: PropTypes.any,
|
|
|
|
footer: PropTypes.any,
|
|
|
|
locale: PropTypes.object,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-06-16 13:30:41 +00:00
|
|
|
|
2018-09-19 05:21:57 +00:00
|
|
|
const List = {
|
2018-06-16 13:30:41 +00:00
|
|
|
Item,
|
|
|
|
name: 'AList',
|
|
|
|
props: initDefaultProps(ListProps(), {
|
|
|
|
dataSource: [],
|
|
|
|
bordered: false,
|
|
|
|
split: true,
|
|
|
|
loading: false,
|
|
|
|
pagination: false,
|
|
|
|
}),
|
2019-01-12 03:33:27 +00:00
|
|
|
provide() {
|
2018-06-16 13:30:41 +00:00
|
|
|
return {
|
|
|
|
listContext: this,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-06-16 13:30:41 +00:00
|
|
|
},
|
2019-03-19 02:02:14 +00:00
|
|
|
inject: {
|
2019-09-11 14:35:25 +00:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
2019-03-19 02:02:14 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
|
|
|
this.keys = [];
|
2018-06-16 13:30:41 +00:00
|
|
|
this.defaultPaginationProps = {
|
|
|
|
current: 1,
|
|
|
|
pageSize: 10,
|
|
|
|
onChange: (page, pageSize) => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const { pagination } = this;
|
|
|
|
this.paginationCurrent = page;
|
2018-06-16 13:30:41 +00:00
|
|
|
if (pagination && pagination.onChange) {
|
2019-01-12 03:33:27 +00:00
|
|
|
pagination.onChange(page, pageSize);
|
2018-06-16 13:30:41 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
total: 0,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-06-16 13:30:41 +00:00
|
|
|
return {
|
|
|
|
paginationCurrent: 1,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-06-16 13:30:41 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
renderItem2(item, index) {
|
2019-03-19 02:02:14 +00:00
|
|
|
const { $scopedSlots, rowKey } = this;
|
2019-01-12 03:33:27 +00:00
|
|
|
let key;
|
|
|
|
const renderItem = this.renderItem || $scopedSlots.renderItem;
|
2018-06-16 13:30:41 +00:00
|
|
|
if (typeof rowKey === 'function') {
|
2019-03-19 02:02:14 +00:00
|
|
|
key = rowKey(item);
|
2018-06-16 13:30:41 +00:00
|
|
|
} else if (typeof rowKey === 'string') {
|
2019-03-19 02:02:14 +00:00
|
|
|
key = item[rowKey];
|
2018-06-16 13:30:41 +00:00
|
|
|
} else {
|
2019-03-19 02:02:14 +00:00
|
|
|
key = item.key;
|
2018-06-16 13:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!key) {
|
2019-01-12 03:33:27 +00:00
|
|
|
key = `list-item-${index}`;
|
2018-06-16 13:30:41 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
this.keys[index] = key;
|
2018-06-16 13:30:41 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
return renderItem(item, index);
|
2018-06-16 13:30:41 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
isSomethingAfterLastItem() {
|
|
|
|
const { pagination } = this;
|
|
|
|
const loadMore = getComponentFromProp(this, 'loadMore');
|
|
|
|
const footer = getComponentFromProp(this, 'footer');
|
|
|
|
return !!(loadMore || pagination || footer);
|
2018-06-16 13:30:41 +00:00
|
|
|
},
|
|
|
|
|
2019-03-19 02:02:14 +00:00
|
|
|
renderEmpty(prefixCls, renderEmpty) {
|
|
|
|
const locale = this;
|
|
|
|
return (
|
|
|
|
<div class={`${prefixCls}-empty-text`}>
|
2019-05-28 03:37:38 +00:00
|
|
|
{(locale && locale.emptyText) || renderEmpty(h, 'List')}
|
2019-03-19 02:02:14 +00:00
|
|
|
</div>
|
|
|
|
);
|
2018-06-16 13:30:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2018-06-16 13:30:41 +00:00
|
|
|
const {
|
2019-03-19 02:02:14 +00:00
|
|
|
prefixCls: customizePrefixCls,
|
2018-06-16 13:30:41 +00:00
|
|
|
bordered,
|
|
|
|
split,
|
|
|
|
itemLayout,
|
|
|
|
pagination,
|
|
|
|
grid,
|
|
|
|
dataSource,
|
|
|
|
size,
|
|
|
|
loading,
|
|
|
|
$listeners,
|
|
|
|
$slots,
|
|
|
|
paginationCurrent,
|
2019-01-12 03:33:27 +00:00
|
|
|
} = this;
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-19 02:02:14 +00:00
|
|
|
const prefixCls = getPrefixCls('list', customizePrefixCls);
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const loadMore = getComponentFromProp(this, 'loadMore');
|
|
|
|
const footer = getComponentFromProp(this, 'footer');
|
|
|
|
const header = getComponentFromProp(this, 'header');
|
|
|
|
const children = filterEmpty($slots.default || []);
|
|
|
|
let loadingProp = loading;
|
2018-06-16 13:30:41 +00:00
|
|
|
if (typeof loadingProp === 'boolean') {
|
|
|
|
loadingProp = {
|
|
|
|
spinning: loadingProp,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-06-16 13:30:41 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
const isLoading = loadingProp && loadingProp.spinning;
|
2018-06-16 13:30:41 +00:00
|
|
|
|
|
|
|
// large => lg
|
|
|
|
// small => sm
|
2019-01-12 03:33:27 +00:00
|
|
|
let sizeCls = '';
|
2018-06-16 13:30:41 +00:00
|
|
|
switch (size) {
|
|
|
|
case 'large':
|
2019-01-12 03:33:27 +00:00
|
|
|
sizeCls = 'lg';
|
|
|
|
break;
|
2018-06-16 13:30:41 +00:00
|
|
|
case 'small':
|
2019-01-12 03:33:27 +00:00
|
|
|
sizeCls = 'sm';
|
|
|
|
break;
|
2018-06-16 13:30:41 +00:00
|
|
|
default:
|
2019-01-12 03:33:27 +00:00
|
|
|
break;
|
2018-06-16 13:30:41 +00:00
|
|
|
}
|
|
|
|
const classString = classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-vertical`]: itemLayout === 'vertical',
|
|
|
|
[`${prefixCls}-${sizeCls}`]: sizeCls,
|
|
|
|
[`${prefixCls}-split`]: split,
|
|
|
|
[`${prefixCls}-bordered`]: bordered,
|
|
|
|
[`${prefixCls}-loading`]: isLoading,
|
|
|
|
[`${prefixCls}-grid`]: grid,
|
2018-09-05 13:28:54 +00:00
|
|
|
[`${prefixCls}-something-after-last-item`]: this.isSomethingAfterLastItem(),
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-06-16 13:30:41 +00:00
|
|
|
const paginationProps = {
|
|
|
|
...this.defaultPaginationProps,
|
|
|
|
total: dataSource.length,
|
|
|
|
current: paginationCurrent,
|
2019-01-12 03:33:27 +00:00
|
|
|
...(pagination || {}),
|
|
|
|
};
|
|
|
|
const largestPage = Math.ceil(paginationProps.total / paginationProps.pageSize);
|
2018-06-16 13:30:41 +00:00
|
|
|
if (paginationProps.current > largestPage) {
|
2019-01-12 03:33:27 +00:00
|
|
|
paginationProps.current = largestPage;
|
2018-06-16 13:30:41 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
const { class: cls, style, onShowSizeChange = () => {}, ...restProps } = paginationProps;
|
2018-06-16 13:30:41 +00:00
|
|
|
const paginationContent = pagination ? (
|
|
|
|
<div class={`${prefixCls}-pagination`}>
|
|
|
|
<Pagination
|
|
|
|
{...{
|
|
|
|
props: omit(restProps, ['onChange']),
|
2019-01-12 03:33:27 +00:00
|
|
|
class: cls,
|
|
|
|
style,
|
2018-06-16 13:30:41 +00:00
|
|
|
on: { change: this.defaultPaginationProps.onChange, showSizeChange: onShowSizeChange },
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
) : null;
|
2018-06-16 13:30:41 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
let splitDataSource = [...dataSource];
|
2018-06-16 13:30:41 +00:00
|
|
|
if (pagination) {
|
2019-01-12 03:33:27 +00:00
|
|
|
if (dataSource.length > (paginationProps.current - 1) * paginationProps.pageSize) {
|
2018-06-16 13:30:41 +00:00
|
|
|
splitDataSource = [...dataSource].splice(
|
|
|
|
(paginationProps.current - 1) * paginationProps.pageSize,
|
|
|
|
paginationProps.pageSize,
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-06-16 13:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
let childrenContent;
|
|
|
|
childrenContent = isLoading && <div style={{ minHeight: 53 }} />;
|
2018-06-16 13:30:41 +00:00
|
|
|
if (splitDataSource.length > 0) {
|
2019-01-12 03:33:27 +00:00
|
|
|
const items = splitDataSource.map((item, index) => this.renderItem2(item, index));
|
|
|
|
const childrenList = items.map((child, index) =>
|
|
|
|
cloneElement(child, {
|
|
|
|
key: this.keys[index],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
childrenContent = grid ? <Row gutter={grid.gutter}>{childrenList}</Row> : childrenList;
|
2018-06-16 14:57:11 +00:00
|
|
|
} else if (!children.length && !isLoading) {
|
2019-09-11 14:35:25 +00:00
|
|
|
const renderEmpty = this.configProvider.renderEmpty;
|
2019-03-19 02:02:14 +00:00
|
|
|
childrenContent = this.renderEmpty(prefixCls, renderEmpty);
|
2018-06-16 13:30:41 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
const paginationPosition = paginationProps.position || 'bottom';
|
2018-06-16 13:30:41 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div class={classString} {...{ on: $listeners }}>
|
|
|
|
{(paginationPosition === 'top' || paginationPosition === 'both') && paginationContent}
|
|
|
|
{header && <div class={`${prefixCls}-header`}>{header}</div>}
|
|
|
|
<Spin {...{ props: loadingProp }}>
|
|
|
|
{childrenContent}
|
|
|
|
{children}
|
|
|
|
</Spin>
|
|
|
|
{footer && <div class={`${prefixCls}-footer`}>{footer}</div>}
|
2019-01-12 03:33:27 +00:00
|
|
|
{loadMore ||
|
|
|
|
((paginationPosition === 'bottom' || paginationPosition === 'both') && paginationContent)}
|
2018-06-16 13:30:41 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-06-16 13:30:41 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-09-19 05:21:57 +00:00
|
|
|
|
|
|
|
/* istanbul ignore next */
|
2019-01-12 03:33:27 +00:00
|
|
|
List.install = function(Vue) {
|
2019-08-28 02:50:19 +00:00
|
|
|
Vue.use(Base);
|
2019-01-12 03:33:27 +00:00
|
|
|
Vue.component(List.name, List);
|
|
|
|
Vue.component(List.Item.name, List.Item);
|
|
|
|
Vue.component(List.Item.Meta.name, List.Item.Meta);
|
|
|
|
};
|
2018-09-19 05:21:57 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export default List;
|