select-date.uvue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <cl-page>
  3. <view class="p-3">
  4. <demo-item :label="t('基础用法')">
  5. <cl-select-date v-model="form.date1" type="year"></cl-select-date>
  6. </demo-item>
  7. <demo-item :label="t('精确到秒')">
  8. <cl-select-date v-model="form.date2" type="second"></cl-select-date>
  9. </demo-item>
  10. <demo-item :label="t('固定开始、结束时间')">
  11. <cl-select-date
  12. v-model="form.date3"
  13. start="2025-06-01 10:00:00"
  14. end="2026-06-01 10:00:00"
  15. ></cl-select-date>
  16. </demo-item>
  17. <demo-item :label="t('自定义触发器')">
  18. <view class="flex flex-row">
  19. <cl-button @tap="openSelect4">{{ t("打开选择器") }}</cl-button>
  20. </view>
  21. <cl-select-date
  22. ref="selectRef4"
  23. v-model="form.date4"
  24. type="date"
  25. :show-trigger="false"
  26. ></cl-select-date>
  27. </demo-item>
  28. <demo-item :label="t('范围选择')">
  29. <cl-text :pt="{ className: 'mb-3' }">{{ form.date5 }}</cl-text>
  30. <cl-select-date v-model:values="form.date5" type="date" rangeable></cl-select-date>
  31. </demo-item>
  32. <demo-item :label="t('自定义')">
  33. <cl-select-date
  34. v-model="form.date6"
  35. :type="type"
  36. :label-format="labelFormat"
  37. :disabled="isDisabled"
  38. ></cl-select-date>
  39. <cl-list
  40. border
  41. :pt="{
  42. className: 'mt-3'
  43. }"
  44. >
  45. <cl-list-item label="YYYY">
  46. <cl-switch v-model="isYear"></cl-switch>
  47. </cl-list-item>
  48. <cl-list-item label="YYYY-MM">
  49. <cl-switch v-model="isMonth"></cl-switch>
  50. </cl-list-item>
  51. <cl-list-item
  52. label="YYYY-MM-DD"
  53. :pt="{
  54. label: {
  55. className: 'flex-[2]'
  56. }
  57. }"
  58. >
  59. <cl-switch v-model="isDate"></cl-switch>
  60. </cl-list-item>
  61. <cl-list-item
  62. label="YYYY-MM-DD HH"
  63. :pt="{
  64. label: {
  65. className: 'flex-[2]'
  66. }
  67. }"
  68. >
  69. <cl-switch v-model="isHour"></cl-switch>
  70. </cl-list-item>
  71. <cl-list-item
  72. label="YYYY-MM-DD HH:mm"
  73. :pt="{
  74. label: {
  75. className: 'flex-[2]'
  76. }
  77. }"
  78. >
  79. <cl-switch v-model="isMinute"></cl-switch>
  80. </cl-list-item>
  81. <cl-list-item
  82. label="YYYY-MM-DD HH:mm:ss"
  83. :pt="{
  84. label: {
  85. className: 'flex-[2]'
  86. }
  87. }"
  88. >
  89. <cl-switch v-model="isSecond"></cl-switch>
  90. </cl-list-item>
  91. <cl-list-item :label="t('标签格式化')">
  92. <cl-switch v-model="isFormat"></cl-switch>
  93. </cl-list-item>
  94. <cl-list-item :label="t('禁用')">
  95. <cl-switch v-model="isDisabled"></cl-switch>
  96. </cl-list-item>
  97. </cl-list>
  98. </demo-item>
  99. </view>
  100. </cl-page>
  101. </template>
  102. <script lang="ts" setup>
  103. import { computed, reactive, ref } from "vue";
  104. import DemoItem from "../components/item.uvue";
  105. import { t } from "@/locale";
  106. import { useUi } from "@/uni_modules/cool-ui";
  107. const ui = useUi();
  108. type Form = {
  109. date1: string;
  110. date2: string;
  111. date3: string;
  112. date4: string;
  113. date5: string[];
  114. date6: string;
  115. };
  116. const form = reactive<Form>({
  117. date1: "",
  118. date2: "",
  119. date3: "",
  120. date4: "",
  121. date5: [],
  122. date6: ""
  123. });
  124. const isDisabled = ref(false);
  125. const isYear = ref(false);
  126. const isMonth = ref(false);
  127. const isDate = ref(true);
  128. const isHour = ref(false);
  129. const isMinute = ref(false);
  130. const isSecond = ref(false);
  131. const isFormat = ref(false);
  132. const type = computed(() => {
  133. if (isSecond.value) {
  134. return "second";
  135. }
  136. if (isMinute.value) {
  137. return "minute";
  138. }
  139. if (isHour.value) {
  140. return "hour";
  141. }
  142. if (isDate.value) {
  143. return "date";
  144. }
  145. if (isMonth.value) {
  146. return "month";
  147. }
  148. if (isYear.value) {
  149. return "year";
  150. }
  151. return "second";
  152. });
  153. const labelFormat = computed(() => {
  154. if (isFormat.value) {
  155. if (isSecond.value) {
  156. return "YYYY年MM月DD日 HH时mm分ss秒";
  157. }
  158. if (isMinute.value) {
  159. return "YYYY年MM月DD日 HH时mm分";
  160. }
  161. if (isHour.value) {
  162. return "YYYY年MM月DD日 HH时";
  163. }
  164. if (isDate.value) {
  165. return "YYYY年MM月DD日";
  166. }
  167. if (isMonth.value) {
  168. return "YYYY年MM月";
  169. }
  170. if (isYear.value) {
  171. return "YYYY年";
  172. }
  173. } else {
  174. if (isSecond.value) {
  175. return "YYYY-MM-DD HH:mm:ss";
  176. }
  177. if (isMinute.value) {
  178. return "YYYY-MM-DD HH:mm";
  179. }
  180. if (isHour.value) {
  181. return "YYYY-MM-DD HH";
  182. }
  183. if (isDate.value) {
  184. return "YYYY-MM-DD";
  185. }
  186. if (isMonth.value) {
  187. return "YYYY-MM";
  188. }
  189. if (isYear.value) {
  190. return "YYYY";
  191. }
  192. }
  193. return "YYYY-MM-DD HH:mm:ss";
  194. });
  195. const selectRef4 = ref<ClSelectDateComponentPublicInstance | null>(null);
  196. function openSelect4() {
  197. selectRef4.value!.open((value) => {
  198. ui.showToast({
  199. message: `你选择了:${value}`
  200. });
  201. });
  202. }
  203. </script>