cl-picker-view.uvue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. :immediate-change="true"
  26. :indicator-style="indicatorStyle"
  27. @change="onChange"
  28. >
  29. <picker-view-column
  30. class="cl-select-popup__column"
  31. v-for="(column, columnIndex) in columns"
  32. :key="columnIndex"
  33. >
  34. <!-- #ifdef APP-ANDROID -->
  35. <view
  36. ref="columnItemRef"
  37. :style="{
  38. height: `${itemHeight * column.length}px`
  39. }"
  40. android-layer-type="hardware"
  41. ></view>
  42. <!-- #endif -->
  43. <!-- #ifndef APP-ANDROID -->
  44. <view
  45. class="cl-picker-view__item"
  46. :style="{
  47. height: `${itemHeight}px`
  48. }"
  49. v-for="(item, index) in column"
  50. :key="index"
  51. >
  52. <cl-text
  53. :pt="{
  54. className: parseClass([
  55. [isDark, 'text-surface-500'],
  56. [isDark && index == value[columnIndex], 'text-white']
  57. ])
  58. }"
  59. >{{ item.label }}</cl-text
  60. >
  61. </view>
  62. <!-- #endif -->
  63. </picker-view-column>
  64. </picker-view>
  65. </view>
  66. </view>
  67. </template>
  68. <script setup lang="ts">
  69. import {
  70. forInObject,
  71. getColor,
  72. isAppIOS,
  73. isDark,
  74. isEqual,
  75. isNull,
  76. parseClass,
  77. rpx2px,
  78. parseRpx
  79. } from "@/cool";
  80. import type { ClSelectOption } from "../../types";
  81. import { computed, nextTick, onMounted, shallowRef, watch } from "vue";
  82. import type { PropType } from "vue";
  83. import { useSize } from "../../hooks";
  84. defineOptions({
  85. name: "cl-select-picker-view"
  86. });
  87. const props = defineProps({
  88. // 选择器表头
  89. headers: {
  90. type: Array as PropType<string[]>,
  91. default: () => []
  92. },
  93. // 选择器值
  94. value: {
  95. type: Array as PropType<number[]>,
  96. default: () => []
  97. },
  98. // 选择器选项
  99. columns: {
  100. type: Array as PropType<ClSelectOption[][]>,
  101. default: () => []
  102. },
  103. // 选择器选项高度
  104. itemHeight: {
  105. type: Number,
  106. default: isAppIOS() ? 50 : 42
  107. },
  108. // 选择器高度
  109. height: {
  110. type: Number,
  111. default: 600
  112. }
  113. });
  114. const emit = defineEmits(["change-value", "change-index"]);
  115. const { getScale } = useSize();
  116. // 获取窗口宽度,用于计算选择器列宽
  117. const { windowWidth } = uni.getWindowInfo();
  118. // 顶部显示表头
  119. const headers = computed(() => {
  120. return props.headers.slice(0, props.columns.length);
  121. });
  122. // 遮罩层样式
  123. const maskStyle = computed(() => {
  124. if (isDark.value) {
  125. if (isAppIOS()) {
  126. return `background-color: rgba(0, 0, 0, 0);`;
  127. }
  128. return `background-image: linear-gradient(
  129. 180deg,
  130. rgba(0, 0, 0, 0),
  131. rgba(0, 0, 0, 0)
  132. )`;
  133. }
  134. return "";
  135. });
  136. // 计算选择器列样式
  137. const indicatorStyle = computed(() => {
  138. // 根据窗口宽度和列数计算每列宽度
  139. const width = ((windowWidth - rpx2px(20)) / props.columns.length - rpx2px(2) - 8).toFixed(0);
  140. let str = "";
  141. // 选择器列样式配置
  142. const style = {
  143. height: `${props.itemHeight}px`,
  144. width: `${width}px`,
  145. left: "4px",
  146. backgroundColor: "rgba(10, 10, 10, 0.04)",
  147. borderRadius: "10px",
  148. border: "1rpx solid rgba(10, 10, 10, 0.2)"
  149. };
  150. // 深色模式
  151. if (isDark.value) {
  152. style.backgroundColor = "rgba(0, 0, 0, 0.1)";
  153. style.border = "1rpx solid rgba(255, 255, 255, 0.3)";
  154. }
  155. // 构建样式字符串
  156. forInObject(style, (value, key) => {
  157. str += `${key}: ${value};`;
  158. });
  159. return str;
  160. });
  161. // 监听选择器值改变事件
  162. function onChange(e: UniPickerViewChangeEvent) {
  163. // 获取选择器当前选中值数组
  164. const indexs = e.detail.value;
  165. // 处理因快速滑动导致下级数据未及时渲染而产生的索引越界问题
  166. indexs.forEach((v, i, arr) => {
  167. if (i < props.columns.length) {
  168. const n = props.columns[i].length;
  169. if (v >= n) {
  170. arr[i] = n - 1;
  171. }
  172. }
  173. });
  174. // 相同值不触发事件
  175. if (isEqual(indexs, props.value)) {
  176. return;
  177. }
  178. // 获取所有列的值
  179. const values = props.columns.map((c, i) => {
  180. return isNull(c[indexs[i]]) ? 0 : c[indexs[i]].value;
  181. });
  182. // 返回所有列的值或下标
  183. emit("change-value", values);
  184. emit("change-index", indexs);
  185. }
  186. // 列项ref引用
  187. const columnItemRef = shallowRef<UniElement[]>([]);
  188. // 渲染列项内容
  189. const renderColumnItem = () => {
  190. // 当前全局或默认字体大小
  191. const fontSize = getScale() * 14;
  192. // 根据主题切换字体颜色(深色/浅色)
  193. const color = isDark.value ? "white" : getColor("surface-700");
  194. // 遍历所有 picker-view-column 的ref,与每列数据一一对应
  195. for (let i = 0; i < columnItemRef.value.length; i++) {
  196. const column = props.columns[i]; // 当前列的数据
  197. const dom = columnItemRef.value[i]; // 当前列对应的DOM节点
  198. // 获取节点尺寸,用于文本居中计算
  199. const rect = dom!.getBoundingClientRect();
  200. // 获取原生画布上下文
  201. const ctx = dom!.getDrawableContext()!;
  202. ctx.reset(); // 每次重绘前需先重置画布
  203. ctx.textAlign = "center";
  204. // 计算文本水平方向的中点位置
  205. const x = rect.width / 2;
  206. // 循环绘制本列的每一个选项
  207. for (let j = 0; j < column.length; j++) {
  208. ctx.fillStyle = color; // 设置文本颜色
  209. ctx.font = `${fontSize}px`;
  210. // 计算每行文本的垂直位置,使其垂直居中于item元素内
  211. const y = 12 + (props.itemHeight - fontSize) / 2 + props.itemHeight * j;
  212. ctx.fillText(column[j].label, x, y); // 绘制文本
  213. }
  214. ctx.update(); // 完成当前列绘制,刷到视图
  215. }
  216. };
  217. onMounted(() => {
  218. nextTick(() => {
  219. renderColumnItem();
  220. });
  221. watch(
  222. computed(() => [isDark.value, props.columns, props.itemHeight]),
  223. () => {
  224. renderColumnItem();
  225. }
  226. );
  227. });
  228. </script>
  229. <style lang="scss" scoped>
  230. .cl-picker-view {
  231. @apply w-full h-full;
  232. &__header {
  233. @apply flex flex-row items-center py-4;
  234. }
  235. &__item {
  236. @apply flex flex-row items-center justify-center;
  237. }
  238. .uni-picker-view-indicator {
  239. // #ifdef H5
  240. &::after,
  241. &::before {
  242. display: none;
  243. }
  244. // #endif
  245. }
  246. }
  247. </style>