cl-select-time.uvue 6.4 KB

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