cl-calendar-select.uvue 4.9 KB

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