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