cl-picker-view.uvue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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, isDark, 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. return `background-image: linear-gradient(
  103. 180deg,
  104. rgba(0, 0, 0, 0),
  105. rgba(0, 0, 0, 0)
  106. )`;
  107. }
  108. return "";
  109. });
  110. // 计算选择器列样式
  111. const indicatorStyle = computed(() => {
  112. // 根据窗口宽度和列数计算每列宽度
  113. const width = ((windowWidth - rpx2px(20)) / props.columns.length - rpx2px(2) - 8).toFixed(0);
  114. let str = "";
  115. // 选择器列样式配置
  116. const style = {
  117. height: `${props.itemHeight}px`,
  118. width: `${width}px`,
  119. left: "4px",
  120. backgroundColor: "rgba(10, 10, 10, 0.04)",
  121. borderRadius: "10px",
  122. border: "1rpx solid rgba(10, 10, 10, 0.2)"
  123. };
  124. // 深色模式
  125. if (isDark.value) {
  126. style.backgroundColor = "rgba(0, 0, 0, 0.1)";
  127. style.border = "1rpx solid rgba(255, 255, 255, 0.3)";
  128. }
  129. // 构建样式字符串
  130. forInObject(style, (value, key) => {
  131. str += `${key}: ${value};`;
  132. });
  133. return str;
  134. });
  135. // 监听选择器值改变事件
  136. function onChange(e: UniPickerViewChangeEvent) {
  137. // 获取选择器当前选中值数组
  138. const indexs = e.detail.value;
  139. // 获取所有列的值
  140. const values = props.columns.map((c, i) => {
  141. return c[indexs[i]].value;
  142. });
  143. // 返回所有列的值或下标
  144. emit("change-value", values);
  145. emit("change-index", indexs);
  146. }
  147. </script>
  148. <style lang="scss" scoped>
  149. .cl-picker-view {
  150. @apply w-full h-full;
  151. &__header {
  152. @apply flex flex-row items-center py-4;
  153. }
  154. &__item {
  155. @apply flex flex-row items-center justify-center;
  156. }
  157. .uni-picker-view-indicator {
  158. // #ifdef H5
  159. &::after,
  160. &::before {
  161. display: none;
  162. }
  163. // #endif
  164. }
  165. }
  166. </style>