| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- <template>
- <cl-select-trigger
- v-if="showTrigger"
- :pt="{
- className: pt.trigger?.className,
- icon: pt.trigger?.icon
- }"
- :placeholder="placeholder"
- :disabled="disabled"
- :focus="popupRef?.isOpen"
- :text="text"
- @open="open()"
- @clear="clear"
- ></cl-select-trigger>
- <cl-popup
- ref="popupRef"
- v-model="visible"
- :title="title"
- :pt="{
- className: pt.popup?.className,
- header: pt.popup?.header,
- container: pt.popup?.container,
- mask: pt.popup?.mask,
- draw: pt.popup?.draw
- }"
- >
- <view class="cl-select-popup" @touchmove.stop>
- <slot name="prepend"></slot>
- <view class="cl-select-popup__picker">
- <cl-calendar
- v-model="value"
- v-model:date="date"
- :mode="mode"
- :date-config="dateConfig"
- @change="onCalendarChange"
- ></cl-calendar>
- </view>
- <slot name="append"></slot>
- <view class="cl-select-popup__op">
- <cl-button
- v-if="showCancel"
- size="large"
- text
- border
- type="light"
- :pt="{
- className: 'flex-1 !rounded-xl h-[80rpx]'
- }"
- @tap="close"
- >{{ cancelText }}</cl-button
- >
- <cl-button
- v-if="showConfirm"
- size="large"
- :pt="{
- className: 'flex-1 !rounded-xl h-[80rpx]'
- }"
- @tap="confirm"
- >{{ confirmText }}</cl-button
- >
- </view>
- </view>
- </cl-popup>
- </template>
- <script setup lang="ts">
- import { ref, computed, type PropType } from "vue";
- import type { ClCalendarDateConfig, ClCalendarMode } from "../../types";
- import { isEmpty, parsePt } from "@/cool";
- import type { ClSelectTriggerPassThrough } from "../cl-select-trigger/props";
- import type { ClPopupPassThrough } from "../cl-popup/props";
- import { t } from "@/locale";
- defineOptions({
- name: "cl-calendar-select"
- });
- defineSlots<{
- prepend(): any;
- append(): any;
- }>();
- // 组件属性定义
- const props = defineProps({
- // 透传样式配置
- pt: {
- type: Object,
- default: () => ({})
- },
- // 选择器的值
- modelValue: {
- type: String as PropType<string | null>,
- default: null
- },
- // 多个日期
- date: {
- type: Array as PropType<string[]>,
- default: () => []
- },
- // 日期选择模式
- mode: {
- type: String as PropType<ClCalendarMode>,
- default: "single"
- },
- // 日期配置
- dateConfig: {
- type: Array as PropType<ClCalendarDateConfig[]>,
- default: () => []
- },
- // 选择器标题
- title: {
- type: String,
- default: () => t("请选择")
- },
- // 选择器占位符
- placeholder: {
- type: String,
- default: () => t("请选择")
- },
- // 是否显示选择器触发器
- showTrigger: {
- type: Boolean,
- default: true
- },
- // 是否禁用选择器
- disabled: {
- type: Boolean,
- default: false
- },
- // 分隔符
- splitor: {
- type: String,
- default: "、"
- },
- // 范围分隔符
- rangeSplitor: {
- type: String,
- default: () => t(" 至 ")
- },
- // 确认按钮文本
- confirmText: {
- type: String,
- default: () => t("确定")
- },
- // 是否显示确认按钮
- showConfirm: {
- type: Boolean,
- default: true
- },
- // 取消按钮文本
- cancelText: {
- type: String,
- default: () => t("取消")
- },
- // 是否显示取消按钮
- showCancel: {
- type: Boolean,
- default: true
- }
- });
- // 定义事件
- const emit = defineEmits(["update:modelValue", "update:date", "change", "select"]);
- // 弹出层引用
- const popupRef = ref<ClPopupComponentPublicInstance | null>(null);
- // 透传样式类型定义
- type PassThrough = {
- trigger?: ClSelectTriggerPassThrough;
- popup?: ClPopupPassThrough;
- };
- // 解析透传样式配置
- const pt = computed(() => parsePt<PassThrough>(props.pt));
- // 当前选中的值
- const value = ref<string | null>(null);
- // 当前选中的日期
- const date = ref<string[]>([]);
- // 显示文本
- const text = computed(() => {
- switch (props.mode) {
- case "single":
- return props.modelValue ?? "";
- case "multiple":
- return props.date.join(props.splitor);
- case "range":
- return props.date.join(props.rangeSplitor);
- default:
- return "";
- }
- });
- // 选择器显示状态
- const visible = ref(false);
- // 选择回调函数
- let callback: ((value: string | string[]) => void) | null = null;
- // 打开选择器
- function open(cb: ((value: string | string[]) => void) | null = null) {
- visible.value = true;
- // 单选日期
- value.value = props.modelValue;
- // 多个日期
- date.value = props.date;
- // 回调
- callback = cb;
- }
- // 关闭选择器
- function close() {
- visible.value = false;
- }
- // 清空选择器
- function clear() {
- value.value = null;
- date.value = [] as string[];
- emit("update:modelValue", value.value);
- emit("update:date", date.value);
- }
- // 确认选择
- function confirm() {
- if (props.mode == "single") {
- if (value.value == null) {
- return;
- }
- emit("update:modelValue", value.value);
- emit("change", value.value);
- // 触发回调
- if (callback != null) {
- callback!(value.value);
- }
- } else {
- if (isEmpty(date.value)) {
- return;
- }
- emit("update:date", date.value);
- emit("change", date.value);
- // 触发回调
- if (callback != null) {
- callback!(date.value);
- }
- }
- // 关闭选择器
- close();
- }
- // 日历变化
- function onCalendarChange(date: string[]) {
- emit("select", date);
- }
- defineExpose({
- open,
- close
- });
- </script>
- <style lang="scss" scoped>
- .cl-select {
- &-popup {
- &__picker {
- @apply p-3 pt-0;
- }
- &__op {
- @apply flex flex-row items-center justify-center;
- padding: 24rpx;
- }
- }
- }
- </style>
|