MultiSelectPopup.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <view v-if="visible" class="popup-overlay" @click.self="closePopup">
  3. <view class="popup-content" @click.stop>
  4. <!-- Parent Categories as Tabs -->
  5. <view class="parent-tabs">
  6. <scroll-view scroll-x class="tabs-scroll" :show-scrollbar="false"
  7. :scroll-into-view="'tab-' + activeParentIndex - 1" scroll-with-animation>
  8. <view v-for="(parent, index) in options" :key="index" :id="'tab-' + index" class="tab-item"
  9. :class="{ 'active': activeParentIndex === index }" @click="setActiveParent(index)">
  10. {{ parent.label }}
  11. </view>
  12. </scroll-view>
  13. </view>
  14. <!-- Child Items as Tags in a Grid -->
  15. <scroll-view scroll-y class="child-tags-container">
  16. <view class="tags-grid" v-if="activeParentIndex !== null && currentChildOptions.length > 0">
  17. <view v-for="(child, cIndex) in currentChildOptions" :key="child.value + '-' + cIndex"
  18. class="tag-item" :class="{ 'selected': selectedChildValues.includes(child.value) }"
  19. @click="selectChild(child)">
  20. {{ child.label }}
  21. </view>
  22. </view>
  23. <view v-else-if="activeParentIndex !== null && currentChildOptions.length === 0"
  24. class="no-children-text">
  25. 暂无子选项
  26. </view>
  27. </scroll-view>
  28. <!-- Confirm Button -->
  29. <view class="popup-footer">
  30. <view class="confirm-btn" @click="confirmSelection">确认</view>
  31. </view>
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. export default {
  37. name: 'MultiSelectPopup',
  38. props: {
  39. initialOptions: {
  40. type: Array,
  41. default: () => []
  42. },
  43. initialSelectedValues: { // Optional: to pre-select items
  44. type: Array,
  45. default: () => []
  46. }
  47. },
  48. data() {
  49. return {
  50. visible: false,
  51. options: [],
  52. activeParentIndex: null,
  53. selectedChildValues: [] // Changed to array for multi-select
  54. };
  55. },
  56. computed: {
  57. currentChildOptions() {
  58. if (this.activeParentIndex !== null && this.options[this.activeParentIndex] && this.options[this.activeParentIndex].children) {
  59. return this.options[this.activeParentIndex].children;
  60. }
  61. return [];
  62. }
  63. },
  64. watch: {
  65. initialOptions: {
  66. immediate: true,
  67. handler(newVal) {
  68. this.options = JSON.parse(JSON.stringify(newVal));
  69. // Ensure selectedChildValues is initialized from props when options load
  70. this.selectedChildValues = [...this.initialSelectedValues];
  71. if (this.options.length > 0) {
  72. let preselectedParentIndex = 0;
  73. // Find the first parent that has any of the initially selected children
  74. for (let i = 0; i < this.options.length; i++) {
  75. const parent = this.options[i];
  76. if (parent.children && parent.children.some(c => this.initialSelectedValues.includes(c.value))) {
  77. preselectedParentIndex = i;
  78. break;
  79. }
  80. }
  81. this.activeParentIndex = preselectedParentIndex;
  82. } else {
  83. this.activeParentIndex = null;
  84. }
  85. }
  86. }
  87. },
  88. methods: {
  89. openPopup() {
  90. this.options = JSON.parse(JSON.stringify(this.initialOptions));
  91. this.selectedChildValues = [...this.initialSelectedValues];
  92. if (this.options.length > 0) {
  93. let preselectedParentIndex = 0;
  94. for (let i = 0; i < this.options.length; i++) {
  95. const parent = this.options[i];
  96. if (parent.children && parent.children.some(c => this.selectedChildValues.includes(c.value))) {
  97. preselectedParentIndex = i;
  98. break;
  99. }
  100. }
  101. this.activeParentIndex = (this.options.length > 0) ? preselectedParentIndex : null;
  102. } else {
  103. this.activeParentIndex = null;
  104. }
  105. this.visible = true;
  106. },
  107. closePopup() {
  108. this.visible = false;
  109. this.$emit('popup-closed');
  110. },
  111. setActiveParent(parentIndex) {
  112. this.activeParentIndex = parentIndex;
  113. },
  114. selectChild(child) {
  115. const index = this.selectedChildValues.indexOf(child.value);
  116. if (index > -1) {
  117. this.selectedChildValues.splice(index, 1);
  118. } else {
  119. this.selectedChildValues.push(child.value);
  120. }
  121. },
  122. confirmSelection() {
  123. if (this.selectedChildValues.length > 0) {
  124. this.$emit('selection-confirmed', [...this.selectedChildValues]);
  125. this.closePopup();
  126. } else {
  127. uni.showToast({
  128. title: '请至少选择一个选项',
  129. icon: 'none'
  130. });
  131. }
  132. }
  133. }
  134. };
  135. </script>
  136. <style lang="scss" scoped>
  137. .popup-overlay {
  138. position: fixed;
  139. top: 0;
  140. left: 0;
  141. width: 100%;
  142. height: 100%;
  143. background-color: rgba(0, 0, 0, 0.7); // Darker overlay
  144. display: flex;
  145. align-items: flex-end;
  146. justify-content: center;
  147. z-index: 1000;
  148. }
  149. .popup-content {
  150. background:url("../../static/makedetail/ai-bg.jpg") center top/100% 100%;
  151. width: 100%;
  152. max-height: 70vh; // Increased height
  153. min-height: 40vh;
  154. border-top-left-radius: 20rpx;
  155. border-top-right-radius: 20rpx;
  156. display: flex;
  157. flex-direction: column;
  158. animation: slideUp 0.3s ease-out;
  159. padding: 0 30rpx; // Add horizontal padding to content
  160. box-sizing: border-box;
  161. border-radius: 28rpx 28rpx 0 0;
  162. }
  163. @keyframes slideUp {
  164. from {
  165. transform: translateY(100%);
  166. }
  167. to {
  168. transform: translateY(0);
  169. }
  170. }
  171. .parent-tabs {
  172. width: 100%;
  173. border-bottom: 1rpx solid rgba(255, 255, 255, 0.1);
  174. margin-bottom: 20rpx;
  175. padding-top: 30rpx; // Space at the top
  176. .tabs-scroll {
  177. white-space: nowrap;
  178. width: 100%;
  179. }
  180. .tab-item {
  181. display: inline-block;
  182. padding: 20rpx 30rpx;
  183. font-size: 28rpx;
  184. color: rgba(255, 255, 255, 0.7);
  185. cursor: pointer;
  186. position: relative;
  187. &.active {
  188. color: #FFFFFF;
  189. font-weight: bold;
  190. &::after {
  191. content: '';
  192. position: absolute;
  193. bottom: 0;
  194. left: 50%;
  195. transform: translateX(-50%);
  196. width: 40rpx;
  197. height: 6rpx;
  198. background-color: #FFFFFF;
  199. border-radius: 3rpx;
  200. }
  201. }
  202. }
  203. }
  204. .child-tags-container {
  205. flex: 1;
  206. overflow-y: auto;
  207. padding-bottom: 20rpx;
  208. .tags-grid {
  209. display: flex;
  210. flex-wrap: wrap;
  211. gap: 20rpx; // Spacing between tags
  212. }
  213. .tag-item {
  214. background-color: rgba(255, 255, 255, 0.1);
  215. color: rgba(255, 255, 255, 0.8);
  216. padding: 15rpx 30rpx;
  217. font-size: 26rpx;
  218. border-radius: 40rpx;
  219. cursor: pointer;
  220. border: 2rpx solid transparent;
  221. transition: all 0.2s ease;
  222. text-align: center;
  223. min-width: 120rpx; // Ensure tags have some minimum width
  224. box-sizing: border-box;
  225. border: 2rpx solid transparent;
  226. &.selected {
  227. border-color: #ACF934;
  228. color: #ACF934;
  229. }
  230. }
  231. }
  232. .no-children-text {
  233. padding: 40rpx;
  234. text-align: center;
  235. color: rgba(255, 255, 255, 0.5);
  236. font-size: 26rpx;
  237. }
  238. .popup-footer {
  239. display: flex;
  240. justify-content: center;
  241. align-items: center;
  242. padding: 30rpx 0;
  243. border-top: 1rpx solid rgba(255, 255, 255, 0.1);
  244. .confirm-btn {
  245. background:url("../../static/makedetail/ai-btn-bg.png") center /100% 100%;
  246. color: #FFFFFF;
  247. font-size: 32rpx;
  248. font-weight: bold;
  249. text-align: center;
  250. width: 100%;
  251. max-width: 600rpx;
  252. padding: 25rpx 0;
  253. border-radius: 50rpx;
  254. cursor: pointer;
  255. transition: opacity 0.2s ease;
  256. &:active {
  257. opacity: 0.8;
  258. }
  259. }
  260. }
  261. </style>