forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
288 lines (265 loc) · 8.27 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import PropTypes from '../_util/vue-types';
import classNames from 'classnames';
import omit from 'omit.js';
import { ConfigConsumerProps } from '../config-provider';
import Spin from '../spin';
import Pagination, { PaginationConfig } from '../pagination';
import { Row } from '../grid';
import Item from './Item';
import { initDefaultProps, getComponent, getSlot } from '../_util/props-util';
import { cloneElement } from '../_util/vnode';
import { provide, inject } from 'vue';
export { ListItemProps, ListItemMetaProps } from './Item';
export const ColumnCount = ['', 1, 2, 3, 4, 6, 8, 12, 24];
export const ColumnType = ['gutter', 'column', 'xs', 'sm', 'md', 'lg', 'xl', 'xxl'];
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),
};
export const ListSize = ['small', 'default', 'large'];
export const ListProps = () => ({
bordered: PropTypes.bool,
dataSource: PropTypes.array,
extra: PropTypes.any,
grid: PropTypes.shape(ListGridType).loose,
itemLayout: PropTypes.string,
loading: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
loadMore: PropTypes.any,
pagination: PropTypes.oneOfType([PropTypes.shape(PaginationConfig()).loose, PropTypes.bool]),
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,
});
const List = {
inheritAttrs: false,
Item,
name: 'AList',
props: initDefaultProps(ListProps(), {
dataSource: [],
bordered: false,
split: true,
loading: false,
pagination: false,
}),
created() {
provide('listContext', this);
},
setup() {
return {
configProvider: inject('configProvider', ConfigConsumerProps),
};
},
data() {
this.keys = [];
this.defaultPaginationProps = {
current: 1,
pageSize: 10,
onChange: (page, pageSize) => {
const { pagination } = this;
this.paginationCurrent = page;
if (pagination && pagination.onChange) {
pagination.onChange(page, pageSize);
}
},
total: 0,
};
this.onPaginationChange = this.triggerPaginationEvent('onChange');
this.onPaginationShowSizeChange = this.triggerPaginationEvent('onShowSizeChange');
const { pagination } = this.$props;
const paginationObj = pagination && typeof pagination === 'object' ? pagination : {};
return {
paginationCurrent: paginationObj.defaultCurrent || 1,
paginationSize: paginationObj.defaultPageSize || 10,
};
},
methods: {
triggerPaginationEvent(eventName) {
return (page, pageSize) => {
const { pagination } = this.$props;
this.paginationCurrent = page;
this.paginationSize = pageSize;
if (pagination && pagination[eventName]) {
pagination[eventName](page, pageSize);
}
};
},
innerRenderItem(item, index) {
const {
$slots: { renderItem },
rowKey,
} = this;
const renderer = this.renderItem || renderItem;
if (!renderer) return null;
let key;
if (typeof rowKey === 'function') {
key = rowKey(item);
} else if (typeof rowKey === 'string') {
key = item[rowKey];
} else {
key = item.key;
}
if (!key) {
key = `list-item-${index}`;
}
this.keys[index] = key;
return renderer(item, index);
},
isSomethingAfterLastItem() {
const { pagination } = this;
const loadMore = getComponent(this, 'loadMore');
const footer = getComponent(this, 'footer');
return !!(loadMore || pagination || footer);
},
renderEmpty(prefixCls, renderEmpty) {
const { locale } = this;
return (
<div class={`${prefixCls}-empty-text`}>
{(locale && locale.emptyText) || renderEmpty('List')}
</div>
);
},
},
render() {
const {
prefixCls: customizePrefixCls,
bordered,
split,
itemLayout,
pagination,
grid,
dataSource = [],
size,
loading,
paginationCurrent,
paginationSize,
$attrs,
} = this;
const getPrefixCls = this.configProvider.getPrefixCls;
const prefixCls = getPrefixCls('list', customizePrefixCls);
const { class: _cls, ...restAttrs } = $attrs;
const loadMore = getComponent(this, 'loadMore');
const footer = getComponent(this, 'footer');
const header = getComponent(this, 'header');
const children = getSlot(this);
let loadingProp = loading;
if (typeof loadingProp === 'boolean') {
loadingProp = {
spinning: loadingProp,
};
}
const isLoading = loadingProp && loadingProp.spinning;
// large => lg
// small => sm
let sizeCls = '';
switch (size) {
case 'large':
sizeCls = 'lg';
break;
case 'small':
sizeCls = 'sm';
break;
default:
break;
}
const classString = classNames(
prefixCls,
{
[`${prefixCls}-vertical`]: itemLayout === 'vertical',
[`${prefixCls}-${sizeCls}`]: sizeCls,
[`${prefixCls}-split`]: split,
[`${prefixCls}-bordered`]: bordered,
[`${prefixCls}-loading`]: isLoading,
[`${prefixCls}-grid`]: grid,
[`${prefixCls}-something-after-last-item`]: this.isSomethingAfterLastItem(),
},
$attrs.class,
);
const paginationProps = {
...this.defaultPaginationProps,
total: dataSource.length,
current: paginationCurrent,
pageSize: paginationSize,
...(pagination || {}),
};
classString;
const largestPage = Math.ceil(paginationProps.total / paginationProps.pageSize);
if (paginationProps.current > largestPage) {
paginationProps.current = largestPage;
}
const { class: cls, style, ...restProps } = paginationProps;
const paginationContent = pagination ? (
<div class={`${prefixCls}-pagination`}>
<Pagination
{...{
props: omit(restProps, ['onChange']),
class: cls,
style,
on: {
change: this.onPaginationChange,
showSizeChange: this.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,
);
}
}
let childrenContent;
childrenContent = isLoading && <div style={{ minHeight: 53 }} />;
if (splitDataSource.length > 0) {
const items = splitDataSource.map((item, index) => this.innerRenderItem(item, index));
const childrenList = items.map((child, index) =>
cloneElement(child, {
key: this.keys[index],
}),
);
childrenContent = grid ? (
<Row gutter={grid.gutter}>{childrenList}</Row>
) : (
<ul class={`${prefixCls}-items`}>{childrenList}</ul>
);
} else if (!children.length && !isLoading) {
const renderEmpty = this.configProvider.renderEmpty;
childrenContent = this.renderEmpty(prefixCls, renderEmpty);
}
const paginationPosition = paginationProps.position || 'bottom';
return (
<div class={classString} {...restAttrs}>
{(paginationPosition === 'top' || paginationPosition === 'both') && paginationContent}
{header && <div class={`${prefixCls}-header`}>{header}</div>}
<Spin {...loadingProp}>
{childrenContent}
{children}
</Spin>
{footer && <div class={`${prefixCls}-footer`}>{footer}</div>}
{loadMore ||
((paginationPosition === 'bottom' || paginationPosition === 'both') && paginationContent)}
</div>
);
},
};
/* istanbul ignore next */
List.install = function(app) {
app.component(List.name, List);
app.component(List.Item.name, List.Item);
app.component(List.Item.Meta.name, List.Item.Meta);
};
export default List;