优化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">
import { computed } from 'vue'
import { computed, ref, nextTick } from 'vue'
import { NInput } from 'naive-ui'
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 }>()
@ -9,9 +10,25 @@ const props = defineProps<{ df: any; record: Record<string, any>; canEdit: boole
const labelLayout = computed(() => props.df.label_layout || 'vertical')
//
const iconValue = computed(() => {
return props.record[props.df.fieldname] || ''
const iconValue = computed({
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>
<template>
@ -20,10 +37,32 @@ const iconValue = computed(() => {
{{ ctx.t(df.label || df.fieldname) }}
<span v-if="df.reqd" class="required">*</span>
</label>
<!-- Icon 字段特殊布局左边显示图标右边显示字段值 -->
<!-- Icon 字段特殊布局左边显示图标可点击右边显示字段值 -->
<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" />
<span v-else class="icon-placeholder"></span>
</div>
@ -33,14 +72,25 @@ const iconValue = computed(() => {
<span v-if="!canEdit" class="field-value-text">
{{ iconValue || '—' }}
</span>
<!-- 编辑模式显示输入框 -->
<!-- 编辑模式输入框无前缀图标无右侧按钮-->
<n-input
v-else
v-model:value="record[df.fieldname]"
:placeholder="ctx.t(df.fieldname)"
v-model:value="iconValue"
:placeholder="ctx.t('Enter icon name or click icon to select')"
class="icon-input"
clearable
/>
</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>
</template>
@ -60,10 +110,28 @@ const iconValue = computed(() => {
display: flex;
align-items: center;
justify-content: center;
min-width: 32px;
width: 32px;
height: 32px;
min-width: 36px;
width: 36px;
height: 36px;
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 {
@ -87,6 +155,20 @@ const iconValue = computed(() => {
display: flex;
align-items: center;
}
/* 编辑模式:输入框样式 */
.icon-input {
flex: 1;
min-width: 0;
}
.icon-preview {
transition: all 0.2s ease;
}
.icon-placeholder-icon {
opacity: 0.5;
}
</style>