gaoluyang
2026-11-02 fa95ef8dd4c23d05bf7bee16927f207cc0793412
src/views/wls/warehouse/components/area-select.vue
@@ -5,7 +5,10 @@
import { Select, SelectOption, Tooltip } from 'ant-design-vue';
import { getWarehouseAreaSimpleList } from '#/api/mes/wm/warehouse/area';
import {
  createWarehouseArea,
  getWarehouseAreaSimpleList,
} from '#/api/mes/wm/warehouse/area';
defineOptions({ name: 'WmWarehouseAreaSelect', inheritAttrs: false });
@@ -31,24 +34,66 @@
  'update:modelValue': [value: number | undefined];
}>();
const CREATE_VALUE = '__CREATE_AREA__';
const attrs = useAttrs();
const allList = ref<MesWmWarehouseAreaApi.WarehouseArea[]>([]);
const selectedItem = ref<MesWmWarehouseAreaApi.WarehouseArea>();
const searchText = ref('');
const selectValue = computed({
  get: () => props.modelValue,
  set: (val) => emit('update:modelValue', val),
});
/** 选中变化 */
function handleChange(val: any) {
/** 输入内容尚未存在于列表时,允许直接新建 */
const canCreate = computed(() => {
  const keyword = searchText.value.trim();
  if (!keyword || !props.locationId) {
    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);
}
/** 前端过滤(name + code) */
/** 输入名称直接新建库位并选中 */
async function handleCreate() {
  const name = searchText.value.trim();
  if (!name || !props.locationId) {
    return;
  }
  const id = await createWarehouseArea({
    allowBatchMixing: true,
    allowItemMixing: true,
    code: `KW${Date.now()}`,
    frozen: false,
    locationId: props.locationId,
    name,
    status: 0,
  });
  allList.value = await getWarehouseAreaSimpleList(props.locationId);
  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;
@@ -75,6 +120,7 @@
/** 库区变更时清空已选库位并重新加载列表 */
watchEffect(async () => {
  searchText.value = '';
  if (!props.locationId) {
    allList.value = [];
    return;
@@ -113,10 +159,14 @@
      :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>