DropdownMenu.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. vue
  2. <template>
  3. <div class="dropdown-list-wrap">
  4. <div class="list-ite-wrap" :class="[{active: isActvie && item[defaultprop.label] === activeValue}]" :style="isActvie && item[defaultprop.label] === activeValue ? activeStyle : {}" v-for="(item, index) in list" :key="index" @click="selectHandle(item)">
  5. <span class="list-ite-label">{{ item.label }}</span>
  6. <span class="list-ite-value">{{ item.value }}</span>
  7. </div>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. name: 'DropdownMenu',
  13. props: {
  14. list: {
  15. type: Array,
  16. default: () => []
  17. },
  18. // 替换回显以及key字段
  19. prop: {
  20. type: Object,
  21. default: () => ({})
  22. },
  23. // 选中的值
  24. activeValue: '',
  25. // 下拉菜单是否激活选中项
  26. isActvie: {
  27. type: Boolean,
  28. default: false
  29. },
  30. // 下拉菜单选中时的样式
  31. activeStyle: {
  32. type: [Object, Array],
  33. default: () => ({})
  34. }
  35. },
  36. data() {
  37. return {
  38. // 默认的prop
  39. defaultprop: {
  40. label: 'label',
  41. value: 'value'
  42. },
  43. };
  44. },
  45. mounted() {
  46. // 如果有自定义的prop,那么就直接替换
  47. if (Object.keys(this.prop).length) Object.assign(this.defaultprop, this.prop)
  48. },
  49. methods: {
  50. selectHandle(item) {
  51. // 触发选择事件
  52. this.$emit('change', item);
  53. }
  54. }
  55. }
  56. </script>
  57. <style lang="scss" scoped>
  58. .dropdown-list-wrap{
  59. .list-ite-wrap{
  60. display: flex;
  61. padding: 12px 0;
  62. width: 110px;
  63. justify-content: space-between;
  64. }
  65. }
  66. .active {
  67. color: #3666F2;
  68. }
  69. </style>