| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- const n = `<template>
- <div class="s-icon-select-common">
- <a-popover trigger="click" placement="bottomLeft" v-model:open="visible">
- <template #content>
- <div class="picker-panel" :style="{ width: panelWidth }">
- <a-input-search
- v-model:value="keyword"
- class="icon-search"
- :placeholder="$l('搜索图标')"
- allow-clear
- />
- <div class="icon-list">
- <div
- v-for="icon in filteredIcons"
- :key="icon"
- class="item-icon"
- :class="{ active: icon === value }"
- @click="pick(icon)"
- >
- <component :is="icon" class="icon" />
- <div class="name" :title="icon">{{ icon }}</div>
- </div>
- <div v-if="!filteredIcons.length" class="icon-empty">{{ $l('暂无数据') }}</div>
- </div>
- </div>
- </template>
- <a-input
- class="icon-input"
- :readonly="true"
- :value="value"
- :placeholder="placeholder"
- :style="{ width: inputWidth }"
- >
- <template #prefix>
- <component v-if="value" :is="value" class="prefix-icon" />
- </template>
- <template #suffix>
- <down-outlined class="select-arrow" :class="{ open: visible }" />
- </template>
- </a-input>
- </a-popover>
- </div>
- </template>
- <script setup>
- import { computed, ref, watch } from 'vue'
- import { filterIcons } from './icon-groups'
- const props = defineProps({
- // 选中图标名,支持 v-model:value
- value: {
- type: String,
- default: ''
- },
- // 面板宽度,单位 px
- width: {
- type: [Number, String],
- default: 280
- },
- // 搜索框占位文案
- placeholder: {
- type: String,
- default() {
- return window.$l('请选择图标')
- }
- },
- // 保留:下拉版不再需要弹窗标题,兼容旧调用方传参
- title: {
- type: String,
- default() {
- return window.$l('图标选择')
- }
- }
- })
- const emits = defineEmits(['update:value', 'change'])
- const visible = ref(false)
- const keyword = ref('')
- // width 允许 280 / '280' / '280px' / '50%' / '20rem':数字或无单位字符串补 px,其余原样使用
- const inputWidth = computed(() => {
- const raw = props.width
- if (typeof raw === 'number') return \`\${raw}px\`
- const str = String(raw ?? '').trim()
- if (!str) return '280px'
- return /^-?\\d+(\\.\\d+)?$/.test(str) ? \`\${str}px\` : str
- })
- // 面板比输入框宽,保证一行能放 5 个图标;输入框本身更宽时跟随输入框
- const panelWidth = computed(() => {
- const px = parseFloat(inputWidth.value)
- return \`\${Math.max(280, Number.isFinite(px) ? px : 0)}px\`
- })
- const filteredIcons = computed(() => filterIcons('all', keyword.value))
- // 面板收起就清空关键字,下次打开回到完整列表(下拉的常规行为)
- watch(visible, (val) => {
- if (!val) keyword.value = ''
- })
- const open = () => {
- visible.value = true
- }
- const pick = (icon) => {
- emits('update:value', icon)
- emits('change', icon)
- visible.value = false
- }
- defineExpose({ open })
- <\/script>
- <style lang="less" scoped>
- .s-icon-select-common {
- display: inline-block;
- }
- .icon-input {
- cursor: pointer;
- :deep(input.ant-input) {
- cursor: pointer;
- }
- }
- .prefix-icon {
- font-size: 16px;
- }
- .select-arrow {
- font-size: 12px;
- color: var(--s-text-color-secondary);
- transition: transform 0.2s;
- &.open {
- transform: rotate(180deg);
- }
- }
- .picker-panel {
- .icon-search {
- margin-bottom: 10px;
- }
- }
- .icon-list {
- max-height: 264px;
- overflow: hidden auto;
- display: flex;
- flex-wrap: wrap;
- align-content: flex-start;
- .item-icon {
- width: 64px;
- margin: 2px;
- padding: 8px 0 6px;
- cursor: pointer;
- border: 1px solid transparent;
- border-radius: 4px;
- box-sizing: border-box;
- display: flex;
- flex-direction: column;
- justify-content: center;
- align-items: center;
- &:hover {
- background: var(--s-bg-color);
- }
- &.active {
- border-color: var(--s-color);
- background: var(--s-color-bg);
- }
- .icon {
- font-size: 20px;
- }
- .name {
- width: 100%;
- margin-top: 6px;
- padding: 0 2px;
- font-size: 10px;
- line-height: 14px;
- text-align: center;
- color: var(--s-text-color-secondary);
- text-overflow: ellipsis;
- overflow: hidden;
- white-space: nowrap;
- box-sizing: border-box;
- }
- }
- }
- .icon-empty {
- width: 100%;
- padding: 30px 0;
- text-align: center;
- color: var(--s-text-color-secondary);
- }
- </style>
- `;
- export {
- n as default
- };
|