ny
22 小时以前 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
<script lang="ts" setup>
import type { FieldNames } from './props';
 
import { computed, ref, unref, watch } from 'vue';
 
import { useAttrs } from '@jnpf/hooks';
 
import { selectProps } from './props';
 
defineOptions({ inheritAttrs: false, name: 'JnpfSelect' });
const props = defineProps(selectProps);
const emit = defineEmits(['update:value', 'change']);
defineExpose({ getSelectRef });
const attrs = useAttrs({ excludeDefaultKeys: false });
const innerValue = ref('');
const selectRef = ref(null);
 
const getFieldNames = computed((): Required<FieldNames> => {
  const { fieldNames } = props;
  return {
    disabled: 'disabled',
    label: 'fullName',
    options: '',
    value: 'id',
    ...fieldNames,
  };
});
const getOptionFilterProp = computed(() => props.optionFilterProp || unref(getFieldNames).label);
const getBindValue = computed(() => ({
  ...unref(attrs),
  ...props,
  fieldNames: unref(getFieldNames),
  getPopupContainer: () => document.body,
  mode: props.multiple ? 'multiple' : '',
  optionFilterProp: unref(getOptionFilterProp),
  showArrow: Reflect.has(unref(attrs), 'showArrow') ? (unref(attrs) as { showArrow: boolean }).showArrow : true,
}));
 
watch(
  () => unref(props.value),
  (val) => {
    setValue(val);
  },
  { immediate: true },
);
 
function setValue(value) {
  innerValue.value = value || value === 0 ? value : undefined;
}
function onChange(val, option) {
  emit('update:value', val);
  emit('change', val, option);
}
function getSelectRef() {
  const select = unref(selectRef);
  if (!select) {
    throw new Error('select is null!');
  }
  return select;
}
</script>
 
<template>
  <a-select v-bind="getBindValue" ref="selectRef" v-model:value="innerValue" @change="onChange">
    <template v-for="item in Object.keys($slots)" #[item]="data"><slot :name="item" v-bind="data || {}"></slot></template>
  </a-select>
</template>