cl-picker-view.uvue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <template>
  2. <view class="cl-picker-view">
  3. <view class="cl-picker-view__header" v-if="headers.length > 0">
  4. <text
  5. class="cl-picker-view__header-item dark:!text-white"
  6. v-for="(label, index) in headers"
  7. :key="index"
  8. >{{ label }}</text
  9. >
  10. </view>
  11. <view
  12. class="px-[10rpx]"
  13. :style="{
  14. height: parseRpx(height)
  15. }"
  16. >
  17. <picker-view
  18. class="h-full"
  19. :value="value"
  20. :mask-style="maskStyle"
  21. :mask-top-style="maskStyle"
  22. :mask-bottom-style="maskStyle"
  23. :indicator-style="indicatorStyle"
  24. @change="onChange"
  25. >
  26. <picker-view-column
  27. class="cl-select-popup__column"
  28. v-for="(column, columnIndex) in columns"
  29. :key="columnIndex"
  30. >
  31. <view
  32. class="cl-picker-view__item"
  33. :style="{
  34. height: `${itemHeight}px`
  35. }"
  36. v-for="(item, index) in column"
  37. :key="index"
  38. >
  39. <text
  40. class="cl-picker-view__item-text"
  41. :class="[
  42. {
  43. 'is-active': index == value[columnIndex],
  44. 'is-dark': isDark
  45. }
  46. ]"
  47. >{{ item.label }}</text
  48. >
  49. </view>
  50. </picker-view-column>
  51. </picker-view>
  52. </view>
  53. </view>
  54. </template>
  55. <script setup lang="ts">
  56. import { forInObject, isDark, rpx2px } from "@/cool";
  57. import type { ClSelectOption } from "../../types";
  58. import { parseRpx } from "@/cool";
  59. import { computed } from "vue";
  60. import type { PropType } from "vue";
  61. defineOptions({
  62. name: "cl-select-picker-view"
  63. });
  64. const props = defineProps({
  65. // 选择器表头
  66. headers: {
  67. type: Array as PropType<string[]>,
  68. default: () => []
  69. },
  70. // 选择器值
  71. value: {
  72. type: Array as PropType<number[]>,
  73. default: () => []
  74. },
  75. // 选择器选项
  76. columns: {
  77. type: Array as PropType<ClSelectOption[][]>,
  78. default: () => []
  79. },
  80. // 选择器选项高度
  81. itemHeight: {
  82. type: Number,
  83. default: 42
  84. },
  85. // 选择器高度
  86. height: {
  87. type: Number,
  88. default: 600
  89. },
  90. // 选择后子集是否回到0
  91. resetOnChange: {
  92. type: Boolean,
  93. default: true
  94. }
  95. });
  96. const emit = defineEmits(["change"]);
  97. // 获取窗口宽度,用于计算选择器列宽
  98. const { windowWidth } = uni.getWindowInfo();
  99. // 顶部显示表头
  100. const headers = computed(() => {
  101. return props.headers.slice(0, props.columns.length);
  102. });
  103. // 遮罩层样式
  104. const maskStyle = computed(() => {
  105. if (isDark.value) {
  106. return `background-image: linear-gradient(
  107. 180deg,
  108. rgba(0, 0, 0, 0),
  109. rgba(0, 0, 0, 0)
  110. )`;
  111. }
  112. return "";
  113. });
  114. // 计算选择器列样式
  115. const indicatorStyle = computed(() => {
  116. // 根据窗口宽度和列数计算每列宽度
  117. const width = ((windowWidth - rpx2px(20)) / props.columns.length - rpx2px(2) - 8).toFixed(0);
  118. let str = "";
  119. // 选择器列样式配置
  120. const style = {
  121. height: `${props.itemHeight}px`,
  122. width: `${width}px`,
  123. left: "4px",
  124. backgroundColor: "rgba(10, 10, 10, 0.04)",
  125. borderRadius: "10px",
  126. border: "1rpx solid rgba(10, 10, 10, 0.2)"
  127. };
  128. // 深色模式
  129. if (isDark.value) {
  130. style.backgroundColor = "rgba(0, 0, 0, 0.1)";
  131. style.border = "1rpx solid rgba(255, 255, 255, 0.3)";
  132. }
  133. // 构建样式字符串
  134. forInObject(style, (value, key) => {
  135. str += `${key}: ${value};`;
  136. });
  137. return str;
  138. });
  139. // 选择器值改变事件
  140. function onChange(e: UniPickerViewChangeEvent) {
  141. // 获取选择器当前选中值数组
  142. const arr = e.detail.value;
  143. // 如果选择后子集是否回到0
  144. if (props.resetOnChange) {
  145. // 记录第一个发生变化的列的索引,初始为-1表示未发生变化
  146. let start = -1;
  147. // 遍历原选中值数组
  148. props.value.forEach((e, i) => {
  149. if (start >= 0) {
  150. // 如果之前的列发生过变化,后续列都重置为0
  151. arr[i] = 0;
  152. } else {
  153. // 比较当前列的值是否发生变化
  154. if (e != arr[i]) {
  155. // 记录第一个变化的列的索引
  156. start = i;
  157. }
  158. }
  159. });
  160. }
  161. // 触发change事件,传递新的选中值数组
  162. emit("change", arr);
  163. }
  164. </script>
  165. <style lang="scss" scoped>
  166. .cl-picker-view {
  167. @apply w-full h-full;
  168. &__header {
  169. @apply flex flex-row items-center py-4;
  170. &-item {
  171. @apply text-center text-sm text-surface-700;
  172. flex: 1;
  173. }
  174. }
  175. &__item {
  176. @apply flex flex-row items-center justify-center;
  177. &-text {
  178. @apply text-md text-center text-surface-700;
  179. &.is-dark {
  180. @apply text-surface-500;
  181. &.is-active {
  182. @apply text-white;
  183. }
  184. }
  185. }
  186. }
  187. .uni-picker-view-indicator {
  188. // #ifdef H5
  189. &::after,
  190. &::before {
  191. display: none;
  192. }
  193. // #endif
  194. }
  195. }
  196. </style>