forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.tsx
241 lines (223 loc) · 7.54 KB
/
index.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
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
import type { App, PropType, Plugin, ExtractPropTypes } from 'vue';
import { computed, defineComponent, ref } from 'vue';
import classNames from '../_util/classNames';
import type { BaseSelectRef } from '../vc-select';
import RcSelect, { selectProps as vcSelectProps, Option, OptGroup } from '../vc-select';
import type { BaseOptionType, DefaultOptionType } from '../vc-select/Select';
import type { OptionProps } from '../vc-select/Option';
import getIcons from './utils/iconUtil';
import PropTypes from '../_util/vue-types';
import useConfigInject from '../_util/hooks/useConfigInject';
import omit from '../_util/omit';
import { useInjectFormItemContext } from '../form/FormItemContext';
import { getTransitionName } from '../_util/transition';
import type { SizeType } from '../config-provider';
import { initDefaultProps } from '../_util/props-util';
type RawValue = string | number;
export type OptionType = typeof Option;
export type { OptionProps, BaseSelectRef as RefSelectProps, BaseOptionType, DefaultOptionType };
export interface LabeledValue {
key?: string;
value: RawValue;
label?: any;
}
export type SelectValue = RawValue | RawValue[] | LabeledValue | LabeledValue[] | undefined;
export const selectProps = () => ({
...omit(vcSelectProps<SelectValue>(), [
'inputIcon',
'mode',
'getInputElement',
'getRawInputElement',
'backfill',
]),
value: {
type: [Array, Object, String, Number] as PropType<SelectValue>,
},
defaultValue: {
type: [Array, Object, String, Number] as PropType<SelectValue>,
},
notFoundContent: PropTypes.any,
suffixIcon: PropTypes.any,
itemIcon: PropTypes.any,
size: String as PropType<SizeType>,
mode: String as PropType<'multiple' | 'tags' | 'SECRET_COMBOBOX_MODE_DO_NOT_USE'>,
bordered: { type: Boolean, default: true },
transitionName: String,
choiceTransitionName: { type: String, default: '' },
'onUpdate:value': Function as PropType<(val: SelectValue) => void>,
});
export type SelectProps = Partial<ExtractPropTypes<ReturnType<typeof selectProps>>>;
const SECRET_COMBOBOX_MODE_DO_NOT_USE = 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
const Select = defineComponent({
name: 'ASelect',
Option,
OptGroup,
inheritAttrs: false,
props: initDefaultProps(selectProps(), {
listHeight: 256,
listItemHeight: 24,
}),
SECRET_COMBOBOX_MODE_DO_NOT_USE,
// emits: ['change', 'update:value', 'blur'],
slots: [
'notFoundContent',
'suffixIcon',
'itemIcon',
'removeIcon',
'clearIcon',
'dropdownRender',
'option',
'placeholder',
'tagRender',
'maxTagPlaceholder',
'optionLabel', // donot use, maybe remove it
],
setup(props, { attrs, emit, slots, expose }) {
const selectRef = ref<BaseSelectRef>();
const formItemContext = useInjectFormItemContext();
const focus = () => {
selectRef.value?.focus();
};
const blur = () => {
selectRef.value?.blur();
};
const scrollTo: BaseSelectRef['scrollTo'] = arg => {
selectRef.value?.scrollTo(arg);
};
const mode = computed(() => {
const { mode } = props;
if ((mode as any) === 'combobox') {
return undefined;
}
if (mode === SECRET_COMBOBOX_MODE_DO_NOT_USE) {
return 'combobox';
}
return mode;
});
const { prefixCls, direction, configProvider, size, getPrefixCls } = useConfigInject(
'select',
props,
);
const rootPrefixCls = computed(() => getPrefixCls());
const transitionName = computed(() =>
getTransitionName(rootPrefixCls.value, 'slide-up', props.transitionName),
);
const mergedClassName = computed(() =>
classNames({
[`${prefixCls.value}-lg`]: size.value === 'large',
[`${prefixCls.value}-sm`]: size.value === 'small',
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
[`${prefixCls.value}-borderless`]: !props.bordered,
}),
);
const triggerChange: SelectProps['onChange'] = (...args) => {
emit('update:value', args[0]);
emit('change', ...args);
formItemContext.onFieldChange();
};
const handleBlur: SelectProps['onBlur'] = e => {
emit('blur', e);
formItemContext.onFieldBlur();
};
expose({
blur,
focus,
scrollTo,
});
const isMultiple = computed(() => mode.value === 'multiple' || mode.value === 'tags');
return () => {
const {
notFoundContent,
listHeight = 256,
listItemHeight = 24,
getPopupContainer,
dropdownClassName,
virtual,
dropdownMatchSelectWidth,
id = formItemContext.id.value,
placeholder = slots.placeholder?.(),
} = props;
const { renderEmpty, getPopupContainer: getContextPopupContainer } = configProvider;
// ===================== Empty =====================
let mergedNotFound: any;
if (notFoundContent !== undefined) {
mergedNotFound = notFoundContent;
} else if (slots.notFoundContent) {
mergedNotFound = slots.notFoundContent();
} else if (mode.value === 'combobox') {
mergedNotFound = null;
} else {
mergedNotFound = renderEmpty('Select') as any;
}
// ===================== Icons =====================
const { suffixIcon, itemIcon, removeIcon, clearIcon } = getIcons(
{
...props,
multiple: isMultiple.value,
prefixCls: prefixCls.value,
},
slots,
);
const selectProps = omit(props, [
'prefixCls',
'suffixIcon',
'itemIcon',
'removeIcon',
'clearIcon',
'size',
'bordered',
]);
const rcSelectRtlDropDownClassName = classNames(dropdownClassName, {
[`${prefixCls.value}-dropdown-${direction.value}`]: direction.value === 'rtl',
});
return (
<RcSelect
ref={selectRef}
virtual={virtual}
dropdownMatchSelectWidth={dropdownMatchSelectWidth}
{...selectProps}
{...attrs}
placeholder={placeholder}
listHeight={listHeight}
listItemHeight={listItemHeight}
mode={mode.value}
prefixCls={prefixCls.value}
direction={direction.value}
inputIcon={suffixIcon}
menuItemSelectedIcon={itemIcon}
removeIcon={removeIcon}
clearIcon={clearIcon}
notFoundContent={mergedNotFound}
class={[mergedClassName.value, attrs.class]}
getPopupContainer={getPopupContainer || getContextPopupContainer}
dropdownClassName={rcSelectRtlDropDownClassName}
onChange={triggerChange}
onBlur={handleBlur}
id={id}
dropdownRender={selectProps.dropdownRender || slots.dropdownRender}
v-slots={{ option: slots.option }}
transitionName={transitionName.value}
children={slots.default?.()}
tagRender={props.tagRender || slots.tagRender}
optionLabelRender={slots.optionLabel}
maxTagPlaceholder={props.maxTagPlaceholder || slots.maxTagPlaceholder}
></RcSelect>
);
};
},
});
/* istanbul ignore next */
Select.install = function (app: App) {
app.component(Select.name, Select);
app.component(Select.Option.displayName, Select.Option);
app.component(Select.OptGroup.displayName, Select.OptGroup);
return app;
};
export const SelectOption = Select.Option;
export const SelectOptGroup = Select.OptGroup;
export default Select as typeof Select &
Plugin & {
readonly Option: typeof Option;
readonly OptGroup: typeof OptGroup;
readonly SECRET_COMBOBOX_MODE_DO_NOT_USE: 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
};