ant-design-vue/components/list/index.jsx

251 lines
7.2 KiB
Vue
Raw Normal View History

2019-01-12 11:33:27 +08:00
import PropTypes from '../_util/vue-types';
import classNames from 'classnames';
import omit from 'omit.js';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import defaultLocale from '../locale-provider/default';
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';
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
const List = {
2018-06-16 21:30:41 +08:00
Item,
name: 'AList',
props: initDefaultProps(ListProps(), {
dataSource: [],
prefixCls: 'ant-list',
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-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) {
const { dataSource, $scopedSlots, rowKey } = this;
let key;
const renderItem = this.renderItem || $scopedSlots.renderItem;
2018-06-16 21:30:41 +08:00
if (typeof rowKey === 'function') {
2019-01-12 11:33:27 +08:00
key = rowKey(dataSource[index]);
2018-06-16 21:30:41 +08:00
} else if (typeof rowKey === 'string') {
2019-01-12 11:33:27 +08:00
key = dataSource[rowKey];
2018-06-16 21:30:41 +08:00
} else {
2019-01-12 11:33:27 +08:00
key = dataSource.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-01-12 11:33:27 +08:00
renderEmpty(contextLocale) {
const locale = { ...contextLocale, ...this.locale };
return <div class={`${this.prefixCls}-empty-text`}>{locale.emptyText}</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 {
bordered,
split,
itemLayout,
pagination,
prefixCls,
grid,
dataSource,
size,
loading,
$listeners,
$slots,
paginationCurrent,
2019-01-12 11:33:27 +08:00
} = this;
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,
update to antd3.8.3 (#159) * refactor: align * feat: update align to 2.4.3 * feat: update trigger 2.5.4 * feat: update tooltip 3.7.2 * fix: align * feat: update vc-calendar to 9.6.2 * feat: update vc-checkbox to 2.1.5 * feat: update vc-dialog to 7.1.8 * feat: update vc-from to 2.2.1 * feat: update vc-notification to 3.1.1 * test: update snapshots * feat: update vc-tree to 1.12.6 * feat: update vc-table to 6.2.8 * feat: update vc-upload to 2.5.1 * feat: update vc-input-number to 4.0.12 * feat: update vc-tabs to 9.2.6 * refactor: vc-menu * refactor: update vc-menu to 7.0.5 * style: remove unused * feat: update pagination to 1.16.5 * feat: add vc-progress 2.2.5 tag * feat: add vc-rate 2.4.0 tag * feat: update vc-slider to 8.6.1 * fix: tooltip error * style: delete conosle * feat: update vc-steps to 3.1.1 * add vc-switch tag 1.6.0 * feat: update upload to 2.5.1 * fix: update vc-menu * fix: update store * fix: add ref dir * fix: trigger mock shouldComponentUpdate * fix: update vc-select * revert: trigger lazyrenderbox * fix: update vc-select * fix: update vc-select * fix: update vc-select * fix: update vc-menu * fix: update vc-slick ref * update style to 3.8.2 * test: update snapshots * update vc-select * update util & affix * feat: add drawer * fix: support title add slot mode * test: update affix test * update alert * update anchor * update snapshots * fix: doc and vc-drawer * update select & auto-complete * update back-top & grid * feractor: avatar * test: add drawer test * update badge * update button * update card * update divider * feat: update vc-tabs to 9.3.6 and tabs * add afterEnter callback * update form * fix: update drawer * test: update snapshots * update modal & notification * test: update snapshots * update message * update locale-provider * update dropdown * update layout popconfirm popover * update time-picker * update menu * update date-picker * docs: update input docs * update input * update snapshots * update table * update test snapshots * feat: update progress * update checkbox * feat: update spin * update radio * docs: slider steps timeline * update list * update transfer * update collapse * update cascader * update upload
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) {
2018-06-16 21:30:41 +08:00
childrenContent = (
<LocaleReceiver
2019-01-12 11:33:27 +08:00
componentName="Table"
2018-06-16 21:30:41 +08:00
defaultLocale={defaultLocale.Table}
2019-01-12 11:33:27 +08:00
scopedSlots={{ default: this.renderEmpty }}
2018-06-16 21:30:41 +08:00
/>
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 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
};
/* istanbul ignore next */
2019-01-12 11:33:27 +08:00
List.install = function(Vue) {
Vue.component(List.name, List);
Vue.component(List.Item.name, List.Item);
Vue.component(List.Item.Meta.name, List.Item.Meta);
};
2019-01-12 11:33:27 +08:00
export default List;