zhangwencui
7 天以前 7619c19a67c2ac824f803090bab753fc5ea14408
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
<script lang="ts" setup>
import type { MesWmWarehouseLocationApi } from '#/api/mes/wm/warehouse/location';
 
import { computed, ref, useAttrs, watch, watchEffect } from 'vue';
 
import { Select, SelectOption, Tag, Tooltip } from 'ant-design-vue';
 
import { getWarehouseLocationSimpleList } from '#/api/mes/wm/warehouse/location';
 
defineOptions({ name: 'WmWarehouseLocationSelect', inheritAttrs: false });
 
const props = withDefaults(
  defineProps<{
    allowClear?: boolean;
    disabled?: boolean;
    modelValue?: number;
    placeholder?: string;
    warehouseId?: number;
  }>(),
  {
    allowClear: true,
    disabled: false,
    modelValue: undefined,
    placeholder: '请选择库区',
    warehouseId: undefined,
  },
);
 
const emit = defineEmits<{
  change: [item: MesWmWarehouseLocationApi.WarehouseLocation | undefined];
  'update:modelValue': [value: number | undefined];
}>();
 
const attrs = useAttrs();
const allList = ref<MesWmWarehouseLocationApi.WarehouseLocation[]>([]);
const selectedItem = ref<MesWmWarehouseLocationApi.WarehouseLocation>();
 
const selectValue = computed({
  get: () => props.modelValue,
  set: (val) => emit('update:modelValue', val),
});
 
/** 选中变化 */
function handleChange(val: any) {
  const item = allList.value.find((o) => o.id === val);
  selectedItem.value = item;
  emit('change', item);
}
 
/** 前端过滤(name + code) */
function filterOption(input: string, option: any) {
  const item = allList.value.find((o) => o.id === option.value);
  if (!item) {
    return false;
  }
  const keyword = input.toLowerCase();
  return (
    !!item.name?.toLowerCase().includes(keyword) ||
    !!item.code?.toLowerCase().includes(keyword)
  );
}
 
watch(
  () => props.modelValue,
  (val) => {
    if (val === null) {
      selectedItem.value = undefined;
      return;
    }
    if (selectedItem.value?.id !== val && allList.value.length > 0) {
      selectedItem.value = allList.value.find((o) => o.id === val);
    }
  },
);
 
/** 仓库变更或初始化时重新加载库区列表 */
watchEffect(async () => {
  allList.value = await getWarehouseLocationSimpleList(props.warehouseId);
  if (props.modelValue !== null) {
    selectedItem.value = allList.value.find((o) => o.id === props.modelValue);
  }
});
</script>
 
<template>
  <Tooltip :mouse-enter-delay="0.5" :open="selectedItem ? undefined : false">
    <template #title>
      <div v-if="selectedItem" class="leading-6">
        <div>编码:{{ selectedItem.code || '-' }}</div>
        <div>名称:{{ selectedItem.name || '-' }}</div>
        <div>所属仓库:{{ selectedItem.warehouseName || '-' }}</div>
      </div>
    </template>
    <Select
      v-bind="attrs"
      v-model:value="selectValue"
      :allow-clear="allowClear"
      class="!w-full"
      :disabled="disabled"
      :filter-option="filterOption"
      :placeholder="placeholder"
      show-search
      @change="handleChange"
    >
      <SelectOption v-for="item in allList" :key="item.id" :value="item.id">
        <div class="flex items-center gap-2">
          <span>{{ item.name }}</span>
          <Tag v-if="item.code" color="blue">编号: {{ item.code }}</Tag>
        </div>
      </SelectOption>
    </Select>
  </Tooltip>
</template>