time-picker.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <script setup lang="ts">
  2. import { ref, watch } from "vue";
  3. defineOptions({
  4. name: "TimePicker"
  5. });
  6. const size = ref("default");
  7. const dynamicSize = ref();
  8. /** 时间选择器 */
  9. const value = ref("");
  10. const value1 = ref("");
  11. const value3 = ref();
  12. const value2 = ref(new Date(2016, 9, 10, 18, 30));
  13. const makeRange = (start: number, end: number) => {
  14. const result: number[] = [];
  15. for (let i = start; i <= end; i++) {
  16. result.push(i);
  17. }
  18. return result;
  19. };
  20. const disabledHours = () => {
  21. return makeRange(0, 16).concat(makeRange(19, 23));
  22. };
  23. const disabledMinutes = (hour: number) => {
  24. if (hour === 17) {
  25. return makeRange(0, 29);
  26. }
  27. if (hour === 18) {
  28. return makeRange(31, 59);
  29. }
  30. };
  31. const disabledSeconds = (hour: number, minute: number) => {
  32. if (hour === 18 && minute === 30) {
  33. return makeRange(1, 59);
  34. }
  35. };
  36. watch(size, val =>
  37. val === "disabled"
  38. ? (dynamicSize.value = "default")
  39. : (dynamicSize.value = size.value)
  40. );
  41. /** 时间选择 */
  42. const value4 = ref("");
  43. const value5 = ref("");
  44. const startTime = ref("");
  45. const endTime = ref("");
  46. </script>
  47. <template>
  48. <div>
  49. <el-card shadow="never">
  50. <template #header>
  51. <div class="card-header">
  52. <el-space wrap :size="40">
  53. <el-link
  54. v-tippy="{
  55. content: '点击查看详细文档'
  56. }"
  57. href="https://element-plus.org/zh-CN/component/time-picker.html"
  58. target="_blank"
  59. style="font-size: 16px; font-weight: 800"
  60. >
  61. 时间选择器
  62. </el-link>
  63. <el-radio-group v-model="size" size="small">
  64. <el-radio value="large">大尺寸</el-radio>
  65. <el-radio value="default">默认尺寸</el-radio>
  66. <el-radio value="small">小尺寸</el-radio>
  67. <el-radio value="disabled">禁用</el-radio>
  68. </el-radio-group>
  69. </el-space>
  70. </div>
  71. </template>
  72. <p class="mb-2">日期和时间点</p>
  73. <el-space wrap>
  74. <p class="text-[15px]">鼠标滚轮进行选择</p>
  75. <el-time-picker
  76. v-model="value"
  77. placeholder="请选择时间"
  78. class="!w-[140px]"
  79. :size="dynamicSize"
  80. :disabled="size === 'disabled'"
  81. />
  82. <p class="text-[15px]">箭头进行选择</p>
  83. <el-time-picker
  84. v-model="value1"
  85. arrow-control
  86. placeholder="请选择时间"
  87. class="!w-[140px]"
  88. :size="dynamicSize"
  89. :disabled="size === 'disabled'"
  90. />
  91. </el-space>
  92. <el-divider />
  93. <p class="mb-2">限制时间选择范围</p>
  94. <el-time-picker
  95. v-model="value2"
  96. class="!w-[140px]"
  97. :disabled-hours="disabledHours"
  98. :disabled-minutes="disabledMinutes"
  99. :disabled-seconds="disabledSeconds"
  100. placeholder="Arbitrary time"
  101. :size="dynamicSize"
  102. :disabled="size === 'disabled'"
  103. />
  104. <el-divider />
  105. <p class="mb-2">任意时间范围</p>
  106. <el-time-picker
  107. v-model="value3"
  108. class="!w-[220px]"
  109. is-range
  110. range-separator="至"
  111. start-placeholder="开始时间"
  112. end-placeholder="结束时间"
  113. :size="dynamicSize"
  114. :disabled="size === 'disabled'"
  115. />
  116. </el-card>
  117. <el-card shadow="never" class="mt-4">
  118. <template #header>
  119. <div class="card-header">
  120. <el-link
  121. v-tippy="{
  122. content: '点击查看详细文档'
  123. }"
  124. href="https://element-plus.org/zh-CN/component/time-select.html"
  125. target="_blank"
  126. style="font-size: 16px; font-weight: 800"
  127. >
  128. 时间选择
  129. </el-link>
  130. </div>
  131. </template>
  132. <p class="mb-2">固定时间点</p>
  133. <el-time-select
  134. v-model="value4"
  135. placeholder="请选择时间"
  136. class="!w-[140px]"
  137. start="08:30"
  138. step="00:15"
  139. end="18:30"
  140. :size="dynamicSize"
  141. :disabled="size === 'disabled'"
  142. />
  143. <p class="mb-2 mt-4">时间格式</p>
  144. <el-time-select
  145. v-model="value5"
  146. placeholder="请选择时间"
  147. class="!w-[140px]"
  148. start="00:00"
  149. step="00:30"
  150. end="23:59"
  151. format="hh:mm A"
  152. :size="dynamicSize"
  153. :disabled="size === 'disabled'"
  154. />
  155. <p class="mb-2 mt-4">固定时间范围</p>
  156. <el-space wrap>
  157. <el-time-select
  158. v-model="startTime"
  159. placeholder="开始时间"
  160. class="!w-[140px]"
  161. :max-time="endTime"
  162. start="08:30"
  163. step="00:15"
  164. end="18:30"
  165. :size="dynamicSize"
  166. :disabled="size === 'disabled'"
  167. />
  168. <el-time-select
  169. v-model="endTime"
  170. placeholder="结束时间"
  171. class="!w-[140px]"
  172. :min-time="startTime"
  173. start="08:30"
  174. step="00:15"
  175. end="18:30"
  176. :size="dynamicSize"
  177. :disabled="size === 'disabled'"
  178. />
  179. </el-space>
  180. </el-card>
  181. </div>
  182. </template>