cl-select-time.uvue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. arrow-icon="time-line"
  13. @open="open()"
  14. @clear="clear"
  15. ></cl-select-trigger>
  16. <cl-popup
  17. ref="popupRef"
  18. v-model="visible"
  19. :title="title"
  20. :pt="{
  21. className: pt.popup?.className,
  22. header: pt.popup?.header,
  23. container: pt.popup?.container,
  24. mask: pt.popup?.mask,
  25. draw: pt.popup?.draw
  26. }"
  27. >
  28. <view class="cl-select-popup" @touchmove.stop>
  29. <view class="cl-select-popup__picker">
  30. <cl-picker-view
  31. :value="indexes"
  32. :headers="headers"
  33. :columns="columns"
  34. :reset-on-change="false"
  35. @change="onChange"
  36. ></cl-picker-view>
  37. </view>
  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, watch } from "vue";
  66. import type { ClSelectOption } from "../../types";
  67. import { isEmpty, isNull, 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-select-time"
  73. });
  74. // 组件属性定义
  75. const props = defineProps({
  76. // 透传样式配置
  77. pt: {
  78. type: Object,
  79. default: () => ({})
  80. },
  81. // 选择器的值
  82. modelValue: {
  83. type: String,
  84. default: ""
  85. },
  86. // 表头
  87. headers: {
  88. type: Array as PropType<string[]>,
  89. default: () => [t("小时"), t("分钟"), t("秒数")]
  90. },
  91. // 类型,控制选择的粒度
  92. type: {
  93. type: String as PropType<"hour" | "minute" | "second">,
  94. default: "second"
  95. },
  96. // 选择器标题
  97. title: {
  98. type: String,
  99. default: () => t("请选择")
  100. },
  101. // 选择器占位符
  102. placeholder: {
  103. type: String,
  104. default: () => t("请选择")
  105. },
  106. // 是否显示选择器触发器
  107. showTrigger: {
  108. type: Boolean,
  109. default: true
  110. },
  111. // 是否禁用选择器
  112. disabled: {
  113. type: Boolean,
  114. default: false
  115. },
  116. // 确认按钮文本
  117. confirmText: {
  118. type: String,
  119. default: () => t("确定")
  120. },
  121. // 是否显示确认按钮
  122. showConfirm: {
  123. type: Boolean,
  124. default: true
  125. },
  126. // 取消按钮文本
  127. cancelText: {
  128. type: String,
  129. default: () => t("取消")
  130. },
  131. // 是否显示取消按钮
  132. showCancel: {
  133. type: Boolean,
  134. default: true
  135. },
  136. // 时间标签格式化
  137. labelFormat: {
  138. type: String as PropType<string | null>,
  139. default: null
  140. }
  141. });
  142. // 定义事件
  143. const emit = defineEmits(["update:modelValue", "change"]);
  144. // 弹出层引用
  145. const popupRef = ref<ClPopupComponentPublicInstance | null>(null);
  146. // 透传样式类型定义
  147. type PassThrough = {
  148. trigger?: ClSelectTriggerPassThrough;
  149. popup?: ClPopupPassThrough;
  150. };
  151. // 解析透传样式配置
  152. const pt = computed(() => parsePt<PassThrough>(props.pt));
  153. // 标签格式化
  154. const labelFormat = computed(() => {
  155. if (props.labelFormat != null) {
  156. return props.labelFormat;
  157. }
  158. if (props.type == "hour") {
  159. return "{H}";
  160. }
  161. if (props.type == "minute") {
  162. return "{H}:{m}";
  163. }
  164. return "{H}:{m}:{s}";
  165. });
  166. // 当前选中的值
  167. const value = ref<string[]>([]);
  168. // 当前选中项的索引
  169. const indexes = ref<number[]>([]);
  170. // 时间选择器列表
  171. const list = computed(() => {
  172. const arr = [[], [], []] as ClSelectOption[][];
  173. for (let i = 0; i < 60; i++) {
  174. const v = i.toString().padStart(2, "0");
  175. const item = {
  176. label: v,
  177. value: v
  178. } as ClSelectOption;
  179. if (i < 24) {
  180. arr[0].push(item);
  181. }
  182. arr[1].push(item);
  183. arr[2].push(item);
  184. }
  185. return arr;
  186. });
  187. // 列数,决定显示多少列(时、分、秒)
  188. const columnNum = computed(() => {
  189. return ["hour", "minute", "second"].findIndex((e) => e == props.type) + 1;
  190. });
  191. // 列数据,取出需要显示的列
  192. const columns = computed(() => {
  193. return list.value.slice(0, columnNum.value);
  194. });
  195. // 显示文本
  196. const text = computed(() => {
  197. // 获取当前v-model绑定的时间字符串
  198. const val = props.modelValue;
  199. // 如果值为空或为null,返回空字符串
  200. if (isEmpty(val) || isNull(val)) {
  201. return "";
  202. }
  203. // 拆分时间字符串,分别获取小时、分钟、秒
  204. const [h, m, s] = val.split(":");
  205. // 按照labelFormat格式化显示文本
  206. return labelFormat.value.replace("{H}", h).replace("{m}", m).replace("{s}", s);
  207. });
  208. // 选择器值改变事件
  209. function onChange(a: number[]) {
  210. // 复制当前组件内部维护的索引数组
  211. const b = [...indexes.value];
  212. // 遍历所有列,处理联动逻辑
  213. for (let i = 0; i < a.length; i++) {
  214. // 检查当前列是否发生改变
  215. if (b[i] != a[i]) {
  216. // 更新当前列的索引
  217. b[i] = a[i];
  218. }
  219. }
  220. // 更新组件内部维护的索引数组
  221. indexes.value = b;
  222. // 根据最新的索引数组,更新选中的值数组
  223. value.value = b.map((e, i) => list.value[i][e].value as string);
  224. }
  225. // 选择器显示状态
  226. const visible = ref(false);
  227. // 选择回调函数
  228. let callback: ((value: string) => void) | null = null;
  229. // 打开选择器
  230. function open(cb: ((value: string) => void) | null = null) {
  231. if (props.disabled) {
  232. return;
  233. }
  234. visible.value = true;
  235. callback = cb;
  236. }
  237. // 关闭选择器
  238. function close() {
  239. visible.value = false;
  240. }
  241. // 清空选择器
  242. function clear() {
  243. emit("update:modelValue", "");
  244. emit("change", "");
  245. }
  246. // 确认选择
  247. function confirm() {
  248. // 将选中值转换为字符串格式
  249. const val = value.value.join(":");
  250. // 触发更新事件
  251. emit("update:modelValue", val);
  252. emit("change", val);
  253. // 触发回调
  254. if (callback != null) {
  255. callback!(val);
  256. }
  257. // 关闭选择器
  258. close();
  259. }
  260. // 监听modelValue变化
  261. watch(
  262. computed(() => props.modelValue),
  263. (val: string) => {
  264. // 声明选中值数组
  265. let _value: string[];
  266. // 判断输入值是否为空
  267. if (isEmpty(val) || isNull(val)) {
  268. // 设置空数组
  269. _value = [];
  270. } else {
  271. // 按冒号分割字符串为数组
  272. _value = val.split(":");
  273. }
  274. // 声明索引数组
  275. let _indexes = [] as number[];
  276. // 遍历时分秒三列
  277. for (let i = 0; i < 3; i++) {
  278. // 判断是否需要设置默认值
  279. if (i >= _value.length) {
  280. // 添加默认索引0
  281. _indexes.push(0);
  282. // 添加默认值
  283. _value.push(list.value[i][0].value as string);
  284. } else {
  285. // 查找当前值对应的索引
  286. let index = list.value[i].findIndex((e) => e.value == _value[i]);
  287. // 索引无效时重置为0
  288. if (index < 0) {
  289. index = 0;
  290. }
  291. // 添加索引
  292. _indexes.push(index);
  293. }
  294. }
  295. // 更新选中值
  296. value.value = _value;
  297. // 更新索引值
  298. indexes.value = _indexes;
  299. },
  300. {
  301. immediate: true
  302. }
  303. );
  304. defineExpose({
  305. open,
  306. close
  307. });
  308. </script>
  309. <style lang="scss" scoped>
  310. .cl-select {
  311. &-popup {
  312. &__op {
  313. @apply flex flex-row items-center justify-center;
  314. padding: 24rpx;
  315. }
  316. }
  317. }
  318. </style>