date-picker.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <script setup lang="ts">
  2. import { ref, watch } from "vue";
  3. import { useRenderIcon } from "@/components/ReIcon/src/hooks";
  4. defineOptions({
  5. name: "DatePicker"
  6. });
  7. const size = ref("default");
  8. const dynamicSize = ref();
  9. const value = ref("");
  10. const shortcuts = [
  11. {
  12. text: "今天",
  13. value: new Date()
  14. },
  15. {
  16. text: "昨天",
  17. value: () => {
  18. const date = new Date();
  19. date.setTime(date.getTime() - 3600 * 1000 * 24);
  20. return date;
  21. }
  22. },
  23. {
  24. text: "一周前",
  25. value: () => {
  26. const date = new Date();
  27. date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
  28. return date;
  29. }
  30. }
  31. ];
  32. const disabledDate = (time: Date) => {
  33. return time.getTime() > Date.now();
  34. };
  35. const value1 = ref("");
  36. const value2 = ref("");
  37. const value3 = ref("");
  38. const value4 = ref("");
  39. const value5 = ref("");
  40. const shortcuts1 = [
  41. {
  42. text: "上周",
  43. value: () => {
  44. const end = new Date();
  45. const start = new Date();
  46. start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
  47. return [start, end];
  48. }
  49. },
  50. {
  51. text: "上个月",
  52. value: () => {
  53. const end = new Date();
  54. const start = new Date();
  55. start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
  56. return [start, end];
  57. }
  58. },
  59. {
  60. text: "三个月前",
  61. value: () => {
  62. const end = new Date();
  63. const start = new Date();
  64. start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
  65. return [start, end];
  66. }
  67. }
  68. ];
  69. const value6 = ref("");
  70. const shortcuts2 = [
  71. {
  72. text: "本月",
  73. value: [new Date(), new Date()]
  74. },
  75. {
  76. text: "今年",
  77. value: () => {
  78. const end = new Date();
  79. const start = new Date(new Date().getFullYear(), 0);
  80. return [start, end];
  81. }
  82. },
  83. {
  84. text: "六个月前",
  85. value: () => {
  86. const end = new Date();
  87. const start = new Date();
  88. start.setMonth(start.getMonth() - 6);
  89. return [start, end];
  90. }
  91. }
  92. ];
  93. const value7 = ref("");
  94. const dateFormat = ref("");
  95. const value8 = ref("");
  96. const value9 = ref("2023-10-30");
  97. const holidays = [
  98. "2023-10-22",
  99. "2023-10-23",
  100. "2023-10-24",
  101. "2023-10-25",
  102. "2023-10-26",
  103. "2023-10-27",
  104. "2023-10-28",
  105. "2023-10-29",
  106. "2023-10-30",
  107. "2023-10-31"
  108. ];
  109. const isHoliday = ({ dayjs }) => {
  110. return holidays.includes(dayjs.format("YYYY-MM-DD"));
  111. };
  112. watch(size, val =>
  113. val === "disabled"
  114. ? (dynamicSize.value = "default")
  115. : (dynamicSize.value = size.value)
  116. );
  117. </script>
  118. <template>
  119. <el-card shadow="never">
  120. <template #header>
  121. <div class="card-header">
  122. <el-space wrap :size="40">
  123. <el-link
  124. href="https://element-plus.org/zh-CN/component/date-picker.html"
  125. target="_blank"
  126. style="font-size: 16px; font-weight: 800"
  127. >
  128. 日期选择器
  129. </el-link>
  130. <el-radio-group v-model="size" size="small">
  131. <el-radio label="large">大尺寸</el-radio>
  132. <el-radio label="default">默认尺寸</el-radio>
  133. <el-radio label="small">小尺寸</el-radio>
  134. <el-radio label="disabled">禁用</el-radio>
  135. </el-radio-group>
  136. </el-space>
  137. </div>
  138. </template>
  139. <p class="mb-2">选择某一天</p>
  140. <el-date-picker
  141. v-model="value"
  142. type="date"
  143. class="!w-[160px]"
  144. placeholder="请选择"
  145. :disabled-date="disabledDate"
  146. :shortcuts="shortcuts"
  147. :popper-options="{
  148. placement: 'bottom-start'
  149. }"
  150. :size="dynamicSize"
  151. :disabled="size === 'disabled'"
  152. />
  153. <p class="mb-2 mt-4">选择周、月、年或多个日期</p>
  154. <el-space wrap>
  155. <el-date-picker
  156. v-model="value1"
  157. type="week"
  158. class="!w-[160px]"
  159. format="YYYY年第ww周"
  160. placeholder="选择某年中的某周"
  161. :size="dynamicSize"
  162. :disabled="size === 'disabled'"
  163. />
  164. <el-date-picker
  165. v-model="value2"
  166. type="month"
  167. class="!w-[160px]"
  168. placeholder="选择某月"
  169. :size="dynamicSize"
  170. :disabled="size === 'disabled'"
  171. />
  172. <el-date-picker
  173. v-model="value3"
  174. type="year"
  175. class="!w-[160px]"
  176. placeholder="选择某年"
  177. :size="dynamicSize"
  178. :disabled="size === 'disabled'"
  179. />
  180. <el-date-picker
  181. v-model="value4"
  182. type="dates"
  183. class="!w-[160px]"
  184. placeholder="选择多个日期"
  185. :size="dynamicSize"
  186. :disabled="size === 'disabled'"
  187. />
  188. </el-space>
  189. <p class="mb-2 mt-4">选择一段时间</p>
  190. <el-date-picker
  191. v-model="value5"
  192. type="daterange"
  193. class="!w-[240px]"
  194. unlink-panels
  195. range-separator="至"
  196. start-placeholder="开始时间"
  197. end-placeholder="结束时间"
  198. :shortcuts="shortcuts1"
  199. :popper-options="{
  200. placement: 'bottom-start' // 下拉面板出现的位置,或 'top-start'、'bottom-end'、'top-end' 等,具体看 https://popper.js.org/docs/v2/constructors/#options
  201. }"
  202. :size="dynamicSize"
  203. :disabled="size === 'disabled'"
  204. />
  205. <p class="mb-2 mt-4">选择月份范围</p>
  206. <el-date-picker
  207. v-model="value6"
  208. type="monthrange"
  209. unlink-panels
  210. range-separator="至"
  211. start-placeholder="开始月份"
  212. end-placeholder="结束月份"
  213. :shortcuts="shortcuts2"
  214. :popper-options="{
  215. placement: 'bottom-start'
  216. }"
  217. :size="dynamicSize"
  218. :disabled="size === 'disabled'"
  219. />
  220. <p class="mb-2 mt-4">日期格式</p>
  221. <el-radio-group
  222. v-model="dateFormat"
  223. class="mb-2"
  224. :disabled="size === 'disabled'"
  225. @change="value7 = ''"
  226. >
  227. <el-radio label="">Date</el-radio>
  228. <el-radio label="YYYY-MM-DD">年月日</el-radio>
  229. <el-radio label="x">时间戳</el-radio>
  230. </el-radio-group>
  231. <br />
  232. <el-space wrap>
  233. <el-date-picker
  234. v-model="value7"
  235. type="date"
  236. class="!w-[160px]"
  237. placeholder="请选择日期"
  238. format="YYYY/MM/DD"
  239. :value-format="dateFormat"
  240. :size="dynamicSize"
  241. :disabled="size === 'disabled'"
  242. />
  243. <span class="ml-2">{{ value7 }}</span>
  244. </el-space>
  245. <p class="mb-2 mt-4">自定义前缀</p>
  246. <el-date-picker
  247. v-model="value8"
  248. type="date"
  249. class="!w-[160px]"
  250. placeholder="请选择日期"
  251. :prefix-icon="useRenderIcon('twemoji:spiral-calendar')"
  252. :size="dynamicSize"
  253. :disabled="size === 'disabled'"
  254. />
  255. <p class="mb-2 mt-4">自定义内容</p>
  256. <el-date-picker
  257. v-model="value9"
  258. type="date"
  259. placeholder="请选择日期"
  260. format="YYYY/MM/DD"
  261. value-format="YYYY-MM-DD"
  262. :size="dynamicSize"
  263. :disabled="size === 'disabled'"
  264. >
  265. <template #default="cell">
  266. <div class="cell" :class="{ current: cell.isCurrent }">
  267. <span class="text">{{ cell.text }}</span>
  268. <span v-if="isHoliday(cell)" class="holiday" />
  269. </div>
  270. </template>
  271. </el-date-picker>
  272. </el-card>
  273. </template>
  274. <style scoped>
  275. .cell {
  276. box-sizing: border-box;
  277. height: 30px;
  278. padding: 3px 0;
  279. }
  280. .cell .text {
  281. position: absolute;
  282. left: 50%;
  283. display: block;
  284. width: 24px;
  285. height: 24px;
  286. margin: 0 auto;
  287. line-height: 24px;
  288. border-radius: 50%;
  289. transform: translateX(-50%);
  290. }
  291. .cell.current .text {
  292. color: #fff;
  293. background: #626aef;
  294. }
  295. .cell .holiday {
  296. position: absolute;
  297. bottom: 0;
  298. left: 50%;
  299. width: 6px;
  300. height: 6px;
  301. background: var(--el-color-danger);
  302. border-radius: 50%;
  303. transform: translateX(-50%);
  304. }
  305. </style>