-
Notifications
You must be signed in to change notification settings - Fork 287
/
Copy pathuse-select-content.ts
134 lines (116 loc) · 3.99 KB
/
use-select-content.ts
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
import { computed, inject, ref, getCurrentInstance } from 'vue';
import { SELECT_TOKEN } from '../const';
import { FORM_ITEM_TOKEN } from '../../../form';
import { OptionObjectItem } from '../select-types';
import { useNamespace } from '../../../shared/hooks/use-namespace';
import { className } from '../utils';
import { isFunction } from 'lodash';
import { createI18nTranslate } from '../../../locale/create';
export default function useSelectContent() {
const ns = useNamespace('select');
const select = inject(SELECT_TOKEN);
const formItemContext = inject(FORM_ITEM_TOKEN, undefined);
const app = getCurrentInstance();
const t = createI18nTranslate('DSelect', app);
const searchQuery = ref('');
const selectedData = computed<OptionObjectItem[]>(() => {
return select?.selectedOptions || [];
});
const isSelectDisable = computed<boolean>(() => !!select?.selectDisabled);
const isSupportCollapseTags = computed<boolean>(() => !!select?.collapseTags);
const isSupportTagsTooltip = computed<boolean>(() => !!select?.collapseTagsTooltip);
const isValidateError = computed(() => formItemContext?.validateState === 'error');
const isReadOnly = computed<boolean>(() => {
if (select) {
return isFunction(select.filter) ? false : !(typeof select.filter === 'boolean' && select.filter);
} else {
return true;
}
});
const displayInputValue = computed(() => {
if (select?.selectedOptions) {
return select.selectedOptions.length > 1
? select.selectedOptions.map((item) => item?.name || item?.value || '').join(',')
: select.selectedOptions[0]?.name || '';
} else {
return '';
}
});
// 是否可清空
const mergeClearable = computed<boolean>(() => {
return !isSelectDisable.value && !!select?.allowClear && (displayInputValue.value ? true : false);
});
// 是否禁用Tooltip
const isDisabledTooltip = computed<boolean>(() => {
return !isSupportTagsTooltip.value || !!select?.isOpen;
});
const selectionCls = computed(() => {
return className(ns.e('selection'), {
[ns.e('clearable')]: mergeClearable.value,
[ns.em('selection', 'error')]: isValidateError.value,
[ns.em('selection', 'glow-style')]: Boolean(select?.showGlowStyle),
});
});
const inputCls = computed(() => {
return className(ns.e('input'), {
[ns.em('input', 'lg')]: select?.selectSize === 'lg',
[ns.em('input', 'sm')]: select?.selectSize === 'sm',
});
});
const tagSize = computed(() => select?.selectSize || 'sm');
const placeholder = computed<string>(() => (displayInputValue.value ? '' : select?.placeholder || t('placeholder')));
const isMultiple = computed<boolean>(() => !!select?.multiple);
const handleClear = (e: MouseEvent) => {
e.preventDefault();
e.stopPropagation();
select?.handleClear();
};
const tagDelete = (data: OptionObjectItem) => {
if (data && (data.value || data.value === 0)) {
select?.tagDelete(data);
}
};
const onFocus = (e: FocusEvent) => {
select?.onFocus(e);
};
const onBlur = (e: FocusEvent) => {
select?.onBlur(e);
// 使用setTimeout的原因:点击option会触发onBlur事件,导致先触发onblur事件
setTimeout(()=> {
if(e&&e.target) {
e.target.value = select?.multiple ? '' : displayInputValue.value;
}
if (!isReadOnly.value && select?.debounceQueryFilter) {
select?.debounceQueryFilter('');
}
},300);
};
const queryFilter = (e: Event) => {
e.preventDefault();
e.stopPropagation();
const query = (e.target as HTMLInputElement).value;
if (!isReadOnly.value && select?.debounceQueryFilter) {
select?.debounceQueryFilter(query);
}
};
return {
select,
searchQuery,
selectedData,
isSelectDisable,
isSupportCollapseTags,
isDisabledTooltip,
isReadOnly,
selectionCls,
inputCls,
tagSize,
placeholder,
isMultiple,
displayInputValue,
handleClear,
tagDelete,
onFocus,
onBlur,
queryFilter,
};
}