cl-calendar-select.uvue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <template>
  2. <cl-select-trigger
  3. v-if="showTrigger"
  4. :pt="ptTrigger"
  5. :placeholder="placeholder"
  6. :disabled="disabled"
  7. :focus="popupRef?.isOpen"
  8. :text="text"
  9. @open="open()"
  10. @clear="clear"
  11. ></cl-select-trigger>
  12. <cl-popup ref="popupRef" v-model="visible" :title="title" :pt="ptPopup">
  13. <view class="cl-select-popup" @touchmove.stop>
  14. <slot name="prepend"></slot>
  15. <view class="cl-select-popup__picker">
  16. <cl-calendar
  17. v-model="value"
  18. v-model:date="date"
  19. :mode="mode"
  20. :date-config="dateConfig"
  21. @change="onCalendarChange"
  22. ></cl-calendar>
  23. </view>
  24. <slot name="append"></slot>
  25. <view class="cl-select-popup__op">
  26. <cl-button
  27. v-if="showCancel"
  28. size="large"
  29. text
  30. border
  31. type="light"
  32. :pt="{
  33. className: 'flex-1 !rounded-xl h-[80rpx]'
  34. }"
  35. @tap="close"
  36. >{{ cancelText }}</cl-button
  37. >
  38. <cl-button
  39. v-if="showConfirm"
  40. size="large"
  41. :pt="{
  42. className: 'flex-1 !rounded-xl h-[80rpx]'
  43. }"
  44. @tap="confirm"
  45. >{{ confirmText }}</cl-button
  46. >
  47. </view>
  48. </view>
  49. </cl-popup>
  50. </template>
  51. <script setup lang="ts">
  52. import { ref, computed, type PropType } from "vue";
  53. import type { ClCalendarDateConfig, ClCalendarMode } from "../../types";
  54. import { isEmpty, parsePt, parseToObject } from "@/cool";
  55. import type { ClSelectTriggerPassThrough } from "../cl-select-trigger/props";
  56. import type { ClPopupPassThrough } from "../cl-popup/props";
  57. import { t } from "@/locale";
  58. defineOptions({
  59. name: "cl-calendar-select"
  60. });
  61. defineSlots<{
  62. prepend(): any;
  63. append(): any;
  64. }>();
  65. // 组件属性定义
  66. const props = defineProps({
  67. // 透传样式配置
  68. pt: {
  69. type: Object,
  70. default: () => ({})
  71. },
  72. // 选择器的值
  73. modelValue: {
  74. type: String as PropType<string | null>,
  75. default: null
  76. },
  77. // 多个日期
  78. date: {
  79. type: Array as PropType<string[]>,
  80. default: () => []
  81. },
  82. // 日期选择模式
  83. mode: {
  84. type: String as PropType<ClCalendarMode>,
  85. default: "single"
  86. },
  87. // 日期配置
  88. dateConfig: {
  89. type: Array as PropType<ClCalendarDateConfig[]>,
  90. default: () => []
  91. },
  92. // 选择器标题
  93. title: {
  94. type: String,
  95. default: () => t("请选择")
  96. },
  97. // 选择器占位符
  98. placeholder: {
  99. type: String,
  100. default: () => t("请选择")
  101. },
  102. // 是否显示选择器触发器
  103. showTrigger: {
  104. type: Boolean,
  105. default: true
  106. },
  107. // 是否禁用选择器
  108. disabled: {
  109. type: Boolean,
  110. default: false
  111. },
  112. // 分隔符
  113. splitor: {
  114. type: String,
  115. default: "、"
  116. },
  117. // 范围分隔符
  118. rangeSplitor: {
  119. type: String,
  120. default: () => t(" 至 ")
  121. },
  122. // 确认按钮文本
  123. confirmText: {
  124. type: String,
  125. default: () => t("确定")
  126. },
  127. // 是否显示确认按钮
  128. showConfirm: {
  129. type: Boolean,
  130. default: true
  131. },
  132. // 取消按钮文本
  133. cancelText: {
  134. type: String,
  135. default: () => t("取消")
  136. },
  137. // 是否显示取消按钮
  138. showCancel: {
  139. type: Boolean,
  140. default: true
  141. }
  142. });
  143. // 定义事件
  144. const emit = defineEmits(["update:modelValue", "update:date", "change", "select"]);
  145. // 弹出层引用
  146. const popupRef = ref<ClPopupComponentPublicInstance | null>(null);
  147. // 透传样式类型定义
  148. type PassThrough = {
  149. trigger?: ClSelectTriggerPassThrough;
  150. popup?: ClPopupPassThrough;
  151. };
  152. // 解析透传样式配置
  153. const pt = computed(() => parsePt<PassThrough>(props.pt));
  154. // 解析触发器透传样式配置
  155. const ptTrigger = computed(() => parseToObject(pt.value.trigger));
  156. // 解析弹窗透传样式配置
  157. const ptPopup = computed(() => parseToObject(pt.value.popup));
  158. // 当前选中的值
  159. const value = ref<string | null>(null);
  160. // 当前选中的日期
  161. const date = ref<string[]>([]);
  162. // 显示文本
  163. const text = computed(() => {
  164. switch (props.mode) {
  165. case "single":
  166. return props.modelValue ?? "";
  167. case "multiple":
  168. return props.date.join(props.splitor);
  169. case "range":
  170. return props.date.join(props.rangeSplitor);
  171. default:
  172. return "";
  173. }
  174. });
  175. // 选择器显示状态
  176. const visible = ref(false);
  177. // 选择回调函数
  178. let callback: ((value: string | string[]) => void) | null = null;
  179. // 打开选择器
  180. function open(cb: ((value: string | string[]) => void) | null = null) {
  181. visible.value = true;
  182. // 单选日期
  183. value.value = props.modelValue;
  184. // 多个日期
  185. date.value = props.date;
  186. // 回调
  187. callback = cb;
  188. }
  189. // 关闭选择器
  190. function close() {
  191. visible.value = false;
  192. }
  193. // 清空选择器
  194. function clear() {
  195. value.value = null;
  196. date.value = [] as string[];
  197. emit("update:modelValue", value.value);
  198. emit("update:date", date.value);
  199. }
  200. // 确认选择
  201. function confirm() {
  202. if (props.mode == "single") {
  203. if (value.value == null) {
  204. return;
  205. }
  206. emit("update:modelValue", value.value);
  207. emit("change", value.value);
  208. // 触发回调
  209. if (callback != null) {
  210. callback!(value.value);
  211. }
  212. } else {
  213. if (isEmpty(date.value)) {
  214. return;
  215. }
  216. emit("update:date", date.value);
  217. emit("change", date.value);
  218. // 触发回调
  219. if (callback != null) {
  220. callback!(date.value);
  221. }
  222. }
  223. // 关闭选择器
  224. close();
  225. }
  226. // 日历变化
  227. function onCalendarChange(date: string[]) {
  228. emit("select", date);
  229. }
  230. defineExpose({
  231. open,
  232. close
  233. });
  234. </script>
  235. <style lang="scss" scoped>
  236. .cl-select {
  237. &-popup {
  238. &__picker {
  239. @apply p-3 pt-0;
  240. }
  241. &__op {
  242. @apply flex flex-row items-center justify-center;
  243. padding: 24rpx;
  244. }
  245. }
  246. }
  247. </style>