zhengyiming
12 小时以前 d7c8a3a9e1fc5c8e596a17cdadb7079d20e52297
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
<!--
 * @Author: 秦少卫
 * @Date: 2024-06-11 16:04:59
 * @LastEditors: 秦少卫
 * @LastEditTime: 2024-06-12 16:37:40
 * @Description: 搜索组件
-->
 
<template>
  <div class="search-box">
    <Button
      type="text"
      class="back-btn"
      @click="clear"
      v-if="typeValue || searchKeyWord"
      icon="ios-arrow-back"
    ></Button>
 
    <Select class="select" v-model="typeValue" :disabled="loading" @on-change="change">
      <Option v-for="item in typeList" :value="item.value" :key="item.value">
        {{ item.label }}
      </Option>
    </Select>
    <Input
      class="input"
      :placeholder="`在${typeText}中搜索`"
      v-model="searchKeyWord"
      :disabled="loading"
      clearable
      @on-change="inputChange"
      @on-search="change"
      search
    />
  </div>
</template>
 
<script name="ImportJson" setup>
import { debounce } from 'lodash-es';
 
const props = defineProps({
  typeListApi: {
    type: Function,
    Object: () => ({}),
  },
});
const emit = defineEmits(['change']);
 
const loading = ref(false);
 
const typeValue = ref('');
const searchKeyWord = ref('');
const typeList = ref([]);
const typeText = computed(() => {
  const info = typeList.value.find((item) => item.value === typeValue.value);
  return info?.label || '全部';
});
 
onMounted(async () => {
  loading.value = true;
 
  try {
    const res = await props.typeListApi();
    const list = res.data.data.map((item) => {
      return {
        value: item.id,
        label: item.attributes.name,
      };
    });
    typeList.value = [
      {
        label: '全部',
        value: '',
      },
      ...list,
    ];
  } catch (error) {
    console.log(error);
  }
 
  loading.value = false;
});
 
const change = () => {
  emit('change', { searchKeyWord: searchKeyWord.value, typeValue: typeValue.value });
};
 
// const getValue = () => {
//   return { type: typeValue.value, searchKeyWord };
// };
const clear = () => {
  typeValue.value = '';
  searchKeyWord.value = '';
  change();
};
 
const setType = (type) => {
  typeValue.value = type;
};
 
const inputChange = debounce(change, 300);
 
defineExpose({
  setType,
});
</script>
<style scoped lang="less">
.search-box {
  padding-top: 10px;
  display: flex;
 
  .back-btn {
    margin-right: 10px;
  }
  .input {
    margin-left: 10px;
  }
  .select {
    width: 100px;
  }
}
</style>