forked from Tencent/tdesign-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect-panel.tsx
More file actions
344 lines (315 loc) · 9.7 KB
/
select-panel.tsx
File metadata and controls
344 lines (315 loc) · 9.7 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import {
computed, defineComponent, toRefs, inject, onMounted, onBeforeUnmount,
} from '@vue/composition-api';
import isFunction from 'lodash/isFunction';
import { VNode } from 'vue';
import { useTNodeJSX } from '../hooks/tnode';
import { renderTNodeJSXDefault } from '../utils/render-tnode';
import { useConfig, usePrefixClass } from '../config-provider/useConfig';
import {
TdOptionProps, SelectOptionGroup, TdSelectProps, SelectOption,
} from './type';
import Option from './option';
import useVirtualScroll from '../hooks/useVirtualScroll';
export interface OptionsType extends TdOptionProps {
$index?: number;
}
type SelectPanelProps = Pick<
TdSelectProps,
| 'size'
| 'multiple'
| 'empty'
| 'options'
| 'loadingText'
| 'loading'
| 'valueType'
| 'keys'
| 'panelTopContent'
| 'panelBottomContent'
| 'inputValue'
| 'scroll'
| 'creatable'
| 'filterable'
| 'filter'
>;
const sizeClassMap = {
small: 's',
medium: 'm',
large: 'l',
};
export default defineComponent({
name: 'TSelectPanel',
components: {
TOption: Option,
},
props: [
'inputValue',
'panelTopContent',
'panelBottomContent',
'size',
'options',
'empty',
'filter',
'loading',
'loadingText',
'multiple',
'scroll',
'creatable',
'filterable',
],
setup(props: SelectPanelProps) {
const { options, inputValue } = toRefs(props);
const renderTNode = useTNodeJSX();
const { t, global } = useConfig('select');
const selectProvider: any = inject('tSelect');
const COMPONENT_NAME = usePrefixClass('select');
const panelContentRef = computed(() => selectProvider.getOverlayElm());
const {
type,
rowHeight = 28, // 默认每行高度28
bufferSize = 20,
isFixedRowHeight = false,
threshold = 100,
} = props.scroll || {};
const displayOptions = computed(() => {
if (!inputValue.value || props.creatable || !(props.filterable || isFunction(props.filter))) return options.value;
const filterMethods = (option: SelectOption) => {
if (isFunction(props.filter)) {
return props.filter(`${props.inputValue}`, option);
}
return option.label?.indexOf(`${props.inputValue}`) > -1;
};
const res: SelectOption[] = [];
props.options.forEach((option) => {
if ((option as SelectOptionGroup).group && (option as SelectOptionGroup).children) {
res.push({
...option,
children: (option as SelectOptionGroup).children.filter(filterMethods),
});
}
if (filterMethods(option)) {
res.push(option);
}
});
return res;
});
const isCreateOptionShown = computed(() => props.creatable && props.filterable && props.inputValue);
const isEmpty = computed(() => !displayOptions.value.length);
const isVirtual = computed(
() => props.scroll?.type === 'virtual' && props.options?.length > (props.scroll?.threshold || 100),
);
const {
trs = null,
visibleData = null,
handleScroll: handleVirtualScroll = null,
scrollHeight = null,
translateY = null,
handleRowMounted = null,
} = type === 'virtual'
? useVirtualScroll({
container: panelContentRef,
data: displayOptions,
fixedHeight: isFixedRowHeight,
lineHeight: rowHeight,
bufferSize,
threshold,
})
: {};
let lastScrollY = -1;
const onInnerVirtualScroll = (e: WheelEvent) => {
if (!isVirtual.value) {
return;
}
const target = (e.target || e.srcElement) as HTMLElement;
const top = target.scrollTop;
// 排除横向滚动出发的纵向虚拟滚动计算
if (Math.abs(lastScrollY - top) > 5) {
handleVirtualScroll();
lastScrollY = top;
} else {
lastScrollY = -1;
}
};
// 监听popup滚动 处理虚拟滚动时的virtualData变化
onMounted(() => {
if (props.scroll?.type === 'virtual') {
selectProvider.getOverlayElm().addEventListener('scroll', onInnerVirtualScroll);
}
});
// 卸载时取消监听
onBeforeUnmount(() => {
if (props.scroll?.type === 'virtual') {
selectProvider.getOverlayElm().removeEventListener('scroll', onInnerVirtualScroll);
}
});
return {
t,
global,
isEmpty,
renderTNode,
selectProvider,
isCreateOptionShown,
// 虚拟滚动相关
trs,
isVirtual,
onInnerVirtualScroll,
visibleData,
scrollHeight,
translateY,
scrollType: props.scroll?.type,
handleRowMounted,
bufferSize,
threshold,
displayOptions,
componentName: COMPONENT_NAME,
};
},
methods: {
renderEmptyContent() {
const { empty, t, global } = this;
if (empty && typeof empty === 'string') {
return <div class={`${this.componentName}__empty`}>{empty}</div>;
}
return renderTNodeJSXDefault(this, 'empty', <div class={`${this.componentName}__empty`}>{t(global.empty)}</div>);
},
renderLoadingContent() {
const { loadingText, t, global } = this;
if (loadingText && typeof loadingText === 'string') {
return <div class={`${this.componentName}__loading-tips`}>{loadingText}</div>;
}
return renderTNodeJSXDefault(
this,
'loadingText',
<div class={`${this.componentName}__loading-tips`}>{t(global.loadingText)}</div>,
);
},
renderCreateOption() {
const {
inputValue, trs, scrollType, isVirtual, handleRowMounted, bufferSize,
} = this;
const on = isVirtual ? { onRowMounted: handleRowMounted } : {};
return (
<ul class={[`${this.componentName}__create-option`, `${this.componentName}__list`]}>
<t-option
isCreatedOption={true}
value={inputValue}
label={inputValue}
class={`${this.componentName}__create-option--special`}
trs={trs}
scrollType={scrollType}
isVirtual={isVirtual}
bufferSize={bufferSize}
on={on}
/>
</ul>
);
},
// 递归render options
renderOptionsContent(options: SelectOption[]) {
const {
multiple, trs, scrollType, isVirtual, handleRowMounted, bufferSize,
} = this;
const on = isVirtual ? { onRowMounted: handleRowMounted } : {};
return (
<ul class={`${this.componentName}__list`}>
{options.map(
(
item: SelectOptionGroup &
TdOptionProps & {
slots: VNode;
class: string | undefined;
style: { [key: string]: string } | undefined;
} & OptionsType,
index,
) => {
if (item.group) {
return (
<t-option-group label={item.group} divider={item.divider}>
{this.renderOptionsContent(item.children)}
</t-option-group>
);
}
const scrollProps = isVirtual
? {
rowIndex: item.$index,
trs,
scrollType,
isVirtual,
bufferSize,
}
: { key: index };
// replace `scopedSlots` of `v-slots` in Vue3
return (
<t-option
// 透传 class
class={item.class}
// 透传 style
style={item.style}
// 透传其余参数
{...{ props: { ...item, ...scrollProps } }}
// t-option 自身逻辑所需属性
multiple={multiple}
scopedSlots={{ default: item.slots }}
key={`${item.$index || ''}_${index}`}
on={on}
/>
);
},
)}
</ul>
);
},
renderPanelContent(innerStyle = {}) {
const {
renderTNode, isEmpty, isCreateOptionShown, size, loading, isVirtual, visibleData, displayOptions,
} = this;
return (
<div
class={[
`${this.componentName}__dropdown-inner`,
`${this.componentName}__dropdown-inner--size-${sizeClassMap[size]}`,
]}
style={innerStyle}
>
{renderTNode('panelTopContent')}
{isCreateOptionShown && this.renderCreateOption()}
{loading && this.renderLoadingContent()}
{!loading && isEmpty && this.renderEmptyContent()}
{!loading && !isEmpty && this.renderOptionsContent(isVirtual && visibleData ? visibleData : displayOptions)}
{renderTNode('panelBottomContent')}
</div>
);
},
},
render() {
const { translateY, scrollHeight, isVirtual } = this;
// 虚拟滚动渲染,popup 的 dom 结构有区别
if (isVirtual) {
const cursorTranslate = `translate(0, ${scrollHeight}px)`;
const cursorTranslateStyle = {
position: 'absolute',
width: '1px',
height: '1px',
transition: 'transform 0.2s',
transform: cursorTranslate,
'-ms-transform': cursorTranslate,
'-moz-transform': cursorTranslate,
'-webkit-transform': cursorTranslate,
};
const translate = `translate(0, ${translateY}px)`;
const virtualStyle = {
transform: translate,
'-ms-transform': translate,
'-moz-transform': translate,
'-webkit-transform': translate,
};
return (
<div>
<div style={{ ...cursorTranslateStyle }}></div>
{this.renderPanelContent(virtualStyle)}
</div>
);
}
return (this.renderPanelContent as Function)();
},
});