| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- <template>
- <view class="cl-picker-view">
- <view class="cl-picker-view__header" v-if="headers.length > 0">
- <cl-text
- :pt="{
- className: 'flex-1 text-sm text-center'
- }"
- v-for="(label, index) in headers"
- :key="index"
- >{{ label }}</cl-text
- >
- </view>
- <view
- class="px-[10rpx]"
- :style="{
- height: parseRpx(height)
- }"
- >
- <picker-view
- class="h-full"
- :value="value"
- :mask-style="maskStyle"
- :mask-top-style="maskStyle"
- :mask-bottom-style="maskStyle"
- :indicator-style="indicatorStyle"
- @change="onChange"
- >
- <picker-view-column
- class="cl-select-popup__column"
- v-for="(column, columnIndex) in columns"
- :key="columnIndex"
- >
- <!-- #ifdef APP-ANDROID -->
- <view
- ref="columnItemRef"
- :style="{
- height: `${itemHeight * column.length}px`
- }"
- android-layer-type="hardware"
- ></view>
- <!-- #endif -->
- <!-- #ifndef APP-ANDROID -->
- <view
- class="cl-picker-view__item"
- :style="{
- height: `${itemHeight}px`
- }"
- v-for="(item, index) in column"
- :key="index"
- >
- <cl-text
- :pt="{
- className: parseClass([
- [isDark, 'text-surface-500'],
- [isDark && index == value[columnIndex], 'text-white']
- ])
- }"
- >{{ item.label }}</cl-text
- >
- </view>
- <!-- #endif -->
- </picker-view-column>
- </picker-view>
- </view>
- </view>
- </template>
- <script setup lang="ts">
- import {
- forInObject,
- getColor,
- isAppIOS,
- isDark,
- isEqual,
- isNull,
- parseClass,
- rpx2px,
- parseRpx
- } from "@/cool";
- import type { ClSelectOption } from "../../types";
- import { computed, nextTick, onMounted, shallowRef, watch } from "vue";
- import type { PropType } from "vue";
- import { useSize } from "../../hooks";
- defineOptions({
- name: "cl-select-picker-view"
- });
- const props = defineProps({
- // 选择器表头
- headers: {
- type: Array as PropType<string[]>,
- default: () => []
- },
- // 选择器值
- value: {
- type: Array as PropType<number[]>,
- default: () => []
- },
- // 选择器选项
- columns: {
- type: Array as PropType<ClSelectOption[][]>,
- default: () => []
- },
- // 选择器选项高度
- itemHeight: {
- type: Number,
- default: isAppIOS() ? 50 : 42
- },
- // 选择器高度
- height: {
- type: Number,
- default: 600
- }
- });
- const emit = defineEmits(["change-value", "change-index"]);
- const { getScale } = useSize();
- // 获取窗口宽度,用于计算选择器列宽
- const { windowWidth } = uni.getWindowInfo();
- // 顶部显示表头
- const headers = computed(() => {
- return props.headers.slice(0, props.columns.length);
- });
- // 遮罩层样式
- const maskStyle = computed(() => {
- if (isDark.value) {
- if (isAppIOS()) {
- return `background-color: rgba(0, 0, 0, 0);`;
- }
- return `background-image: linear-gradient(
- 180deg,
- rgba(0, 0, 0, 0),
- rgba(0, 0, 0, 0)
- )`;
- }
- return "";
- });
- // 计算选择器列样式
- const indicatorStyle = computed(() => {
- // 根据窗口宽度和列数计算每列宽度
- const width = ((windowWidth - rpx2px(20)) / props.columns.length - rpx2px(2) - 8).toFixed(0);
- let str = "";
- // 选择器列样式配置
- const style = {
- height: `${props.itemHeight}px`,
- width: `${width}px`,
- left: "4px",
- backgroundColor: "rgba(10, 10, 10, 0.04)",
- borderRadius: "10px",
- border: "1rpx solid rgba(10, 10, 10, 0.2)"
- };
- // 深色模式
- if (isDark.value) {
- style.backgroundColor = "rgba(0, 0, 0, 0.1)";
- style.border = "1rpx solid rgba(255, 255, 255, 0.3)";
- }
- // 构建样式字符串
- forInObject(style, (value, key) => {
- str += `${key}: ${value};`;
- });
- return str;
- });
- // 监听选择器值改变事件
- function onChange(e: UniPickerViewChangeEvent) {
- // 获取选择器当前选中值数组
- const indexs = e.detail.value;
- // 处理因快速滑动导致下级数据未及时渲染而产生的索引越界问题
- indexs.forEach((v, i, arr) => {
- if (i < props.columns.length) {
- const n = props.columns[i].length;
- if (v >= n) {
- arr[i] = n - 1;
- }
- }
- });
- // 相同值不触发事件
- if (isEqual(indexs, props.value)) {
- return;
- }
- // 获取所有列的值
- const values = props.columns.map((c, i) => {
- return isNull(c[indexs[i]]) ? 0 : c[indexs[i]].value;
- });
- // 返回所有列的值或下标
- emit("change-value", values);
- emit("change-index", indexs);
- }
- // 列项ref引用
- const columnItemRef = shallowRef<UniElement[]>([]);
- // 渲染列项内容
- const renderColumnItem = () => {
- // 当前全局或默认字体大小
- const fontSize = getScale() * 14;
- // 根据主题切换字体颜色(深色/浅色)
- const color = isDark.value ? "white" : getColor("surface-700");
- // 遍历所有 picker-view-column 的ref,与每列数据一一对应
- for (let i = 0; i < columnItemRef.value.length; i++) {
- const column = props.columns[i]; // 当前列的数据
- const dom = columnItemRef.value[i]; // 当前列对应的DOM节点
- // 获取节点尺寸,用于文本居中计算
- const rect = dom!.getBoundingClientRect();
- // 获取原生画布上下文
- const ctx = dom!.getDrawableContext()!;
- ctx.reset(); // 每次重绘前需先重置画布
- ctx.textAlign = "center";
- // 计算文本水平方向的中点位置
- const x = rect.width / 2;
- // 循环绘制本列的每一个选项
- for (let j = 0; j < column.length; j++) {
- ctx.fillStyle = color; // 设置文本颜色
- ctx.font = `${fontSize}px`;
- // 计算每行文本的垂直位置,使其垂直居中于item元素内
- const y = 12 + (props.itemHeight - fontSize) / 2 + props.itemHeight * j;
- ctx.fillText(column[j].label, x, y); // 绘制文本
- }
- ctx.update(); // 完成当前列绘制,刷到视图
- }
- };
- onMounted(() => {
- nextTick(() => {
- renderColumnItem();
- });
- watch(
- computed(() => [isDark.value, props.columns, props.itemHeight]),
- () => {
- renderColumnItem();
- }
- );
- });
- </script>
- <style lang="scss" scoped>
- .cl-picker-view {
- @apply w-full h-full;
- &__header {
- @apply flex flex-row items-center py-4;
- }
- &__item {
- @apply flex flex-row items-center justify-center;
- }
- .uni-picker-view-indicator {
- // #ifdef H5
- &::after,
- &::before {
- display: none;
- }
- // #endif
- }
- }
- </style>
|