cl-picker-view.uvue 4.2 KB

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