wk
2024-06-14 8346b6dc91ab5f93d83532451dc44b8ab6c2cf80
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<template>
    <div class="drop-down-search" @click.stop>
      <view style="display: flex; align-items: center;">
          <u--input 
        @focus="handleFocus" 
        @blur="closeDropDown"  
        :style="{height: '100%', paddingRight: clear? '60rpx' : '16rpx', display: 'flex'  ,padding: '6px 9px', marginLeft:'8px'}"
        v-model="searchText" 
        @input="handleSearch" 
              clearable
              maxlength="32"
              type="text"
               :placeholder="placeholder"
              prefixIcon="account"
              prefixIconStyle="font-size: 22px;color: #909399"></u--input>
        <text v-if="searchText.length && clear" @click="handleClearInput" class="iconfont icon-shanchu"></text>
        <view 
          @click="handleDropDownBox" 
          v-if="icon" 
          :class="['dropdown-container', { rotated:  isDropDownVisible}]"
        ></view> <!-- 添加小三角图标 -->
      </view>
 
      <scroll-view 
        :scroll-y="true" 
        class="dropdown-scroll" 
        :style="{ maxHeight: (isDropDownVisible && hasOptions) ? dropdownHeight + 'rpx' : '0rpx' }">
        <div class="dropdown-content">
          <div v-for="(option, index) in filteredOptions" :key="index" class="option" @click="selectOption(option, $event)">
            {{ option.username }}
          </div>
        </div>
      </scroll-view>
 
    </div>
  </template>
  
  <script>
  export default {
    props: {
      options: {
        type: Array,
        default: []
      },
      value: {
        type: [String,  Number], 
        default: ''
      },
      dropdownHeight: {  // 下拉框的高度
        type: Number,
        default: 200
      },
      placeholder: {
        type: String,
        default: ''
      },
      icon: {
        type: Boolean,
        default: true
      },
      clear: {
        type: Boolean,
        default: true,
      }
    },
    data() {
      return {
        searchText: this.value.toString(), // 默认将数字转字符串
        isDropDownVisible: false,
        hasOptions: false,
      };
    },
    computed: {
      // 根据搜索文本过滤选项
      filteredOptions() {
        return this.options.filter(option =>{
            if(this.searchText.username){
                return  option.username.toLowerCase().includes(this.searchText.username.toLowerCase())
            }else{
                return     option.username.toLowerCase().includes(this.searchText.toLowerCase())
            }
        }
        );
      }
    },
    watch: {  // 监听 options 数组,要是输入的东西没有,就关闭下拉框
        searchText() {
          this.hasOptions = (this.filteredOptions.length > 0)
        },
    },
    mounted() {
        // 添加点击外部关闭下拉框的事件监听器
        // document.addEventListener('click', this.closeDropDown.bind(this));
    },
    methods: {
      handleClearInput() { // 清除 input 输入框内容
        this.searchText = ''
        this.$emit('input',''); // 向父组件传递选中的值 // 重新获取新的数据
      },
      handleDropDownBox() { // 控制三角图表事件
        this.isDropDownVisible = !this.isDropDownVisible
        this.hasOptions = (this.filteredOptions.length > 0)
      },
      // 处理搜索事件
      handleSearch(e) {
        this.$emit('input', e); // 向父组件传递选中的值
        this.isDropDownVisible = this.hasOptions;
      },
      handleFocus() {
        this.isDropDownVisible = true;
        this.hasOptions = true;
      },
      // 选择选项
      selectOption(option, event) {
        event.stopPropagation();
        // 处理选项选择逻辑,比如触发事件、更新绑定值等
        this.searchText = option.username; // 选中选项后更新搜索文本
        this.isDropDownVisible = false; // 隐藏下拉框
        this.hasOptions = false;
        this.$emit('changeInput', option); // 向父组件传递选中的值
      },
      // // 获取当前输入的值
      // getSearchText() { 
      //   return this.searchText;
      // },
      closeDropDown() {
        // 点击事件的目标不包含搜索框或下拉框时,隐藏下拉框
        // this.isDropDownVisible = true;
        // this.hasOptions = true;
        if (!this.$el||!this.$el.contains(event.target)) {
          this.isDropDownVisible = false;
        }
      }
    }
  };
  </script>
  
  <style scoped>
  .drop-down-search {
    position: relative;
    height: 100% !important;
  }
  
  .dropdown-scroll {
    position: absolute;
    top: calc(100% + 12rpx);
    left: 0;
    width: 100%;
    background: #fff;
    z-index: 99;
    /* border: 1rpx solid #ccc; */
    box-shadow: 0 14rpx 30rpx rgb(0, 0, 0, 0.1);
    transition: max-height 0.3s ease;
  }
  
  .dropdown-content {
    padding: 10rpx;
  }
  
  .option {
    padding: 10rpx;
    cursor: pointer;
    border-bottom: 1rpx solid #ccc;
  }
  
  input {
    width: 100%;
    padding: 10rpx;
    box-sizing: border-box;
  }
 
  .iconfont {
    position: absolute;
    top: 50%;
    right: 55rpx;
    font-size: 50rpx;
    transform: translateY(-50%);
    font-size: 20rpx;
    color: #999;
    cursor: pointer;
  }
  .dropdown-container {  
    position: relative;  
    width: 30rpx; 
    height: 30rpx; 
    margin-right: 10rpx;
    transform:  rotate(180deg); 
  }  
  
  .dropdown-container::before {  
    content: '';  
    position: absolute;  
    top: 50%;  
    left: 50%;  
    transform: translate(-50%, -50%); 
    width: 0;  
    height: 0;  
    border-left: 15rpx solid transparent;  
    border-right: 15rpx solid transparent;  
    border-bottom: 15rpx solid #ccc; /* 使用border-bottom创建向下的三角形 */  
    transition: transform 0.3s ease; /* 添加过渡效果 */  
  }  
    
  .rotated::before {  
    transform: translate(-50%, -50%) rotate(180deg); /* 旋转三角形 */  
  } 
  </style>