优化pagetype详情页Icon图标渲染及输入方式

This commit is contained in:
jingrow 2025-11-01 20:32:33 +08:00
parent 1fe27e25ca
commit d7be207545

View File

@ -1,7 +1,8 @@
<script setup lang="ts"> <script setup lang="ts">
import { computed } from 'vue' import { computed, ref, nextTick } from 'vue'
import { NInput } from 'naive-ui' import { NInput } from 'naive-ui'
import { Icon } from '@iconify/vue' import { Icon } from '@iconify/vue'
import IconPicker from '@/core/components/IconPicker.vue'
const props = defineProps<{ df: any; record: Record<string, any>; canEdit: boolean; ctx: any }>() const props = defineProps<{ df: any; record: Record<string, any>; canEdit: boolean; ctx: any }>()
@ -9,9 +10,25 @@ const props = defineProps<{ df: any; record: Record<string, any>; canEdit: boole
const labelLayout = computed(() => props.df.label_layout || 'vertical') const labelLayout = computed(() => props.df.label_layout || 'vertical')
// //
const iconValue = computed(() => { const iconValue = computed({
return props.record[props.df.fieldname] || '' get: () => props.record[props.df.fieldname] || '',
set: (value) => {
props.record[props.df.fieldname] = value
}
}) })
// IconPicker
const iconPickerRef = ref<InstanceType<typeof IconPicker> | null>(null)
//
async function openIconPicker() {
await nextTick()
// IconPicker
const triggerButton = iconPickerRef.value?.$el?.querySelector('.icon-trigger') as HTMLElement
if (triggerButton) {
triggerButton.click()
}
}
</script> </script>
<template> <template>
@ -20,10 +37,32 @@ const iconValue = computed(() => {
{{ ctx.t(df.label || df.fieldname) }} {{ ctx.t(df.label || df.fieldname) }}
<span v-if="df.reqd" class="required">*</span> <span v-if="df.reqd" class="required">*</span>
</label> </label>
<!-- Icon 字段特殊布局左边显示图标右边显示字段值 --> <!-- Icon 字段特殊布局左边显示图标可点击右边显示字段值 -->
<div class="icon-field-content"> <div class="icon-field-content">
<!-- 左侧图标 --> <!-- 左侧图标可点击打开选择器 -->
<div class="icon-display"> <div
v-if="canEdit"
class="icon-display clickable"
@click="openIconPicker"
:title="ctx.t('Click to select icon')"
>
<Icon
v-if="iconValue"
:icon="iconValue"
:width="24"
:height="24"
class="icon-gray icon-preview"
/>
<Icon
v-else
icon="tabler:apps"
:width="24"
:height="24"
class="icon-gray icon-placeholder-icon"
/>
</div>
<!-- 只读模式左侧仅显示图标 -->
<div v-else class="icon-display">
<Icon v-if="iconValue" :icon="iconValue" :width="24" :height="24" class="icon-gray" /> <Icon v-if="iconValue" :icon="iconValue" :width="24" :height="24" class="icon-gray" />
<span v-else class="icon-placeholder"></span> <span v-else class="icon-placeholder"></span>
</div> </div>
@ -33,14 +72,25 @@ const iconValue = computed(() => {
<span v-if="!canEdit" class="field-value-text"> <span v-if="!canEdit" class="field-value-text">
{{ iconValue || '—' }} {{ iconValue || '—' }}
</span> </span>
<!-- 编辑模式显示输入框 --> <!-- 编辑模式输入框无前缀图标无右侧按钮-->
<n-input <n-input
v-else v-else
v-model:value="record[df.fieldname]" v-model:value="iconValue"
:placeholder="ctx.t(df.fieldname)" :placeholder="ctx.t('Enter icon name or click icon to select')"
class="icon-input"
clearable
/> />
</div> </div>
</div> </div>
<!-- 图标选择器隐藏触发按钮通过自定义按钮触发-->
<div v-if="canEdit" style="position: absolute; left: -9999px; opacity: 0; pointer-events: none;">
<IconPicker
ref="iconPickerRef"
:model-value="iconValue"
@update:model-value="iconValue = $event"
/>
</div>
</div> </div>
</template> </template>
@ -60,10 +110,28 @@ const iconValue = computed(() => {
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
min-width: 32px; min-width: 36px;
width: 32px; width: 36px;
height: 32px; height: 36px;
flex-shrink: 0; flex-shrink: 0;
border-radius: 6px;
transition: all 0.2s ease;
}
.icon-display.clickable {
cursor: pointer;
background-color: #f9fafb;
border: 1px solid #e5e7eb;
}
.icon-display.clickable:hover {
background-color: #f3f4f6;
border-color: #d1d5db;
transform: scale(1.05);
}
.icon-display.clickable:active {
transform: scale(0.98);
} }
.icon-gray { .icon-gray {
@ -87,6 +155,20 @@ const iconValue = computed(() => {
display: flex; display: flex;
align-items: center; align-items: center;
} }
/* 编辑模式:输入框样式 */
.icon-input {
flex: 1;
min-width: 0;
}
.icon-preview {
transition: all 0.2s ease;
}
.icon-placeholder-icon {
opacity: 0.5;
}
</style> </style>