cl-calendar-select.uvue 5.6 KB

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