gaoluyang
2026-11-02 2fe330a44f9e8bdd8a1afd6124c57851f2a5b7ea
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
<script lang="ts" setup>
import type { MesWmWarehouseLocationApi } from '#/api/mes/wm/warehouse/location';
 
import { computed, ref, useAttrs, watch, watchEffect } from 'vue';
 
import { Select, SelectOption, Tooltip } from 'ant-design-vue';
 
import {
  createWarehouseLocation,
  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 CREATE_VALUE = '__CREATE_LOCATION__';
 
const attrs = useAttrs();
const allList = ref<MesWmWarehouseLocationApi.WarehouseLocation[]>([]);
const selectedItem = ref<MesWmWarehouseLocationApi.WarehouseLocation>();
const searchText = ref('');
 
const selectValue = computed({
  get: () => props.modelValue,
  set: (val) => emit('update:modelValue', val),
});
 
/** 输入内容尚未存在于列表时,允许直接新建 */
const canCreate = computed(() => {
  const keyword = searchText.value.trim();
  if (!keyword || !props.warehouseId) {
    return false;
  }
  return !allList.value.some((o) => o.name === keyword);
});
 
/** 选中变化;选中“新增”项时按输入名称直接建档 */
async function handleChange(val: any) {
  if (val === CREATE_VALUE) {
    await handleCreate();
    return;
  }
  const item = allList.value.find((o) => o.id === val);
  selectedItem.value = item;
  emit('change', item);
}
 
/** 输入名称直接新建库区并选中 */
async function handleCreate() {
  const name = searchText.value.trim();
  if (!name || !props.warehouseId) {
    return;
  }
  const id = await createWarehouseLocation({
    code: `ZD${Date.now()}`,
    frozen: false,
    name,
    warehouseId: props.warehouseId,
  });
  allList.value = await getWarehouseLocationSimpleList(props.warehouseId);
  const item = allList.value.find((o) => o.id === id);
  selectedItem.value = item;
  searchText.value = '';
  emit('update:modelValue', id);
  emit('change', item);
}
 
/** 前端过滤(name + code),始终保留“新增”项 */
function filterOption(input: string, option: any) {
  if (option.value === CREATE_VALUE) {
    return true;
  }
  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 () => {
  searchText.value = '';
  if (!props.warehouseId) {
    allList.value = [];
    return;
  }
  allList.value = await getWarehouseLocationSimpleList(props.warehouseId);
  // 仓库变更时清空已选的库区
  if (props.modelValue !== null && props.modelValue !== undefined) {
    const stillExists = allList.value.find((o) => o.id === props.modelValue);
    if (!stillExists) {
      selectedItem.value = undefined;
      emit('update:modelValue', undefined);
      emit('change', undefined);
    }
  }
});
</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"
      :popup-match-select-width="false"
      show-search
      @change="handleChange"
      @search="searchText = $event"
    >
      <SelectOption v-for="item in allList" :key="item.id" :value="item.id">
        {{ item.name }}
      </SelectOption>
      <SelectOption v-if="canCreate" :value="CREATE_VALUE">
        新增库区“{{ searchText.trim() }}”
      </SelectOption>
    </Select>
  </Tooltip>
</template>