lefengyang
2026-08-20 d01a28cb030b2cb0ff1d42273c877a0f728519a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<template>
  <div class="history-selector">
    <label class="select-label">选择历史数据:</label>
    <div class="custom-select">
      <div class="select-value" @click="toggleDropdown">
        <span v-if="!selectedItem">-- 选择历史数据 --</span>
        <span v-else>{{ selectedItem.name }} - {{ selectedItem.goodsId }}</span>
        <span class="select-arrow" :class="{ 'open': isDropdownOpen }"></span>
      </div>
      <ul class="select-dropdown" v-if="isDropdownOpen" @click.stop>
        <li
          class="select-option"
          :class="{ 'selected': !selectedItem }"
          @click="selectOption(null)"
        >
          -- 选择历史数据 --
        </li>
        <li
          v-for="(item, index) in historyItems"
          :key="index"
          class="select-option"
          :class="{ 'selected': selectedItem && selectedItem.goodsId === item.goodsId && selectedItem.bid === item.bid }"
          @click="selectOption(item)"
        >
          {{ item.name }} - {{ item.goodsId }}
        </li>
      </ul>
    </div>
  </div>
</template>
 
<script setup lang="ts">
import {ref, watch} from 'vue';
import {BidFormData} from "/src-com";
 
interface Props {
  historyItems: BidFormData[];
  modelValue: BidFormData | null;
}
 
const props = defineProps<Props>();
const emit = defineEmits([
    'update:modelValue',
    'selected'
]);
 
const isDropdownOpen = ref(false);
const selectedItem = ref<BidFormData | null>(props.modelValue);
 
// 监听外部modelValue变化
watch(() => props.modelValue, (newValue) => {
  selectedItem.value = newValue;
});
 
// 切换下拉菜单
const toggleDropdown = () => {
  isDropdownOpen.value = !isDropdownOpen.value;
};
 
// 选择选项
const selectOption = (item: BidFormData | null) => {
  selectedItem.value = item;
  emit('update:modelValue', item);
  emit('selected', item);
  isDropdownOpen.value = false;
};
 
// 点击外部关闭下拉菜单
const handleClickOutside = (event: MouseEvent) => {
  const selectElement = document.querySelector('.custom-select');
  if (selectElement && !selectElement.contains(event.target as Node)) {
    isDropdownOpen.value = false;
  }
};
 
// 添加点击外部事件监听
document.addEventListener('click', handleClickOutside);
 
// 组件卸载时移除事件监听
defineExpose({
  onUnmounted: () => {
    document.removeEventListener('click', handleClickOutside);
  }
});
</script>
 
<style scoped>
.history-selector {
  margin-bottom: 20px;
}
 
.select-label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
}
 
.custom-select {
  position: relative;
  width: 100%;
  max-width: 400px;
}
 
.select-value {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background-color: #fff;
  border: 1px solid #ddd;
  border-radius: 4px;
  cursor: pointer;
  transition: border-color 0.3s;
}
 
.select-value:hover {
  border-color: #666;
}
 
.select-arrow {
  display: inline-block;
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 5px solid #666;
  transition: transform 0.3s;
}
 
.select-arrow.open {
  transform: rotate(180deg);
}
 
.select-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background-color: #fff;
  border: 1px solid #ddd;
  border-top: none;
  border-radius: 0 0 4px 4px;
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 200px;
  overflow-y: auto;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 10;
}
 
.select-option {
  padding: 8px 12px;
  cursor: pointer;
  transition: background-color 0.2s;
}
 
.select-option:hover {
  background-color: #f0f0f0;
}
 
.select-option.selected {
  background-color: #e6f7ff;
  color: #1890ff;
}
</style>