cl-picker-view.uvue 3.8 KB

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