antdv Select dropdownRender Input 不能输入的问题

简言之:外层套div,然后利用Select的open属性。直接上代码:

<template>
	<a-form-item-rest>
	 <div @click="selOpen = !selOpen">
	   <Select
	     v-model:value="xxx"
	     placeholder="请选择"
	     :options="groupCodes"
	     :open="selOpen"
	     :field-names="{ label: 'groupName', value: 'groupCode' }"
	     @change="groupChange"
	   >
	     <template #dropdownRender="{ menuNode: menu }">
	       <div
	         @mouseleave="
	           selOpen = false;
	           newGroupName = '';
	         "
	       >
	         <v-nodes :vnodes="menu" />
	         <Divider style="margin: 4px 0" />
	         <div class="flex items-center justify-between px-4">
	           <a-input v-model:value="newGroupName" class="flex-1" placeholder="请输入" />
	           <div class="cursor-pointer px-8px py-4px" @click="addGroup">
	             <plus-outlined />
	             新增分组
	           </div>
	         </div>
	       </div>
	     </template>
	   </Select>
	 </div>
	</a-form-item-rest>
</template>

// 大概这么个引入的意思,具体看子级当前项目怎么个引入方式
<script lang="ts" setup>
import { Form, Select, Divider } from 'ant-design-vue';
import { PlusOutlined } from '@ant-design/icons-vue';

const AFormItemRest = Form.ItemRest:
const VNodes = (_, { attrs }) => {
  return attrs.vnodes;
}

const selOpen = ref(false);
const groupCodes: any = ref([]);
function groupChange(e, a) {
  setFieldsValue({
    groupCode: e,
    groupName: a.groupName,
  });
  selOpen.value = false;
}
const newGroupName = ref('');
async function addGroup() {
  if (!newGroupName.value) return;
  // await groupSave({ groupName: newGroupName.value } as any);
  // const groupList = await groupListApi();
  // groupCodes.value = groupList;
  selOpen.value = false;
  newGroupName.value = '';
}
</script>

小说明:外层 div click 事件触发select开启,然后 slot 内部 div 利用 mouseleave 属性,模拟出select 的失焦 blur 事件

你可能感兴趣的:(Vue,javascript,前端,vue.js)