refactor: 用户选择器重构
This commit is contained in:
parent
2e6d636997
commit
88bc0b649b
|
@ -2,7 +2,6 @@
|
||||||
<a-modal
|
<a-modal
|
||||||
v-model:visible="currentVisible"
|
v-model:visible="currentVisible"
|
||||||
class="ms-modal-form ms-modal-medium"
|
class="ms-modal-form ms-modal-medium"
|
||||||
width="680px"
|
|
||||||
text-align="start"
|
text-align="start"
|
||||||
:ok-text="t('system.userGroup.add')"
|
:ok-text="t('system.userGroup.add')"
|
||||||
unmount-on-close
|
unmount-on-close
|
||||||
|
@ -10,7 +9,7 @@
|
||||||
>
|
>
|
||||||
<template #title> {{ t('system.userGroup.addUser') }} </template>
|
<template #title> {{ t('system.userGroup.addUser') }} </template>
|
||||||
<div class="form">
|
<div class="form">
|
||||||
<a-form ref="formRef" :model="form" size="large" :style="{ width: '600px' }" layout="vertical">
|
<a-form ref="formRef" class="rounded-[4px]" :model="form" layout="vertical">
|
||||||
<a-form-item
|
<a-form-item
|
||||||
field="name"
|
field="name"
|
||||||
:label="t('system.userGroup.user')"
|
:label="t('system.userGroup.user')"
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
multiple
|
multiple
|
||||||
:value-key="props.valueKey"
|
:value-key="props.valueKey"
|
||||||
:disabled="props.disabled"
|
:disabled="props.disabled"
|
||||||
|
:filter-option="false"
|
||||||
allow-clear
|
allow-clear
|
||||||
@change="change"
|
@change="change"
|
||||||
@search="debouncedSearch"
|
@search="debouncedSearch"
|
||||||
|
@ -12,7 +13,7 @@
|
||||||
<template #label="{ data }">
|
<template #label="{ data }">
|
||||||
<span class="text-[var(--color-text-1)]"> {{ data.value.name }} </span>
|
<span class="text-[var(--color-text-1)]"> {{ data.value.name }} </span>
|
||||||
</template>
|
</template>
|
||||||
<a-option v-for="data in currentOptions" :key="data.id" :disabled="data.disabled" :value="data">
|
<a-option v-for="data in allOptions" :key="data.id" :disabled="data.disabled" :value="data">
|
||||||
<span :class="data.disabled ? 'text-[var(--color-text-4)]' : 'text-[var(--color-text-1)]'">
|
<span :class="data.disabled ? 'text-[var(--color-text-4)]' : 'text-[var(--color-text-1)]'">
|
||||||
{{ data.name }}
|
{{ data.name }}
|
||||||
</span>
|
</span>
|
||||||
|
@ -62,25 +63,18 @@
|
||||||
}>();
|
}>();
|
||||||
const { t } = useI18n();
|
const { t } = useI18n();
|
||||||
|
|
||||||
|
const allOptions = ref<MsUserSelectorOption[]>([]);
|
||||||
const currentOptions = ref<MsUserSelectorOption[]>([]);
|
const currentOptions = ref<MsUserSelectorOption[]>([]);
|
||||||
const currentLoadParams = ref<Record<string, any>>(props.loadOptionParams || {});
|
const currentLoadParams = ref<Record<string, any>>(props.loadOptionParams || {});
|
||||||
|
const loading = ref(false);
|
||||||
|
|
||||||
const currentValue = computed(() => {
|
const currentValue = computed(() => {
|
||||||
return currentOptions.value.filter((item) => props.value.includes(item.id)) || [];
|
return currentOptions.value.filter((item) => props.value.includes(item.id)) || [];
|
||||||
});
|
});
|
||||||
|
|
||||||
const change = (
|
|
||||||
value: string | number | boolean | Record<string, any> | (string | number | boolean | Record<string, any>)[]
|
|
||||||
) => {
|
|
||||||
const tmpArr = Array.isArray(value) ? value : [value];
|
|
||||||
const { valueKey } = props;
|
|
||||||
emit(
|
|
||||||
'update:value',
|
|
||||||
tmpArr.map((item) => item[valueKey])
|
|
||||||
);
|
|
||||||
};
|
|
||||||
const loadList = async () => {
|
const loadList = async () => {
|
||||||
try {
|
try {
|
||||||
|
loading.value = true;
|
||||||
const list = (await initOptionsFunc(props.type, currentLoadParams.value || {})) || [];
|
const list = (await initOptionsFunc(props.type, currentLoadParams.value || {})) || [];
|
||||||
const { firstLabelKey, secondLabelKey, disabledKey, valueKey } = props;
|
const { firstLabelKey, secondLabelKey, disabledKey, valueKey } = props;
|
||||||
list.forEach((item: MsUserSelectorOption) => {
|
list.forEach((item: MsUserSelectorOption) => {
|
||||||
|
@ -97,23 +91,49 @@
|
||||||
item.id = item[valueKey] as string;
|
item.id = item[valueKey] as string;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
currentOptions.value = [...list];
|
allOptions.value = [...list];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log(error);
|
console.log(error);
|
||||||
currentOptions.value = [];
|
allOptions.value = [];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const search = async (value: string) => {
|
const debouncedSearch = async (value: string) => {
|
||||||
currentLoadParams.value = {
|
if (!value) {
|
||||||
...currentLoadParams.value,
|
currentLoadParams.value = {
|
||||||
keyword: value,
|
...currentLoadParams.value,
|
||||||
};
|
keyword: value,
|
||||||
await loadList();
|
};
|
||||||
|
await loadList();
|
||||||
|
} else {
|
||||||
|
const fn = debounce(
|
||||||
|
() => {
|
||||||
|
currentLoadParams.value = {
|
||||||
|
...currentLoadParams.value,
|
||||||
|
keyword: value,
|
||||||
|
};
|
||||||
|
loadList();
|
||||||
|
},
|
||||||
|
300,
|
||||||
|
{ maxWait: 1000 }
|
||||||
|
);
|
||||||
|
fn();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const debouncedSearch = debounce(search, 300, { maxWait: 1000 });
|
const change = (
|
||||||
|
value: string | number | boolean | Record<string, any> | (string | number | boolean | Record<string, any>)[]
|
||||||
|
) => {
|
||||||
|
const tmpArr = Array.isArray(value) ? value : [value];
|
||||||
|
currentOptions.value = tmpArr;
|
||||||
|
const { valueKey } = props;
|
||||||
|
emit(
|
||||||
|
'update:value',
|
||||||
|
tmpArr.map((item) => item[valueKey])
|
||||||
|
);
|
||||||
|
debouncedSearch('');
|
||||||
|
};
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
await loadList();
|
await loadList();
|
||||||
|
|
|
@ -66,19 +66,18 @@
|
||||||
v-model:visible="addUserGroupVisible"
|
v-model:visible="addUserGroupVisible"
|
||||||
:ok-text="t('common.create')"
|
:ok-text="t('common.create')"
|
||||||
title-align="start"
|
title-align="start"
|
||||||
class="ms-modal-form ms-modal-medium"
|
class="ms-modal-form ms-modal-small"
|
||||||
width="480px"
|
|
||||||
>
|
>
|
||||||
<template #title> {{ t('project.userGroup.addUserGroup') }} </template>
|
<template #title> {{ t('project.userGroup.addUserGroup') }} </template>
|
||||||
<div class="form">
|
<div class="form">
|
||||||
<a-form ref="addUserGroupFormRef" :model="form" size="large" layout="vertical">
|
<a-form ref="addUserGroupFormRef" :model="form" layout="vertical">
|
||||||
<a-form-item
|
<a-form-item
|
||||||
field="name"
|
field="name"
|
||||||
:label="t('project.userGroup.name')"
|
:label="t('project.userGroup.name')"
|
||||||
:rules="[{ required: true, message: t('project.userGroup.addRequired') }]"
|
:rules="[{ required: true, message: t('project.userGroup.addRequired') }]"
|
||||||
asterisk-position="end"
|
asterisk-position="end"
|
||||||
>
|
>
|
||||||
<a-input v-model="form.name" class="w-100" />
|
<a-input v-model="form.name" />
|
||||||
</a-form-item>
|
</a-form-item>
|
||||||
</a-form>
|
</a-form>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<template>
|
<template>
|
||||||
<a-modal
|
<a-modal
|
||||||
v-model:visible="currentVisible"
|
v-model:visible="currentVisible"
|
||||||
width="680px"
|
title-align="start"
|
||||||
class="ms-modal-form ms-modal-medium"
|
class="ms-modal-form ms-modal-medium"
|
||||||
unmount-on-close
|
unmount-on-close
|
||||||
@cancel="handleCancel(false)"
|
@cancel="handleCancel(false)"
|
||||||
|
@ -16,7 +16,7 @@
|
||||||
</span>
|
</span>
|
||||||
</template>
|
</template>
|
||||||
<div class="form">
|
<div class="form">
|
||||||
<a-form ref="formRef" :model="form" size="large" :style="{ width: '632px' }" layout="vertical">
|
<a-form ref="formRef" class="rounded-[4px]" :model="form" layout="vertical">
|
||||||
<a-form-item
|
<a-form-item
|
||||||
field="name"
|
field="name"
|
||||||
required
|
required
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
<template>
|
<template>
|
||||||
<a-modal
|
<a-modal
|
||||||
v-model:visible="currentVisible"
|
v-model:visible="currentVisible"
|
||||||
width="680px"
|
|
||||||
class="ms-modal-form ms-modal-medium"
|
class="ms-modal-form ms-modal-medium"
|
||||||
:ok-text="t('system.organization.create')"
|
:ok-text="t('system.organization.create')"
|
||||||
unmount-on-close
|
unmount-on-close
|
||||||
|
title-align="start"
|
||||||
@cancel="handleCancel"
|
@cancel="handleCancel"
|
||||||
>
|
>
|
||||||
<template #title>
|
<template #title>
|
||||||
|
@ -17,7 +17,7 @@
|
||||||
</span>
|
</span>
|
||||||
</template>
|
</template>
|
||||||
<div class="form">
|
<div class="form">
|
||||||
<a-form ref="formRef" :model="form" size="large" :style="{ width: '600px' }" layout="vertical">
|
<a-form ref="formRef" class="rounded-[4px]" :model="form" layout="vertical">
|
||||||
<a-form-item
|
<a-form-item
|
||||||
field="name"
|
field="name"
|
||||||
required
|
required
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
width="680px"
|
width="680px"
|
||||||
class="ms-modal-form ms-modal-medium"
|
class="ms-modal-form ms-modal-medium"
|
||||||
:ok-text="isEdit ? t('common.update') : t('common.create')"
|
:ok-text="isEdit ? t('common.update') : t('common.create')"
|
||||||
|
title-align="start"
|
||||||
unmount-on-close
|
unmount-on-close
|
||||||
@cancel="handleCancel(false)"
|
@cancel="handleCancel(false)"
|
||||||
>
|
>
|
||||||
|
@ -17,7 +18,7 @@
|
||||||
</span>
|
</span>
|
||||||
</template>
|
</template>
|
||||||
<div class="form">
|
<div class="form">
|
||||||
<a-form ref="formRef" :model="form" size="large" :style="{ width: '600px' }" layout="vertical">
|
<a-form ref="formRef" :model="form" :style="{ width: '600px' }" layout="vertical">
|
||||||
<a-form-item
|
<a-form-item
|
||||||
field="name"
|
field="name"
|
||||||
required
|
required
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
>
|
>
|
||||||
<template #title> {{ t('system.organization.addMember') }} </template>
|
<template #title> {{ t('system.organization.addMember') }} </template>
|
||||||
<div class="form">
|
<div class="form">
|
||||||
<a-form ref="formRef" :model="form" size="large" :style="{ width: '600px' }" layout="vertical">
|
<a-form ref="formRef" class="rounded-[4px]" :model="form" layout="vertical">
|
||||||
<a-form-item
|
<a-form-item
|
||||||
field="name"
|
field="name"
|
||||||
:label="t('system.organization.member')"
|
:label="t('system.organization.member')"
|
||||||
|
|
Loading…
Reference in New Issue