lj-button-menu-pro.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <div class="LjButtonMenu">
  3. <van-row gutter="4" type="flex" justify="space-between">
  4. <!-- 功能按钮 -->
  5. <van-col class="LjButtonMainCol">
  6. <van-row gutter="4" type="flex">
  7. <van-col v-for="(button, index) in mergeButtons()" :span="button.span || 6" :offset="button.offset || 0" :key="index">
  8. <van-popover
  9. v-model:show="popoverOpen"
  10. :actions="popoverList"
  11. placement="bottom"
  12. v-if="button.type === 'select'"
  13. >
  14. <template>
  15. <div class="van-popover__content">
  16. <div
  17. v-for="item in popoverList"
  18. class="van-popover__action"
  19. :class="item.icon ? 'van-popover__action--with-icon' : ''"
  20. @click="handleClickPopoverItem(item)"
  21. >
  22. <i :class="['iconfont',item.icon,item.icon ? 'van-icon van-popover__action-icon' : '']" v-if="item.icon" />
  23. <div class="van-popover__action-text van-hairline--bottom">{{ item.text }}</div>
  24. </div>
  25. </div>
  26. </template>
  27. <template #reference>
  28. <van-button
  29. block
  30. plain
  31. :text="getTextValue(button)"
  32. :type="getTypeValue(button)"
  33. :size="button.size || 'small'"
  34. @click="handleButtonClick(button)"
  35. class="CommonVanButtonStyle"
  36. >
  37. <template v-if="button.icon" slot="icon">
  38. <i :class="['iconfont', button.icon]" />
  39. </template>
  40. </van-button>
  41. </template>
  42. </van-popover>
  43. <van-button
  44. block
  45. plain
  46. :text="getTextValue(button)"
  47. :key="button.property"
  48. :type="getTypeValue(button)"
  49. :size="button.size || 'small'"
  50. @click="handleButtonClick(button)"
  51. class="CommonVanButtonStyle"
  52. v-else
  53. >
  54. <template v-if="button.icon" slot="icon">
  55. <i :class="['iconfont', button.icon]" />
  56. </template>
  57. </van-button>
  58. </van-col>
  59. </van-row>
  60. </van-col>
  61. <!-- 更多按钮 -->
  62. <van-col>
  63. <van-popover v-model:show="popoverVisible" placement="bottom-end">
  64. <template>
  65. <div class="van-popover__content">
  66. <div
  67. v-for="button in [...defaultMoreButton,...moreButtons]"
  68. class="van-popover__action"
  69. :class="button.icon ? 'van-popover__action--with-icon' : ''"
  70. @click="handleButtonClick(button)"
  71. >
  72. <i :class="['iconfont',button.icon,button.icon ? 'van-icon van-popover__action-icon' : '']" v-if="button.icon" />
  73. <div class="van-popover__action-text van-hairline--bottom">{{ button.text }}</div>
  74. </div>
  75. </div>
  76. </template>
  77. <template #reference>
  78. <van-button
  79. block
  80. plain
  81. size="small"
  82. @click="togglePopover"
  83. class="CommonVanButtonStyle"
  84. >
  85. <template slot="icon">
  86. <i class="iconfont icon-more" />
  87. </template>
  88. </van-button>
  89. </template>
  90. </van-popover>
  91. </van-col>
  92. </van-row>
  93. </div>
  94. </template>
  95. <script>
  96. export default {
  97. props: {
  98. mainButtons: {
  99. type: Array,
  100. default: () => [
  101. { color: 'primary', type: 'button', property: 'add', text: '增加', icon: 'icon-ic_add' },
  102. { color: 'danger', type: 'button', property: 'delete', text: '删除', icon: 'icon-close' }
  103. ]
  104. },
  105. moreButtons: {
  106. type: Array,
  107. default: () => []
  108. }
  109. },
  110. data() {
  111. return {
  112. popoverVisible: false,
  113. defaultMoreButton: [
  114. { property: 'setting', text: '布局', icon: 'icon-settings' }
  115. ],
  116. popoverOpen: false,
  117. popoverList: []
  118. };
  119. },
  120. methods: {
  121. getTypeValue(button) {
  122. return button.color || "default";
  123. },
  124. getTextValue(button) {
  125. return button.text || "";
  126. },
  127. togglePopover() {
  128. this.popoverVisible = !this.popoverVisible;
  129. },
  130. handleButtonClick(button) {
  131. this.$emit('button-click', button);
  132. },
  133. handleLayoutButtonClick() {
  134. if (this.MergeCustomSetting) {
  135. this.MergeCustomSetting();
  136. } else {
  137. this.defaultMergeCustomSetting();
  138. }
  139. },
  140. getDefaultButtons() {
  141. return [
  142. { color: 'primary', type: 'button', property: 'add', text: '增加', icon: 'icon-ic_add' },
  143. { color: 'danger', type: 'button', property: 'delete', text: '删除', icon: 'icon-close' }
  144. ];
  145. },
  146. mergeButtons() {
  147. const defaultButtons = this.getDefaultButtons();
  148. const mainButtons = this.mainButtons;
  149. if(mainButtons && mainButtons.length > 0) {
  150. this.menuButtons = [...mainButtons];
  151. } else {
  152. this.menuButtons = [...defaultButtons];
  153. }
  154. return mainButtons;
  155. },
  156. handleOpenPopover(list) {
  157. this.popoverList = list
  158. this.popoverOpen = true;
  159. },
  160. handleClosePopover() {
  161. this.popoverOpen = false;
  162. },
  163. handleClickPopoverItem(item) {
  164. this.$emit("callPopoverFunction",item)
  165. },
  166. }
  167. };
  168. </script>
  169. <style lang="less" scoped>
  170. .van-popover__wrapper {
  171. width: 100%;
  172. }
  173. .CommonVanButtonStyle {
  174. background: transparent;
  175. }
  176. .iconfont {
  177. font-size: 14px;
  178. }
  179. .LjButtonMainCol {
  180. flex-grow: 1;
  181. }
  182. </style>