NameInput.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <!-- 场景名称输入组件 -->
  2. <template>
  3. <div class="name-input">
  4. <el-input
  5. v-model="localValue"
  6. placeholder="请输入场景名称"
  7. maxlength="50"
  8. show-word-limit
  9. clearable
  10. @blur="handleBlur"
  11. @input="handleInput"
  12. >
  13. <template #prefix>
  14. <Icon icon="ep:edit" class="input-icon" />
  15. </template>
  16. </el-input>
  17. <!-- 智能提示 -->
  18. <!-- TODO @puhui999:暂时不用考虑智能推荐哈。用途不大 -->
  19. <div v-if="showSuggestions && suggestions.length > 0" class="suggestions">
  20. <div class="suggestions-header">
  21. <span class="suggestions-title">推荐名称</span>
  22. </div>
  23. <div class="suggestions-list">
  24. <div
  25. v-for="suggestion in suggestions"
  26. :key="suggestion"
  27. class="suggestion-item"
  28. @click="applySuggestion(suggestion)"
  29. >
  30. {{ suggestion }}
  31. </div>
  32. </div>
  33. </div>
  34. </div>
  35. </template>
  36. <script setup lang="ts">
  37. import { useVModel } from '@vueuse/core'
  38. /** 场景名称输入组件 */
  39. defineOptions({ name: 'NameInput' })
  40. interface Props {
  41. modelValue: string
  42. }
  43. interface Emits {
  44. (e: 'update:modelValue', value: string): void
  45. }
  46. const props = defineProps<Props>()
  47. const emit = defineEmits<Emits>()
  48. const localValue = useVModel(props, 'modelValue', emit)
  49. // 智能提示相关
  50. const showSuggestions = ref(false)
  51. const suggestions = ref<string[]>([])
  52. // 常用场景名称模板
  53. const nameTemplates = [
  54. '温度过高自动降温',
  55. '设备离线告警通知',
  56. '湿度异常自动调节',
  57. '夜间安防模式启动',
  58. '能耗超标自动关闭',
  59. '故障设备自动重启',
  60. '定时设备状态检查',
  61. '环境数据异常告警'
  62. ]
  63. const handleInput = (value: string) => {
  64. if (value.length > 0 && value.length < 10) {
  65. // 根据输入内容过滤建议
  66. suggestions.value = nameTemplates
  67. .filter(
  68. (template) =>
  69. template.includes(value) || (value.includes('温度') && template.includes('温度'))
  70. )
  71. .slice(0, 5)
  72. showSuggestions.value = suggestions.value.length > 0
  73. } else {
  74. showSuggestions.value = false
  75. }
  76. }
  77. const handleBlur = () => {
  78. // 延迟隐藏建议,允许点击建议项
  79. setTimeout(() => {
  80. showSuggestions.value = false
  81. }, 200)
  82. }
  83. const applySuggestion = (suggestion: string) => {
  84. localValue.value = suggestion
  85. showSuggestions.value = false
  86. }
  87. // 监听外部点击隐藏建议
  88. onMounted(() => {
  89. document.addEventListener('click', (e) => {
  90. const target = e.target as HTMLElement
  91. if (!target.closest('.name-input')) {
  92. showSuggestions.value = false
  93. }
  94. })
  95. })
  96. </script>
  97. <style scoped>
  98. .name-input {
  99. position: relative;
  100. width: 100%;
  101. }
  102. .input-icon {
  103. color: var(--el-text-color-placeholder);
  104. }
  105. .suggestions {
  106. position: absolute;
  107. top: 100%;
  108. left: 0;
  109. right: 0;
  110. z-index: 1000;
  111. background: white;
  112. border: 1px solid var(--el-border-color-light);
  113. border-radius: 4px;
  114. box-shadow: var(--el-box-shadow-light);
  115. margin-top: 4px;
  116. }
  117. .suggestions-header {
  118. padding: 8px 12px;
  119. border-bottom: 1px solid var(--el-border-color-lighter);
  120. background: var(--el-fill-color-light);
  121. }
  122. .suggestions-title {
  123. font-size: 12px;
  124. color: var(--el-text-color-secondary);
  125. font-weight: 500;
  126. }
  127. .suggestions-list {
  128. max-height: 200px;
  129. overflow-y: auto;
  130. }
  131. .suggestion-item {
  132. padding: 8px 12px;
  133. cursor: pointer;
  134. transition: background-color 0.2s;
  135. font-size: 14px;
  136. color: var(--el-text-color-primary);
  137. }
  138. .suggestion-item:hover {
  139. background: var(--el-fill-color-light);
  140. }
  141. .suggestion-item:last-child {
  142. border-bottom: none;
  143. }
  144. </style>