Files
zyt/admin/src/components/medicine-name-select/index.vue
T
2026-10-22 11:12:16 +08:00

142 lines
3.7 KiB
Vue
Executable File

<template>
<span v-if="disabled" class="medicine-name-readonly">{{ modelValue || '—' }}</span>
<el-select
v-else
:model-value="selectedMedicineId"
class="medicine-name-select w-full"
filterable
remote
reserve-keyword
placeholder="搜索药材名称或拼音首字母"
:remote-method="remoteMethod"
:loading="loading"
clearable
@visible-change="onVisibleChange"
@update:model-value="onUpdate"
>
<el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
<div class="medicine-name-option">
<span>{{ item.name }}</span>
<span class="medicine-name-option__meta">
{{ item.id > 0 ? [item.supplier, item.unit, `ID ${item.id}`].filter(Boolean).join(' · ') : '历史名称,请重新选择' }}
</span>
</div>
</el-option>
</el-select>
</template>
<script setup lang="ts">
import { medicineLists } from '@/api/medicine'
const props = withDefaults(
defineProps<{
modelValue: string
medicineId?: number | null
disabled?: boolean
}>(),
{ disabled: false }
)
const emit = defineEmits<{
'update:modelValue': [value: string]
'update:medicineId': [value: number | undefined]
}>()
const loading = ref(false)
type MedicineOption = { id: number; name: string; supplier?: string; unit?: string }
const options = ref<MedicineOption[]>([])
const selectedMedicineId = computed<number | ''>(() => {
const id = Number(props.medicineId)
if (Number.isInteger(id) && id > 0) {
return id
}
return (props.modelValue || '').trim() ? 0 : ''
})
function ensureCurrentInOptions() {
const v = (props.modelValue || '').trim()
if (!v) {
return
}
const id = Number(props.medicineId)
const currentId = Number.isInteger(id) && id > 0 ? id : 0
if (!options.value.some((o) => o.id === currentId)) {
options.value = [{ id: currentId, name: v }, ...options.value]
}
}
const remoteMethod = async (query: string) => {
loading.value = true
try {
const q = (query || '').trim()
const res = await medicineLists({
name: q || undefined,
page_no: 1,
page_size: 100,
status: 1
})
options.value = (res.lists || []) as MedicineOption[]
ensureCurrentInOptions()
} catch (e) {
console.error(e)
options.value = []
} finally {
loading.value = false
}
}
const onVisibleChange = (open: boolean) => {
if (open && options.value.length === 0) {
remoteMethod('')
}
}
const onUpdate = (val: number | string) => {
const id = Number(val)
const selected = id > 0 ? options.value.find((item) => item.id === id) : undefined
emit('update:modelValue', selected?.name || '')
emit('update:medicineId', selected?.id)
}
watch(
() => [props.modelValue, props.medicineId],
() => {
if (!(props.modelValue || '').trim() && Number(props.medicineId) > 0) {
emit('update:medicineId', undefined)
}
ensureCurrentInOptions()
},
{ immediate: true }
)
</script>
<style scoped lang="scss">
.medicine-name-readonly {
color: var(--el-text-color-regular);
}
.medicine-name-select.w-full {
width: 100%;
}
.medicine-name-option {
display: grid;
grid-template-columns: minmax(0, 1fr) minmax(0, 60%);
align-items: center;
gap: 16px;
> span {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
.medicine-name-option__meta {
color: var(--el-text-color-secondary);
font-size: 12px;
text-align: right;
}
</style>