<script lang="ts" setup>
|
import type { ScrollActionType } from '@jnpf/ui';
|
|
import { computed, nextTick, reactive, ref, toRefs, unref, watch } from 'vue';
|
|
import { useAttrs, useGlobSetting, useMessage } from '@jnpf/hooks';
|
import { AButton, ScrollContainer } from '@jnpf/ui';
|
import { ModalClose, ModalFooter } from '@jnpf/ui/modal';
|
|
import { useDebounceFn } from '@vueuse/core';
|
import { Modal as AModal, Avatar, Checkbox, Form, Input, Radio, Select, TabPane, Tabs, Tag } from 'ant-design-vue';
|
import { cloneDeep, pick } from 'lodash-es';
|
|
import { getUserInfoList } from '#/api/permission/user';
|
import { $t } from '#/locales';
|
|
interface State {
|
activeKey: string;
|
finish: boolean;
|
innerValue: any[] | string | undefined;
|
keyword: string;
|
lastList: any[];
|
loading: boolean;
|
options: any[];
|
listQuery: any;
|
selectedData: any[];
|
selectedIds: any[];
|
visible: boolean;
|
}
|
|
defineOptions({ inheritAttrs: false, name: 'QueryUserSelect' });
|
|
const props = defineProps({
|
allowClear: { default: true, type: Boolean },
|
buttonShowType: { default: '', type: String },
|
buttonType: { default: 'select', type: String as PropType<'' | 'button' | 'select' | undefined> },
|
disabled: { default: false, type: Boolean },
|
multiple: { default: false, type: Boolean },
|
required: { default: false, type: Boolean },
|
placeholder: { default: '请选择', type: String },
|
selectType: { default: 'all', type: String },
|
showSelectedList: { default: true, type: Boolean },
|
modalTitle: { default: '选择用户', type: String },
|
size: String,
|
value: { type: [String, Array] as PropType<string | string[]> },
|
api: { type: Function },
|
query: { type: Object, default: () => ({}) },
|
});
|
const emit = defineEmits(['update:value', 'change', 'labelChange']);
|
const attrs: any = useAttrs({ excludeDefaultKeys: false });
|
const globSetting = useGlobSetting();
|
const apiUrl = ref(globSetting.apiURL);
|
const infiniteBody = ref<Nullable<ScrollActionType>>(null);
|
const state = reactive<State>({
|
activeKey: '1',
|
finish: false,
|
innerValue: undefined,
|
keyword: '',
|
lastList: [],
|
loading: false,
|
options: [],
|
listQuery: {
|
currentPage: 1,
|
keyword: '',
|
pageSize: 20,
|
},
|
selectedData: [],
|
selectedIds: [],
|
visible: false,
|
});
|
const { activeKey, innerValue, lastList, loading, options, listQuery, selectedData, selectedIds, visible } = toRefs(state);
|
const formItemContext = Form.useInjectFormItemContext();
|
const { createMessage } = useMessage();
|
const debounceHandleSearch = useDebounceFn(handleSearch, 300);
|
|
const getSelectBindValue: any = computed(() => ({
|
...pick(props, ['placeholder', 'disabled', 'size', 'allowClear']),
|
class: unref(attrs).class ? `w-full ${unref(attrs).class}` : 'w-full',
|
fieldNames: { label: 'fullName', value: 'id' },
|
mode: props.multiple ? 'multiple' : '',
|
open: false,
|
showArrow: true,
|
showSearch: false,
|
}));
|
|
watch(
|
() => props.value,
|
() => {
|
setValue();
|
},
|
{ immediate: true },
|
);
|
watch(
|
() => state.selectedData,
|
(val) => {
|
state.selectedIds = val.map((o) => o.id);
|
},
|
{ deep: true },
|
);
|
|
function setValue() {
|
if (!props.value || props.value.length === 0) return setNullValue();
|
const ids = props.multiple ? (props.value as any[]) : [props.value];
|
getUserInfoList(ids).then((res) => {
|
setOptions(res.data.list || []);
|
});
|
}
|
function setOptions(data) {
|
if (!props.value || props.value.length === 0) return setNullValue();
|
const selectedList: any[] = data;
|
const innerIds = selectedList.map((o) => o.id);
|
state.innerValue = props.multiple ? innerIds : innerIds[0];
|
state.options = cloneDeep(selectedList);
|
state.selectedData = cloneDeep(selectedList);
|
const labels = selectedData.value.map((o) => o.fullName).join(',');
|
emit('labelChange', labels);
|
}
|
function setNullValue() {
|
state.innerValue = props.multiple ? [] : undefined;
|
state.options = [];
|
state.selectedData = [];
|
}
|
function onChange(_val, option) {
|
state.selectedData = option || [];
|
handleSubmit();
|
}
|
function onTagClose(i) {
|
removeData(i);
|
handleSubmit();
|
}
|
function openSelectModal() {
|
if (props.disabled) return;
|
state.visible = true;
|
state.listQuery.keyword = '';
|
initData();
|
nextTick(() => {
|
bindScroll();
|
});
|
setValue();
|
}
|
function handleCancel() {
|
state.visible = false;
|
}
|
function handleSearch(e) {
|
const value = e.target.value;
|
if (state.loading) return;
|
state.listQuery.keyword = value || '';
|
nextTick(() => {
|
bindScroll();
|
});
|
initData();
|
}
|
function handleNodeClick(data) {
|
const index = state.selectedData.findIndex((o) => o.id === data.id);
|
if (index !== -1) return state.selectedData.splice(index, 1);
|
props.multiple ? state.selectedData.push(data) : (state.selectedData = [data]);
|
}
|
function removeAll() {
|
state.selectedData = [];
|
}
|
function removeData(index: number) {
|
state.selectedData.splice(index, 1);
|
}
|
function handleSubmit() {
|
const ids = state.selectedData.map((o) => o.id);
|
if (props.required && !ids.length) {
|
createMessage.warning('至少选择一个用户');
|
return;
|
}
|
state.options = state.selectedData;
|
state.innerValue = props.multiple ? ids : ids[0];
|
if (props.multiple) {
|
emit('update:value', ids);
|
emit('change', ids, state.options);
|
} else {
|
emit('update:value', ids[0] || '');
|
emit('change', ids[0] || '', state.options[0]);
|
}
|
formItemContext.onFieldChange();
|
handleCancel();
|
}
|
function bindScroll() {
|
const bodyRef = infiniteBody.value;
|
const vBody = bodyRef?.getScrollWrap();
|
vBody?.addEventListener('scroll', () => {
|
if (vBody.scrollTop > 0 && vBody.scrollHeight - vBody.clientHeight - vBody.scrollTop <= 200 && !state.loading && !state.finish) {
|
state.listQuery.currentPage += 1;
|
getLastList();
|
}
|
});
|
}
|
function getLastList() {
|
if (!props.api) return;
|
state.loading = true;
|
const query = { ...state.listQuery, ...props.query };
|
props
|
.api(query)
|
.then((res) => {
|
state.finish = res.data.list.length < state.listQuery.pageSize;
|
state.lastList = [...state.lastList, ...res.data.list];
|
state.loading = false;
|
})
|
.catch(() => {
|
state.loading = false;
|
});
|
}
|
async function initData() {
|
state.listQuery.currentPage = 1;
|
state.finish = false;
|
state.lastList = [];
|
getLastList();
|
}
|
</script>
|
|
<template>
|
<div @click="openSelectModal" v-if="$slots.action">
|
<slot name="action"></slot>
|
</div>
|
<template v-else>
|
<div v-if="buttonType === 'button'" :class="[$attrs.class]" class="select-tag-list">
|
<AButton pre-icon="icon-ym icon-ym-btn-add" :type="buttonShowType" @click="openSelectModal">{{ modalTitle }}</AButton>
|
<div class="tags" v-if="showSelectedList">
|
<Tag v-for="(item, i) in options" :key="item.id" :closable="!disabled" class="!mt-[10px]" @close="onTagClose(i)">{{ item.fullName }}</Tag>
|
</div>
|
</div>
|
<Select v-bind="getSelectBindValue" v-else v-model:value="innerValue" :options="options" @change="onChange" @click="openSelectModal" />
|
</template>
|
<AModal v-model:open="visible" :keyboard="false" :mask-closable="false" title="选择用户" :width="800" centered class="common-select-modal">
|
<template #closeIcon>
|
<ModalClose :can-fullscreen="false" @cancel="handleCancel" />
|
</template>
|
<template #footer>
|
<ModalFooter @cancel="handleCancel" @ok="handleSubmit">
|
<template #insertFooter v-if="multiple">
|
<div class="float-left">
|
<span class="mr-[10px]">{{ $t('component.jnpf.common.selected') }}({{ selectedData.length }})</span>
|
<span class="remove-all-btn" @click="removeAll">{{ $t('component.jnpf.common.clearAll') }}</span>
|
</div>
|
</template>
|
</ModalFooter>
|
</template>
|
<div class="common-select-modal-main">
|
<div class="selected-pane">
|
<Tag v-for="(item, i) in selectedData" :key="item.id" :closable="true" class="selected-pane-tag" @close="removeData(i)">
|
{{ item.fullName }}
|
</Tag>
|
</div>
|
<div class="select-pane">
|
<div class="select-pane-tool">
|
<Tabs v-model:active-key="activeKey" :tab-bar-gutter="20" class="select-pane-tool-tabs">
|
<TabPane key="1" tab="用户" />
|
<template #rightExtra>
|
<Input v-model:value="listQuery.keyword" :placeholder="$t('common.enterKeyword')" allow-clear @change="debounceHandleSearch" />
|
</template>
|
</Tabs>
|
<div class="select-pane-main">
|
<ScrollContainer class="select-pane-list select-pane-item" ref="infiniteBody" v-loading="loading && listQuery.currentPage === 1">
|
<div v-for="(item, i) in lastList" :key="i" class="select-pane-list__item" @click="handleNodeClick(item)">
|
<div class="select-pane-list__item-title">
|
<Avatar :size="26" :src="apiUrl + item.headIcon" class="select-pane-list__item--headIcon" v-if="item.headIcon" />
|
<i :class="item.icon" class="mr-[6px]" v-if="item.icon && !item.headIcon"></i>
|
<span :title="item.fullName">{{ item.fullName }}</span>
|
</div>
|
<div class="select-pane-list__item-action">
|
<Checkbox :checked="selectedIds.includes(item.id)" v-if="multiple" />
|
<Radio :checked="selectedIds.includes(item.id)" v-else />
|
</div>
|
</div>
|
<jnpf-empty v-if="lastList.length === 0" />
|
</ScrollContainer>
|
</div>
|
</div>
|
</div>
|
</div>
|
</AModal>
|
</template>
|