cl-picker-view.uvue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <template>
  2. <view class="cl-picker-view">
  3. <view class="cl-picker-view__header" v-if="headers.length > 0">
  4. <cl-text
  5. :pt="{
  6. className: 'flex-1 text-sm text-center'
  7. }"
  8. v-for="(label, index) in headers"
  9. :key="index"
  10. >{{ label }}</cl-text
  11. >
  12. </view>
  13. <view
  14. class="px-[10rpx]"
  15. :style="{
  16. height: parseRpx(height)
  17. }"
  18. >
  19. <picker-view
  20. class="h-full"
  21. :value="value"
  22. :mask-style="maskStyle"
  23. :mask-top-style="maskStyle"
  24. :mask-bottom-style="maskStyle"
  25. :indicator-style="indicatorStyle"
  26. @change="onChange"
  27. >
  28. <picker-view-column
  29. class="cl-select-popup__column"
  30. v-for="(column, columnIndex) in columns"
  31. :key="columnIndex"
  32. >
  33. <view
  34. class="cl-picker-view__item"
  35. :style="{
  36. height: `${itemHeight}px`
  37. }"
  38. v-for="(item, index) in column"
  39. :key="index"
  40. >
  41. <cl-text
  42. :pt="{
  43. className: parseClass([
  44. [isDark, 'text-surface-500'],
  45. [isDark && index == value[columnIndex], 'text-white']
  46. ])
  47. }"
  48. >{{ item.label }}</cl-text
  49. >
  50. </view>
  51. </picker-view-column>
  52. </picker-view>
  53. </view>
  54. </view>
  55. </template>
  56. <script setup lang="ts">
  57. import { forInObject, isAppIOS, isDark, isEqual, isNull, parseClass, rpx2px } from "@/cool";
  58. import type { ClSelectOption } from "../../types";
  59. import { parseRpx } from "@/cool";
  60. import { computed } from "vue";
  61. import type { PropType } from "vue";
  62. defineOptions({
  63. name: "cl-select-picker-view"
  64. });
  65. const props = defineProps({
  66. // 选择器表头
  67. headers: {
  68. type: Array as PropType<string[]>,
  69. default: () => []
  70. },
  71. // 选择器值
  72. value: {
  73. type: Array as PropType<number[]>,
  74. default: () => []
  75. },
  76. // 选择器选项
  77. columns: {
  78. type: Array as PropType<ClSelectOption[][]>,
  79. default: () => []
  80. },
  81. // 选择器选项高度
  82. itemHeight: {
  83. type: Number,
  84. default: 42
  85. },
  86. // 选择器高度
  87. height: {
  88. type: Number,
  89. default: 600
  90. }
  91. });
  92. const emit = defineEmits(["change-value", "change-index"]);
  93. // 获取窗口宽度,用于计算选择器列宽
  94. const { windowWidth } = uni.getWindowInfo();
  95. // 顶部显示表头
  96. const headers = computed(() => {
  97. return props.headers.slice(0, props.columns.length);
  98. });
  99. // 遮罩层样式
  100. const maskStyle = computed(() => {
  101. if (isDark.value) {
  102. if(isAppIOS()) {
  103. return `background-color: rgba(0, 0, 0, 0);`
  104. }
  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 indexs = e.detail.value;
  142. // 处理因快速滑动导致下级数据未及时渲染而产生的索引越界问题
  143. indexs.forEach((v, i, arr) => {
  144. if (i < props.columns.length) {
  145. const n = props.columns[i].length;
  146. if (v >= n) {
  147. arr[i] = n - 1;
  148. }
  149. }
  150. });
  151. // 相同值不触发事件
  152. if (isEqual(indexs, props.value)) {
  153. return;
  154. }
  155. // 获取所有列的值
  156. const values = props.columns.map((c, i) => {
  157. return isNull(c[indexs[i]]) ? 0 : c[indexs[i]].value;
  158. });
  159. // 返回所有列的值或下标
  160. emit("change-value", values);
  161. emit("change-index", indexs);
  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. }
  170. &__item {
  171. @apply flex flex-row items-center justify-center;
  172. }
  173. .uni-picker-view-indicator {
  174. // #ifdef H5
  175. &::after,
  176. &::before {
  177. display: none;
  178. }
  179. // #endif
  180. }
  181. }
  182. </style>