CalendarDemo.svelte 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <!-- Calendar Demo -->
  2. <script>
  3. import { Cell, Calendar } from '../../../../components';
  4. let visible1 = false;
  5. let visible2 = false;
  6. let visible3 = false;
  7. let visible4 = false;
  8. let visible5 = false;
  9. let visible6 = false;
  10. let visible7 = false;
  11. let visible8 = false;
  12. let visible9 = false;
  13. let visible10 = false;
  14. let visible11 = false;
  15. let visible12 = false;
  16. let visible13 = false;
  17. let visible14 = false;
  18. let visible15 = false;
  19. let visible16 = false;
  20. let visible17 = false;
  21. let visible18 = false;
  22. let visible19 = false;
  23. let visible20 = false;
  24. let visible21 = false;
  25. let visible22 = false;
  26. let visible23 = false;
  27. // 随机取出当前日期后的6天,组成 infoDates 数组,其中 date 格式为 YYYYMMDD,月和日补足2位
  28. const now = new Date();
  29. const infoDates = [];
  30. for (let i = 0; i < 6; i++) {
  31. const date = new Date(now.getTime() + i * 24 * 60 * 60 * 1000);
  32. infoDates.push(
  33. `${date.getFullYear()}${(date.getMonth() + 1).toString().padStart(2, '0')}${date.getDate().toString().padStart(2, '0')}`
  34. );
  35. }
  36. // 循环 infoDates ,按照顺序,将每一项转为对象,date 为日期,info分别为出发、入住、购物、 拍照、离店、回程
  37. infoDates.forEach((date, index) => {
  38. infoDates[index] = {
  39. date,
  40. info: ['出发', '入住', '购物', '拍照', '离店', '回程'][index],
  41. };
  42. });
  43. // 获取当前日期的前7天和后7天共14天,组成数组 disabledDates,其中 date 格式为 YYYYMMDD,月和日补足2位
  44. const disabledDates = [];
  45. const before7 = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
  46. for (let i = 0; i < 14; i++) {
  47. const date = new Date(before7.getTime() + i * 24 * 60 * 60 * 1000);
  48. disabledDates.push(
  49. `${date.getFullYear()}${(date.getMonth() + 1).toString().padStart(2, '0')}${date.getDate().toString().padStart(2, '0')}`
  50. );
  51. }
  52. // 传入数字 n,返回当前月份往前推第 n 个月的月份,格式为 YYYYMM,月份补足2位
  53. const getInitMonth = n => {
  54. const now = new Date();
  55. const year = now.getFullYear();
  56. const month = now.getMonth() + 1;
  57. const newMonth = month - n;
  58. if (newMonth > 0) {
  59. return `${year}${newMonth.toString().padStart(2, '0')}`;
  60. } else {
  61. return `${year - 1}${(12 + newMonth).toString().padStart(2, '0')}`;
  62. }
  63. };
  64. const initMonth = getInitMonth(3);
  65. const quickSelects = ['week', 'month', 'quarter', -3, -7, -30, 3, 7, 30];
  66. const quickSelectsDay = [-5, -2, 3, 7];
  67. let selectedDates = [];
  68. const getSelectedDatesFunc = e => {
  69. selectedDates = e.detail.dates;
  70. };
  71. let selectedFormatDates = [];
  72. const getSelectedFormatDatesFunc = e => {
  73. selectedFormatDates = e.detail.dates;
  74. };
  75. </script>
  76. <div class="py-4">
  77. <Cell title="基础用法" on:click={() => (visible1 = true)} />
  78. <Calendar bind:visible={visible1} />
  79. <Cell title="多选" on:click={() => (visible7 = true)} />
  80. <Calendar bind:visible={visible7} mode="multiple" />
  81. <Cell title="范围选择" on:click={() => (visible8 = true)} />
  82. <Calendar bind:visible={visible8} mode="range" />
  83. <Cell title="从周日开始" on:click={() => (visible2 = true)} />
  84. <Calendar bind:visible={visible2} startSunday />
  85. <Cell title="周末文字标红" on:click={() => (visible3 = true)} />
  86. <Calendar bind:visible={visible3} weekendRed />
  87. <Cell title="不使用卡片样式且加水印" on:click={() => (visible4 = true)} />
  88. <Calendar bind:visible={visible4} monthCard={false} monthMark />
  89. <Cell title="高一点" on:click={() => (visible5 = true)} />
  90. <Calendar bind:visible={visible5} height={60} />
  91. <Cell title="自定义显示信息的日期" on:click={() => (visible6 = true)} />
  92. <Calendar bind:visible={visible6} {infoDates} />
  93. <Cell title="不要圆角" on:click={() => (visible9 = true)} />
  94. <Calendar bind:visible={visible9} mode="range" radius="none" />
  95. <Cell title="加大圆角" on:click={() => (visible10 = true)} />
  96. <Calendar bind:visible={visible10} mode="range" radius="2xl" />
  97. <Cell title="滚动时关闭动画" on:click={() => (visible18 = true)} />
  98. <Calendar bind:visible={visible18} useAnimation={false} />
  99. <Cell title="配置确认按钮样式" on:click={() => (visible11 = true)} />
  100. <Calendar bind:visible={visible11} button={{ radius: 'full' }} />
  101. <Cell title="顶部来点圆角" on:click={() => (visible19 = true)} />
  102. <Calendar bind:visible={visible19} popup={{ radius: 'xl' }} />
  103. <Cell title="定义开始与结束月" on:click={() => (visible12 = true)} />
  104. <Calendar bind:visible={visible12} startMonth="202101" endMonth="202106" />
  105. <Cell title="自定义不可选日期" on:click={() => (visible13 = true)} />
  106. <Calendar bind:visible={visible13} {disabledDates} mode="range" />
  107. <Cell title="多选或范围选择时不显示已选天数" on:click={() => (visible14 = true)} />
  108. <Calendar bind:visible={visible14} mode="range" showSelectedDay={false} />
  109. <Cell title="自定义初始显示月份" subTitle="开始月份为当前月份前第三个月" on:click={() => (visible15 = true)} />
  110. <Calendar bind:visible={visible15} {initMonth} />
  111. <Cell title="展示一些快速选择项" on:click={() => (visible16 = true)} />
  112. <Calendar bind:visible={visible16} mode="range" {quickSelects} />
  113. <Cell title="快速选择天数时包含当天" on:click={() => (visible23 = true)} />
  114. <Calendar bind:visible={visible23} mode="range" quickSelects={quickSelectsDay} includeToday />
  115. <Cell title="从周日开始快速选择本周" on:click={() => (visible17 = true)} />
  116. <Calendar bind:visible={visible17} mode="range" startSunday quickSelects={['week']} />
  117. <div class="px-4">
  118. {#if selectedDates.length}
  119. 当前选定了以下共 {selectedDates.length} 天:
  120. {:else}
  121. <div>请选定日期</div>
  122. {/if}
  123. </div>
  124. <div class="grid grid-cols-4 gap-2 p-2 text-primary dark:text-dark">
  125. {#each selectedDates as item}
  126. <div class="text-center">{item}</div>
  127. {/each}
  128. </div>
  129. <Cell title="获取返回的选定日期" on:click={() => (visible20 = true)} />
  130. <Calendar bind:visible={visible20} mode="range" on:confirm={getSelectedDatesFunc} />
  131. <div class="px-4">
  132. {#if selectedFormatDates.length}
  133. 当前选定了以下共 {selectedFormatDates.length} 天:
  134. {:else}
  135. <div>请选定日期</div>
  136. {/if}
  137. </div>
  138. <div class="grid grid-cols-3 gap-2 p-2 text-primary dark:text-dark text-sm">
  139. {#each selectedFormatDates as item}
  140. <div class="text-center">{item}</div>
  141. {/each}
  142. </div>
  143. <Cell title="自定义返回的日期格式" on:click={() => (visible21 = true)} />
  144. <Calendar bind:visible={visible21} mode="range" outFormat="Y年M月D日" on:confirm={getSelectedFormatDatesFunc} />
  145. <Cell title="今日日期关闭高亮显示" on:click={() => (visible22 = true)} />
  146. <Calendar bind:visible={visible22} highlightToday={false} />
  147. </div>