<script lang="ts" setup>
|
import { computed, onMounted, reactive, toRefs, unref, watch } from 'vue';
|
|
import { useAttrs, useMessage } from '@jnpf/hooks';
|
import { AButton, ScrollContainer } from '@jnpf/ui';
|
import { ModalClose, ModalFooter } from '@jnpf/ui/modal';
|
|
import { $t } from '@vben/locales';
|
|
import { globalShareState } from '@vben-core/shared/global-state';
|
|
import { useDebounceFn } from '@vueuse/core';
|
import { Modal as AModal, Checkbox, Form, Input, Radio, Select, TabPane, Tabs, Tag } from 'ant-design-vue';
|
import { cloneDeep, pick } from 'lodash-es';
|
|
import { roleSelectProps } from './props';
|
|
interface State {
|
activeKey: string;
|
allList: any[];
|
cacheData: any[];
|
innerValue: any[] | string | undefined;
|
keyword: string;
|
loading: boolean;
|
options: any[];
|
selectedData: any[];
|
selectedIds: any[];
|
treeData: any[];
|
visible: boolean;
|
}
|
|
defineOptions({ inheritAttrs: false, name: 'JnpfRoleSelect' });
|
|
const props = defineProps(roleSelectProps);
|
const emit = defineEmits(['update:value', 'change', 'labelChange']);
|
const attrs: any = useAttrs({ excludeDefaultKeys: false });
|
const { getRoleByCondition } = globalShareState.getApi();
|
const { useOrganizeStore } = globalShareState.getStores();
|
const organizeStore = useOrganizeStore();
|
const formItemContext = Form.useInjectFormItemContext();
|
const { createMessage } = useMessage();
|
const state = reactive<State>({
|
activeKey: '1',
|
allList: [],
|
cacheData: [],
|
innerValue: '',
|
keyword: '',
|
loading: false,
|
options: [],
|
selectedData: [],
|
selectedIds: [],
|
treeData: [],
|
visible: false,
|
});
|
const { activeKey, innerValue, keyword, loading, options, selectedData, selectedIds, treeData, visible } = toRefs(state);
|
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,
|
style: Reflect.has(unref(attrs), 'style') ? unref(attrs).style : {},
|
}));
|
const getTitle = computed(() => {
|
if (props.roleType === 'organize') return '组织角色';
|
if (props.roleType === 'position') return '岗位角色';
|
return '用户角色';
|
});
|
|
watch(
|
() => unref(props.value),
|
() => {
|
setValue();
|
},
|
{ immediate: true },
|
);
|
watch(
|
() => state.allList,
|
() => {
|
setValue();
|
},
|
{ deep: true },
|
);
|
watch(
|
() => state.selectedData,
|
(val) => {
|
state.selectedIds = val.map((o) => o.id);
|
},
|
{ deep: true },
|
);
|
|
function setValue() {
|
if (!props.value || props.value.length === 0) {
|
state.innerValue = props.multiple ? [] : undefined;
|
state.options = [];
|
state.selectedData = [];
|
emit('labelChange', '');
|
return;
|
}
|
const ids = props.multiple ? (props.value as any[]) : [props.value];
|
const selectedList: any[] = [];
|
for (const id of ids) {
|
inner: for (let j = 0; j < state.allList.length; j++) {
|
if (id === state.allList[j].id) {
|
selectedList.push(state.allList[j]);
|
break inner;
|
}
|
}
|
}
|
const innerIds = selectedList.map((o) => o.id);
|
state.innerValue = props.multiple ? innerIds : innerIds[0];
|
state.options = cloneDeep(selectedList);
|
state.selectedData = cloneDeep(selectedList);
|
const labels = state.selectedData.map((o) => o.fullName).join(',');
|
emit('labelChange', labels);
|
}
|
function onChange(_val, option) {
|
state.selectedData = option ?? [];
|
handleSubmit();
|
}
|
function onTagClose(i) {
|
removeData(i);
|
handleSubmit();
|
}
|
function openSelectModal() {
|
if (props.disabled) return;
|
state.visible = true;
|
state.keyword = '';
|
state.treeData = [];
|
initData();
|
setValue();
|
}
|
function handleCancel() {
|
state.visible = false;
|
}
|
function handleSearch(e) {
|
const value = e.target.value;
|
state.treeData = state.cacheData.filter((o) => o.fullName.includes(value));
|
}
|
function handleSelect(data) {
|
if (data?.disabled) return;
|
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();
|
}
|
async function initData() {
|
state.loading = true;
|
state.allList = await organizeStore.getRoleList({ type: props.roleType, isSystem: props.hasSys ? 2 : 0 });
|
if (props.selectType === 'all') {
|
state.treeData = state.allList;
|
state.cacheData = state.allList;
|
state.loading = false;
|
} else {
|
if (!props.ableIds?.length) {
|
state.treeData = [];
|
state.cacheData = [];
|
state.loading = false;
|
return;
|
}
|
const query = { ids: props.ableIds };
|
getRoleByCondition(query).then((res) => {
|
state.treeData = res.data || [];
|
state.cacheData = res.data || [];
|
state.loading = false;
|
});
|
}
|
}
|
|
onMounted(async () => {
|
state.allList = await organizeStore.getRoleList({ type: props.roleType, isSystem: props.hasSys ? 2 : 0 });
|
});
|
</script>
|
|
<template>
|
<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" />
|
<AModal v-model:open="visible" :keyboard="false" :mask-closable="false" :title="`选择${getTitle}`" :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="getTitle" />
|
<template #rightExtra>
|
<Input v-model:value="keyword" :placeholder="$t('common.enterKeyword')" allow-clear @change="debounceHandleSearch" />
|
</template>
|
</Tabs>
|
<div class="select-pane-main">
|
<ScrollContainer class="select-pane-list" v-loading="loading">
|
<div v-for="(item, i) in treeData" :key="i" class="select-pane-list__item" @click="handleSelect(item)">
|
<div class="select-pane-list__item-title">
|
<i :class="item.icon" v-if="item.icon" class="mr-[6px]"></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="treeData.length === 0" />
|
</ScrollContainer>
|
</div>
|
</div>
|
</div>
|
</div>
|
</AModal>
|
</template>
|