pulldownMenuInEditor.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <div class="pull-down-menu-in-editor" v-clickoutside="onClickOutside">
  3. <button class="menu-cover" @click="isExpand = !isExpand">
  4. {{ placeholder ? placeholder : current.label }}
  5. <i
  6. class="iconfont icon-material_preview_upload_collect"
  7. :class="{ flip: isExpand }"
  8. ></i>
  9. </button>
  10. <div class="menu" v-show="isExpand">
  11. <button
  12. v-for="(item, index) of valueList"
  13. :key="index"
  14. @click="onSelect(item)"
  15. >
  16. {{ item.label }}
  17. </button>
  18. </div>
  19. </div>
  20. </template>
  21. <script>
  22. export default {
  23. props: {
  24. valueList: {
  25. type: Array,
  26. default: function () {
  27. return [];
  28. },
  29. },
  30. placeholder: {
  31. type: String,
  32. default: "",
  33. },
  34. value: {
  35. type: String,
  36. required: true,
  37. },
  38. },
  39. data() {
  40. return {
  41. isExpand: false,
  42. current: {
  43. id: "",
  44. label: "",
  45. },
  46. };
  47. },
  48. watch: {
  49. valueList: {
  50. handler(val) {
  51. if (val && val.length > 0) {
  52. if (!this.current.id) {
  53. this.current = this.valueList[0];
  54. }
  55. }
  56. },
  57. immediate: true,
  58. },
  59. value: {
  60. handler(val) {
  61. const changeValue = this.valueList.find((i) => i.id === val);
  62. if (changeValue) {
  63. this.current = changeValue;
  64. }
  65. },
  66. immediate: true,
  67. },
  68. },
  69. methods: {
  70. onClickOutside() {
  71. if (this.isExpand) {
  72. this.isExpand = false;
  73. }
  74. },
  75. onSelect(item) {
  76. console.log(item, "ads");
  77. this.isExpand = false;
  78. this.$emit("input", item.id);
  79. },
  80. },
  81. };
  82. </script>
  83. <style lang="less" scoped>
  84. button {
  85. background: #252526;
  86. border: 1px solid #404040;
  87. height: 36px;
  88. color: #fff;
  89. width: 100%;
  90. }
  91. .pull-down-menu-in-editor {
  92. width: 140px;
  93. position: relative;
  94. min-width: 160px;
  95. > button.menu-cover {
  96. display: flex;
  97. justify-content: space-between;
  98. align-items: center;
  99. padding: 16px;
  100. cursor: pointer;
  101. > .icon-material_preview_upload_collect {
  102. font-size: 11px;
  103. color: rgba(255, 255, 255, 0.6);
  104. border-radius: 2px;
  105. &.flip {
  106. transform: translateY(-2px) rotate(180deg);
  107. }
  108. }
  109. }
  110. > .menu {
  111. position: absolute;
  112. width: 100%;
  113. > button {
  114. display: block;
  115. border-top: none;
  116. cursor: pointer;
  117. text-align: left;
  118. padding: 0 16px;
  119. }
  120. }
  121. }
  122. </style>