-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathListBody.tsx
172 lines (148 loc) · 4.42 KB
/
ListBody.tsx
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
import type { ExtractPropTypes } from 'vue';
import { defineComponent, computed, ref, watch } from 'vue';
import classNames from '../_util/classNames';
import ListItem from './ListItem';
import Pagination from '../pagination';
import PropTypes from '../_util/vue-types';
import type { TransferItem } from '.';
import { booleanType } from '../_util/type';
export const transferListBodyProps = {
prefixCls: String,
filteredRenderItems: PropTypes.array.def([]),
selectedKeys: PropTypes.array,
disabled: booleanType(),
showRemove: booleanType(),
pagination: PropTypes.any,
onItemSelect: Function,
onScroll: Function,
onItemRemove: Function,
};
export type TransferListBodyProps = Partial<ExtractPropTypes<typeof transferListBodyProps>>;
function parsePagination(pagination) {
if (!pagination) {
return null;
}
const defaultPagination = {
pageSize: 10,
simple: true,
showSizeChanger: false,
showLessItems: false,
};
if (typeof pagination === 'object') {
return {
...defaultPagination,
...pagination,
};
}
return defaultPagination;
}
const ListBody = defineComponent({
compatConfig: { MODE: 3 },
name: 'ListBody',
inheritAttrs: false,
props: transferListBodyProps,
emits: ['itemSelect', 'itemRemove', 'scroll'],
setup(props, { emit, expose }) {
const current = ref(1);
const handleItemSelect = (item: TransferItem) => {
const { selectedKeys } = props;
const checked = selectedKeys.indexOf(item.key) >= 0;
emit('itemSelect', item.key, !checked);
};
const handleItemRemove = (item: TransferItem) => {
emit('itemRemove', [item.key]);
};
const handleScroll = (e: Event) => {
emit('scroll', e);
};
const mergedPagination = computed(() => parsePagination(props.pagination));
watch(
[mergedPagination, () => props.filteredRenderItems],
() => {
if (mergedPagination.value) {
// Calculate the page number
const maxPageCount = Math.ceil(
props.filteredRenderItems.length / mergedPagination.value.pageSize,
);
if (current.value > maxPageCount) {
current.value = maxPageCount;
}
}
},
{ immediate: true },
);
const items = computed(() => {
const { filteredRenderItems } = props;
let displayItems = filteredRenderItems;
if (mergedPagination.value) {
displayItems = filteredRenderItems.slice(
(current.value - 1) * mergedPagination.value.pageSize,
current.value * mergedPagination.value.pageSize,
);
}
return displayItems;
});
const onPageChange = (cur: number) => {
current.value = cur;
};
expose({ items });
return () => {
const {
prefixCls,
filteredRenderItems,
selectedKeys,
disabled: globalDisabled,
showRemove,
} = props;
let paginationNode = null;
if (mergedPagination.value) {
paginationNode = (
<Pagination
simple={mergedPagination.value.simple}
showSizeChanger={mergedPagination.value.showSizeChanger}
showLessItems={mergedPagination.value.showLessItems}
size="small"
disabled={globalDisabled}
class={`${prefixCls}-pagination`}
total={filteredRenderItems.length}
pageSize={mergedPagination.value.pageSize}
current={current.value}
onChange={onPageChange}
/>
);
}
const itemsList = items.value.map(({ renderedEl, renderedText, item }: any) => {
const { disabled } = item;
const checked = selectedKeys.indexOf(item.key) >= 0;
return (
<ListItem
disabled={globalDisabled || disabled}
key={item.key}
item={item}
renderedText={renderedText}
renderedEl={renderedEl}
checked={checked}
prefixCls={prefixCls}
onClick={handleItemSelect}
onRemove={handleItemRemove}
showRemove={showRemove}
/>
);
});
return (
<>
<ul
class={classNames(`${prefixCls}-content`, {
[`${prefixCls}-content-show-remove`]: showRemove,
})}
onScroll={handleScroll}
>
{itemsList}
</ul>
{paginationNode}
</>
);
};
},
});
export default ListBody;