<script lang="ts" setup>
|
import type { FormInstance } from 'ant-design-vue';
|
|
import type { CSSProperties } from 'vue';
|
|
import { computed, inject, nextTick, onMounted, reactive, ref, toRefs, unref, watch } from 'vue';
|
|
import { useAttrs } from '@jnpf/hooks';
|
import { ModalClose } from '@jnpf/ui/modal';
|
|
import { $t } from '@vben/locales';
|
|
import { globalShareState } from '@vben-core/shared/global-state';
|
|
import { RedoOutlined } from '@ant-design/icons-vue';
|
import { Drawer as ADrawer, Modal as AModal, Form } from 'ant-design-vue';
|
import { cloneDeep, pick } from 'lodash-es';
|
|
import ExtraRelationInfo from '../../relationForm/src/ExtraRelationInfo.vue';
|
import { popupSelectProps } from './props';
|
|
interface State {
|
cacheSelectedRowKeys: any[];
|
cacheSelectedRows: any[];
|
extraData: any;
|
list: any[];
|
listQuery: any;
|
loading: boolean;
|
selectedRowKeys: any[];
|
selectedRows: any[];
|
total: number;
|
}
|
|
defineOptions({ inheritAttrs: false, name: 'JnpfPopupSelect' });
|
const props = defineProps(popupSelectProps);
|
const emit = defineEmits(['update:value', 'change']);
|
const attrs: any = useAttrs({ excludeDefaultKeys: false });
|
const formItemContext = Form.useInjectFormItemContext();
|
const { getDataInterfaceDataInfoByIds, getDataInterfaceDataSelect } = globalShareState.getApi();
|
const { useGeneratorStore } = globalShareState.getStores();
|
const generatorStore = useGeneratorStore();
|
const emitter: any = inject('emitter');
|
const drawerPrefixCls = 'jnpf-basic-drawer';
|
const formPrefixCls = 'jnpf-basic-form';
|
const tablePrefixCls = 'jnpf-basic-table';
|
const drawerFooterPrefixCls = 'jnpf-basic-drawer-footer';
|
const innerValue = ref<any[] | string | undefined>(undefined);
|
const selectRow = ref<any>(null);
|
const visible = ref(false);
|
const options = ref<any[]>([]);
|
const formElRef = ref<FormInstance>();
|
const tableElRef = ref<any>(null);
|
const indexColumn = {
|
align: 'center',
|
customRender: ({ index }) => index + 1,
|
dataIndex: 'index',
|
key: 'index',
|
title: $t('component.table.index'),
|
width: 50,
|
};
|
const state = reactive<State>({
|
cacheSelectedRowKeys: [],
|
cacheSelectedRows: [],
|
extraData: {},
|
loading: false,
|
selectedRowKeys: [],
|
selectedRows: [],
|
total: 0,
|
list: [],
|
listQuery: {
|
currentPage: 1,
|
keyword: '',
|
pageSize: 20,
|
},
|
});
|
const { extraData, list, listQuery } = toRefs(state);
|
|
const getFormClass = computed(() => {
|
return [formPrefixCls, `${formPrefixCls}--compact`, 'search-form'];
|
});
|
const getDrawerFooterStyle = computed((): CSSProperties => {
|
const heightStr = `60px`;
|
return {
|
height: heightStr,
|
lineHeight: `calc(${heightStr} - 1px)`,
|
};
|
});
|
const getColumns = computed<any[]>(() => {
|
const columns = (props.columnOptions as any).map((o) => ({ dataIndex: o.value, ellipsis: true, title: o.label }));
|
return [indexColumn, ...columns];
|
});
|
const searchInfo = computed(() => {
|
const paramList = getParamList();
|
const info: any = {
|
columnOptions: (props.columnOptions as any).map((o) => o.value).join(','),
|
interfaceId: props.interfaceId,
|
paramList,
|
propsValue: props.propsValue,
|
relationField: props.relationField,
|
};
|
return info;
|
});
|
const getPagination = computed<any>(() => {
|
if (!props.hasPage) return false;
|
return {
|
current: state.listQuery.currentPage,
|
defaultPageSize: state.listQuery.pageSize,
|
pageSize: state.listQuery.pageSize,
|
pageSizeOptions: ['20', '50', '80', '100'],
|
showQuickJumper: true,
|
showSizeChanger: true,
|
showTotal: (total) => $t('component.table.total', { total }),
|
size: 'small',
|
total: state.total,
|
};
|
});
|
const getRowSelection = computed<any>(() => ({
|
onChange: setSelectedRowKeys,
|
selectedRowKeys: state.selectedRowKeys,
|
type: props.multiple ? 'checkbox' : 'radio',
|
}));
|
const getScrollY = computed(() => {
|
if (props.popupType === 'popover') return 250;
|
let height = props.popupType === 'drawer' ? window.innerHeight - 120 - 52 - 38 : window.innerHeight * 0.7 - 52 - 38;
|
if (props.hasPage) height -= 44;
|
return height;
|
});
|
const getTableBindValues = computed(() => {
|
return {
|
class: unref(tablePrefixCls),
|
columns: unref(getColumns),
|
loading: state.loading,
|
pagination: unref(getPagination),
|
rowKey: props.propsValue,
|
rowSelection: unref(getRowSelection),
|
customRow,
|
scroll: {
|
y: unref(getScrollY),
|
},
|
size: 'small',
|
};
|
});
|
const getSelectBindValue = computed(() => {
|
const className = unref(attrs).class ? `w-full ${unref(attrs).class}` : 'w-full';
|
return {
|
...pick(props, ['size', 'disabled']),
|
allowClear: props.disabled ? false : props.allowClear,
|
class: className,
|
fieldNames: { label: unref(props).relationField, value: unref(props).propsValue },
|
mode: props.multiple ? 'multiple' : '',
|
open: false,
|
placeholder: unref(props).placeholder,
|
showArrow: true,
|
showSearch: false,
|
style: Reflect.has(unref(attrs), 'style') ? unref(attrs).style : {},
|
};
|
});
|
|
watch(
|
() => unref(props.value),
|
(val) => {
|
setValue(val);
|
},
|
{ immediate: true },
|
);
|
|
function customRow(record: Recordable) {
|
return {
|
onClick: (e: Event) => {
|
e?.stopPropagation();
|
const tr: HTMLElement = (e as MouseEvent).composedPath?.().find((dom: any) => dom.tagName === 'TR') as HTMLElement;
|
if (!tr) return;
|
const isCheckbox = unref(getTableBindValues).rowSelection.type === 'checkbox';
|
const key = record[unref(getTableBindValues).rowKey];
|
if (isCheckbox) {
|
// 找到Checkbox,检查是否为disabled
|
const checkBox = tr.querySelector('input[type=checkbox]');
|
if (!checkBox || checkBox.hasAttribute('disabled')) return;
|
if (state.selectedRowKeys.includes(key)) {
|
const keyIndex = state.selectedRowKeys.indexOf(key);
|
state.selectedRowKeys.splice(keyIndex, 1);
|
state.selectedRows = state.selectedRows.filter((o) => o[unref(getTableBindValues).rowKey] !== key);
|
} else {
|
state.selectedRowKeys.push(key);
|
state.selectedRows.push(record);
|
}
|
} else {
|
// 找到radio,检查是否为disabled
|
const radio = tr.querySelector('input[type=radio]');
|
if (!radio || radio.hasAttribute('disabled')) return;
|
state.selectedRowKeys = [key];
|
state.selectedRows = [record];
|
}
|
},
|
};
|
}
|
function setValue(value) {
|
const relationData = generatorStore.getRelationData;
|
if ((props.multiple && (!props.value || (props.value as any[]).length === 0)) || (!props.multiple && !props.value && props.value !== 0)) {
|
setNullValue();
|
return;
|
}
|
if (!props.interfaceId) return;
|
const ids: any[] = props.multiple ? (props.value as any[]) : [props.value];
|
if (!Array.isArray(ids)) return;
|
if (ids.length === 0) return;
|
const paramList = getParamList();
|
const query = {
|
ids,
|
interfaceId: props.interfaceId,
|
paramList,
|
propsValue: props.propsValue,
|
relationField: props.relationField,
|
};
|
getDataInterfaceDataInfoByIds(props.interfaceId, query).then((res) => {
|
if ((props.multiple && (!props.value || (props.value as any[]).length === 0)) || (!props.multiple && !props.value && props.value !== 0)) {
|
setNullValue();
|
return;
|
}
|
const data = res.data && res.data.length > 0 ? res.data[0] : {};
|
state.extraData = data;
|
innerValue.value = value;
|
options.value = res.data;
|
selectRow.value = res.data;
|
state.selectedRowKeys = ids;
|
state.selectedRows = res.data;
|
if (!props.field || props.multiple) return;
|
relationData[props.field] = data;
|
generatorStore.setRelationData(relationData);
|
emitter.emit('setRelationData', { jnpfRelationField: props.field, ...data });
|
});
|
}
|
function setNullValue() {
|
const relationData = generatorStore.getRelationData;
|
state.extraData = {};
|
innerValue.value = props.multiple ? [] : undefined;
|
options.value = [];
|
selectRow.value = null;
|
state.selectedRowKeys = [];
|
state.selectedRows = [];
|
if (!props.field || props.multiple) return;
|
relationData[props.field] = {};
|
generatorStore.setRelationData(relationData);
|
emitter.emit('setRelationData', { jnpfRelationField: props.field });
|
}
|
function onChange(val, option) {
|
if (!val || val.length === 0) {
|
options.value = [];
|
emit('update:value', props.multiple ? [] : '');
|
emit('change', '', props.multiple ? [] : {});
|
state.selectedRowKeys = [];
|
state.selectedRows = [];
|
} else {
|
options.value = option;
|
emit('update:value', val);
|
emit('change', '', props.multiple ? options.value : options.value[0]);
|
}
|
const value = props.multiple ? val : [val];
|
state.selectedRowKeys = state.selectedRowKeys.filter((o) => value.some((l) => l == o));
|
state.selectedRows = state.selectedRows.filter((o) => value.some((l) => l == o[props.propsValue]));
|
formItemContext.onFieldChange();
|
}
|
function getForm() {
|
const form = unref(formElRef);
|
if (!form) {
|
throw new Error('form is null!');
|
}
|
return form;
|
}
|
async function openSelectModal() {
|
if (props.disabled) return;
|
visible.value = true;
|
setTimeout(() => {
|
nextTick(() => {
|
handleReset();
|
const tableEl = tableElRef.value?.$el;
|
const bodyEl = tableEl.querySelector('.ant-table-body');
|
bodyEl!.style.height = `${unref(getScrollY)}px`;
|
if ((!props.multiple && innerValue.value) || (props.multiple && innerValue.value?.length)) {
|
state.selectedRowKeys = props.multiple ? (innerValue.value as any[]) : [innerValue.value];
|
} else {
|
state.selectedRowKeys = [];
|
state.selectedRows = [];
|
}
|
state.cacheSelectedRowKeys = cloneDeep(state.selectedRowKeys);
|
state.cacheSelectedRows = cloneDeep(state.selectedRows);
|
});
|
}, 50);
|
}
|
function handleCancel() {
|
visible.value = false;
|
state.selectedRowKeys = cloneDeep(state.cacheSelectedRowKeys);
|
state.selectedRows = cloneDeep(state.cacheSelectedRows);
|
}
|
function handleSubmit() {
|
if (props.multiple) {
|
updateSelectRow();
|
options.value = selectRow.value;
|
innerValue.value = unref(selectRow).map((o) => o[props.propsValue]);
|
} else {
|
if (state.selectedRowKeys.length === 0 && state.selectedRows.length === 0) return;
|
if (state.selectedRows.length === 0) {
|
emit('update:value', innerValue.value);
|
emit('change', innerValue.value, props.multiple ? options.value : options.value[0]);
|
formItemContext.onFieldChange();
|
visible.value = false;
|
return;
|
}
|
options.value = state.selectedRows;
|
selectRow.value = state.selectedRows;
|
innerValue.value = unref(selectRow)[0][props.propsValue];
|
}
|
emit('update:value', unref(innerValue));
|
emit('change', unref(innerValue), props.multiple ? unref(selectRow) : unref(selectRow)[0]);
|
formItemContext.onFieldChange();
|
visible.value = false;
|
}
|
function updateSelectRow() {
|
if (!selectRow.value) selectRow.value = [];
|
const newSelectRow = state.selectedRows;
|
for (const item of newSelectRow) {
|
if (!selectRow.value.some((o) => o[props.propsValue] === item[props.propsValue])) {
|
selectRow.value.push(item);
|
}
|
}
|
selectRow.value = selectRow.value.filter(
|
(o) => !(state.list.some((l) => l[props.propsValue] === o[props.propsValue]) && !newSelectRow.some((l) => l[props.propsValue] === o[props.propsValue])),
|
);
|
}
|
function getParamList() {
|
const templateJson: any[] = props.templateJson;
|
if (!props.formData) return templateJson;
|
for (const element of templateJson) {
|
if (element.relationField && element.sourceType == 1) {
|
if (element.relationField.includes('-')) {
|
const tableVModel = element.relationField.split('-')[0];
|
const childVModel = element.relationField.split('-')[1];
|
element.defaultValue =
|
(props.formData[tableVModel] &&
|
props.formData[tableVModel][props.rowIndex as unknown as number] &&
|
props.formData[tableVModel][props.rowIndex as unknown as number][childVModel]) ||
|
'';
|
} else {
|
element.defaultValue = props.formData[element.relationField] || '';
|
}
|
}
|
}
|
return templateJson;
|
}
|
function handleSearch() {
|
state.listQuery.currentPage = 1;
|
state.listQuery.pageSize = props.hasPage ? props.pageSize : 100000;
|
updateSelectRow();
|
nextTick(() => initData());
|
}
|
function handleReset() {
|
getForm().resetFields();
|
state.listQuery.keyword = '';
|
handleSearch();
|
}
|
function initData() {
|
if (!props.interfaceId) return;
|
state.loading = true;
|
const query = {
|
...state.listQuery,
|
...unref(searchInfo),
|
};
|
getDataInterfaceDataSelect(query)
|
.then((res) => {
|
state.list = res.data.list || [];
|
state.total = res.data.pagination.total;
|
state.loading = false;
|
})
|
.catch(() => {
|
state.loading = false;
|
});
|
}
|
function handleTableChange(pagination) {
|
state.listQuery.currentPage = pagination.current;
|
state.listQuery.pageSize = pagination.pageSize;
|
updateSelectRow();
|
nextTick(() => initData());
|
}
|
function setSelectedRowKeys(selectedRowKeys, selectedRows) {
|
state.selectedRowKeys = selectedRowKeys;
|
state.selectedRows = selectedRows;
|
}
|
|
onMounted(() => {
|
state.listQuery.pageSize = props.hasPage ? props.pageSize : 100000;
|
});
|
</script>
|
|
<template>
|
<div class="common-container">
|
<a-select
|
v-model:value="innerValue"
|
v-bind="getSelectBindValue"
|
v-if="popupType !== 'popover' || (popupType == 'popover' && !!disabled)"
|
:options="options"
|
@change="onChange"
|
@click="openSelectModal" />
|
<template v-if="popupType === 'dialog'">
|
<AModal
|
v-model:open="visible"
|
:mask-closable="false"
|
:title="popupTitle"
|
:width="popupWidth"
|
class="common-container-modal"
|
@cancel="handleCancel"
|
@ok="handleSubmit">
|
<template #closeIcon>
|
<ModalClose :can-fullscreen="false" @cancel="handleCancel" />
|
</template>
|
<div class="jnpf-common-search-box jnpf-common-search-box-modal">
|
<a-form ref="formElRef" :class="getFormClass" :colon="false" :model="listQuery" label-align="right">
|
<a-row :gutter="10">
|
<a-col :span="8">
|
<a-form-item :label="$t('common.keyword')" name="keyword">
|
<a-input v-model:value="listQuery.keyword" :placeholder="$t('common.enterKeyword')" allow-clear @press-enter="handleSearch" />
|
</a-form-item>
|
</a-col>
|
<a-col :span="8">
|
<a-form-item label=" ">
|
<a-button class="mr-2" type="primary" @click="handleSearch">{{ $t('common.queryText') }}</a-button>
|
<a-button @click="handleReset">{{ $t('common.resetText') }}</a-button>
|
</a-form-item>
|
</a-col>
|
</a-row>
|
</a-form>
|
<div class="jnpf-common-search-box-right">
|
<a-tooltip placement="top">
|
<template #title>
|
<span>{{ $t('common.redo') }}</span>
|
</template>
|
<RedoOutlined class="jnpf-common-search-box-right-icon" @click="initData" />
|
</a-tooltip>
|
</div>
|
</div>
|
<a-table :data-source="list" v-bind="getTableBindValues" ref="tableElRef" @change="handleTableChange">
|
<template #bodyCell="{ column, record }">
|
<template v-if="column.dataIndex !== 'index'">{{ record[column.dataIndex] }}</template>
|
</template>
|
</a-table>
|
</AModal>
|
</template>
|
<template v-if="popupType === 'drawer'">
|
<ADrawer v-model:open="visible" :class="`${drawerPrefixCls} common-container-drawer`" :title="popupTitle" :width="popupWidth">
|
<div class="common-container-drawer-body">
|
<div class="jnpf-common-search-box jnpf-common-search-box-modal">
|
<a-form ref="formElRef" :class="getFormClass" :colon="false" :model="listQuery" label-align="right">
|
<a-row :gutter="10">
|
<a-col :span="8">
|
<a-form-item :label="$t('common.keyword')" name="keyword">
|
<a-input v-model:value="listQuery.keyword" :placeholder="$t('common.enterKeyword')" allow-clear @press-enter="handleSearch" />
|
</a-form-item>
|
</a-col>
|
<a-col :span="8">
|
<a-form-item label=" ">
|
<a-button class="mr-2" type="primary" @click="handleSearch">{{ $t('common.queryText') }}</a-button>
|
<a-button @click="handleReset">{{ $t('common.resetText') }}</a-button>
|
</a-form-item>
|
</a-col>
|
</a-row>
|
</a-form>
|
<div class="jnpf-common-search-box-right">
|
<a-tooltip placement="top">
|
<template #title>
|
<span>{{ $t('common.redo') }}</span>
|
</template>
|
<RedoOutlined class="jnpf-common-search-box-right-icon" @click="initData" />
|
</a-tooltip>
|
</div>
|
</div>
|
<a-table :data-source="list" v-bind="getTableBindValues" ref="tableElRef" @change="handleTableChange">
|
<template #bodyCell="{ column, record }">
|
<template v-if="column.dataIndex !== 'index'">{{ record[column.dataIndex] }}</template>
|
</template>
|
</a-table>
|
</div>
|
<div :class="drawerFooterPrefixCls" :style="getDrawerFooterStyle">
|
<a-button class="mr-[10px]" @click="handleCancel">{{ $t('common.cancelText') }}</a-button>
|
<a-button class="mr-[10px]" type="primary" @click="handleSubmit">{{ $t('common.okText') }}</a-button>
|
</div>
|
</ADrawer>
|
</template>
|
<a-popover v-if="popupType === 'popover' && !disabled" v-model:open="visible" overlay-class-name="popup-select-popover" placement="bottom" trigger="click">
|
<template #content>
|
<div class="jnpf-common-search-box jnpf-common-search-box-modal">
|
<a-form ref="formElRef" :class="getFormClass" :colon="false" :model="listQuery" label-align="right">
|
<a-row :gutter="10">
|
<a-col :span="8">
|
<a-form-item :label="$t('common.keyword')" name="keyword">
|
<a-input v-model:value="listQuery.keyword" :placeholder="$t('common.enterKeyword')" allow-clear @press-enter="handleSearch" />
|
</a-form-item>
|
</a-col>
|
<a-col :span="8">
|
<a-form-item label=" ">
|
<a-button class="mr-2" type="primary" @click="handleSearch">{{ $t('common.queryText') }}</a-button>
|
<a-button @click="handleReset">{{ $t('common.resetText') }}</a-button>
|
</a-form-item>
|
</a-col>
|
</a-row>
|
</a-form>
|
<div class="jnpf-common-search-box-right">
|
<a-tooltip placement="top">
|
<template #title>
|
<span>{{ $t('common.redo') }}</span>
|
</template>
|
<RedoOutlined class="jnpf-common-search-box-right-icon" @click="initData" />
|
</a-tooltip>
|
</div>
|
</div>
|
<a-table :data-source="list" v-bind="getTableBindValues" ref="tableElRef" @change="handleTableChange">
|
<template #bodyCell="{ column, record }">
|
<template v-if="column.dataIndex !== 'index'">{{ record[column.dataIndex] }}</template>
|
</template>
|
</a-table>
|
<a-space :size="10" class="popover-bottom-btns">
|
<a-button @click="handleCancel">{{ $t('common.cancelText') }}</a-button>
|
<a-button type="primary" @click="handleSubmit">{{ $t('common.okText') }}</a-button>
|
</a-space>
|
</template>
|
<a-select v-model:value="innerValue" v-bind="getSelectBindValue" :options="options" @change="onChange" @click="openSelectModal" />
|
</a-popover>
|
<ExtraRelationInfo
|
v-if="popupType !== 'popover' && extraOptions.length > 0 && JSON.stringify(extraData) !== '{}'"
|
:data="extraData"
|
:extra-options="extraOptions" />
|
</div>
|
</template>
|
<style lang="scss">
|
.popup-select-popover {
|
.ant-popover-inner-content {
|
width: 700px;
|
padding: 10px;
|
overflow: hidden;
|
|
.popover-bottom-btns {
|
justify-content: flex-end;
|
width: 100%;
|
margin-top: 10px;
|
}
|
}
|
}
|
</style>
|