fix(components): 修复菜单选择标签渲染问题

This commit is contained in:
xlsea 2025-06-18 23:48:49 +08:00
parent 8c84063ad1
commit 6e6cc4d91f
4 changed files with 33 additions and 8 deletions

View File

@ -1,18 +1,22 @@
<script setup lang="tsx"> <script setup lang="tsx">
import { useAttrs } from 'vue'; import { onMounted, useAttrs } from 'vue';
import type { TreeOption, TreeSelectProps } from 'naive-ui'; import type { TreeOption, TreeSelectProps } from 'naive-ui';
import { useLoading } from '@sa/hooks'; import { useLoading } from '@sa/hooks';
import { fetchGetMenuList } from '@/service/api/system'; import { fetchGetMenuList } from '@/service/api/system';
import { handleTree } from '@/utils/common'; import { handleTree } from '@/utils/common';
import SvgIcon from '@/components/custom/svg-icon.vue'; import SvgIcon from '@/components/custom/svg-icon.vue';
import { $t } from '@/locales';
defineOptions({ name: 'MenuTreeSelect' }); defineOptions({ name: 'MenuTreeSelect' });
interface Props { interface Props {
immediate?: boolean;
[key: string]: any; [key: string]: any;
} }
defineProps<Props>(); const props = withDefaults(defineProps<Props>(), {
immediate: true
});
const value = defineModel<CommonType.IdType | null>('value', { required: false }); const value = defineModel<CommonType.IdType | null>('value', { required: false });
const options = defineModel<Api.System.MenuList>('options', { required: false, default: [] }); const options = defineModel<Api.System.MenuList>('options', { required: false, default: [] });
@ -35,7 +39,19 @@ async function getMenuList() {
endLoading(); endLoading();
} }
getMenuList(); onMounted(() => {
if (props.immediate) {
getMenuList();
}
});
function renderLabel({ option }: { option: TreeOption }) {
let label = String(option.menuName);
if (label?.startsWith('route.') || label?.startsWith('menu.')) {
label = $t(label as App.I18n.I18nKey);
}
return <div>{label}</div>;
}
function renderPrefix({ option }: { option: TreeOption }) { function renderPrefix({ option }: { option: TreeOption }) {
const renderLocalIcon = String(option.icon).startsWith('local-icon-'); const renderLocalIcon = String(option.icon).startsWith('local-icon-');
@ -55,6 +71,8 @@ function renderPrefix({ option }: { option: TreeOption }) {
label-field="menuName" label-field="menuName"
:options="options" :options="options"
:default-expanded-keys="[0]" :default-expanded-keys="[0]"
:render-tag="renderLabel"
:render-label="renderLabel"
:render-prefix="renderPrefix" :render-prefix="renderPrefix"
v-bind="attrs" v-bind="attrs"
/> />

View File

@ -4,6 +4,7 @@ import type { TreeOption, TreeSelectInst, TreeSelectProps } from 'naive-ui';
import { useBoolean } from '@sa/hooks'; import { useBoolean } from '@sa/hooks';
import { fetchGetMenuTreeSelect } from '@/service/api/system'; import { fetchGetMenuTreeSelect } from '@/service/api/system';
import SvgIcon from '@/components/custom/svg-icon.vue'; import SvgIcon from '@/components/custom/svg-icon.vue';
import { $t } from '@/locales';
defineOptions({ name: 'MenuTree' }); defineOptions({ name: 'MenuTree' });
@ -60,6 +61,14 @@ watch([expandAll, options], ([newVal]) => {
} }
}); });
function renderLabel({ option }: { option: TreeOption }) {
let label = option.label;
if (label?.startsWith('route.') || label?.startsWith('menu.')) {
label = $t(label as App.I18n.I18nKey);
}
return <div>{label}</div>;
}
function renderPrefix({ option }: { option: TreeOption }) { function renderPrefix({ option }: { option: TreeOption }) {
const renderLocalIcon = String(option.icon).startsWith('local-icon-'); const renderLocalIcon = String(option.icon).startsWith('local-icon-');
let icon = renderLocalIcon ? undefined : String(option.icon ?? 'material-symbols:buttons-alt-outline-rounded'); let icon = renderLocalIcon ? undefined : String(option.icon ?? 'material-symbols:buttons-alt-outline-rounded');
@ -163,6 +172,7 @@ defineExpose({
:loading="loading" :loading="loading"
virtual-scroll virtual-scroll
check-strategy="all" check-strategy="all"
:render-label="renderLabel"
:render-prefix="renderPrefix" :render-prefix="renderPrefix"
v-bind="attrs" v-bind="attrs"
/> />

View File

@ -69,7 +69,6 @@ declare module 'vue' {
NButton: typeof import('naive-ui')['NButton'] NButton: typeof import('naive-ui')['NButton']
NCard: typeof import('naive-ui')['NCard'] NCard: typeof import('naive-ui')['NCard']
NCheckbox: typeof import('naive-ui')['NCheckbox'] NCheckbox: typeof import('naive-ui')['NCheckbox']
NCheckboxGroup: typeof import('naive-ui')['NCheckboxGroup']
NCode: typeof import('naive-ui')['NCode'] NCode: typeof import('naive-ui')['NCode']
NCollapse: typeof import('naive-ui')['NCollapse'] NCollapse: typeof import('naive-ui')['NCollapse']
NCollapseItem: typeof import('naive-ui')['NCollapseItem'] NCollapseItem: typeof import('naive-ui')['NCollapseItem']

View File

@ -255,12 +255,10 @@ function onCreate() {
<NForm ref="formRef" :model="model" :rules="rules"> <NForm ref="formRef" :model="model" :rules="rules">
<NGrid responsive="screen" item-responsive> <NGrid responsive="screen" item-responsive>
<NFormItemGi :span="24" :label="$t('page.system.menu.parentId')" path="pid"> <NFormItemGi :span="24" :label="$t('page.system.menu.parentId')" path="pid">
<NTreeSelect <MenuTreeSelect
v-model:value="model.parentId" v-model:value="model.parentId"
:immediate="false"
:options="treeData as []" :options="treeData as []"
label-field="menuName"
key-field="menuId"
:default-expanded-keys="[0]"
:placeholder="$t('page.system.menu.form.parentId.required')" :placeholder="$t('page.system.menu.form.parentId.required')"
/> />
</NFormItemGi> </NFormItemGi>