cl-calendar-select.uvue 5.2 KB

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