index.vue 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <template>
  2. <uni-shadow-root class="vant-dist-calendar-index"><van-popup v-if="poppable" :custom-class="'van-calendar__popup--'+(position)" close-icon-class="van-calendar__close-icon" :show="show" :round="round" :position="position" :closeable="showTitle || showSubtitle" :close-on-click-overlay="closeOnClickOverlay" @enter="onOpen" @close="onClose" @after-enter="onOpened" @after-leave="onClosed">
  3. <include src="./calendar.wxml"></include>
  4. </van-popup>
  5. <include v-else src="./calendar.wxml"></include>
  6. <van-toast id="van-toast"></van-toast></uni-shadow-root>
  7. </template>
  8. <wxs src="./index.wxs" module="computed"></wxs><wxs src="../wxs/utils.wxs" module="utils"></wxs>
  9. <script>
  10. const __wxTemplateComponentProps = {}
  11. import __wxTemplateComponent0 from './calendar.vue'
  12. import Header from './components/header/index.vue'
  13. import Month from './components/month/index.vue'
  14. import VanButton from '../button/index.vue'
  15. import VanPopup from '../popup/index.vue'
  16. import VanToast from '../toast/index.vue'
  17. global['__wxVueOptions'] = {components:{'header': Header,'month': Month,'van-button': VanButton,'van-popup': VanPopup,'van-toast': VanToast,}}
  18. global['__wxRoute'] = 'vant/dist/calendar/index'
  19. import { VantComponent } from '../common/component';
  20. import { ROW_HEIGHT, getPrevDay, getNextDay, getToday, compareDay, copyDates, calcDateNum, formatMonthTitle, compareMonth, getMonths, getDayByOffset, } from './utils';
  21. import Toast from '../toast/toast';
  22. import { requestAnimationFrame } from '../common/utils';
  23. const initialMinDate = getToday().getTime();
  24. const initialMaxDate = (() => {
  25. const now = getToday();
  26. return new Date(now.getFullYear(), now.getMonth() + 6, now.getDate()).getTime();
  27. })();
  28. VantComponent({
  29. props: {
  30. title: {
  31. type: String,
  32. value: '日期选择',
  33. },
  34. color: String,
  35. show: {
  36. type: Boolean,
  37. observer(val) {
  38. if (val) {
  39. this.initRect();
  40. this.scrollIntoView();
  41. }
  42. },
  43. },
  44. formatter: null,
  45. confirmText: {
  46. type: String,
  47. value: '确定',
  48. },
  49. confirmDisabledText: {
  50. type: String,
  51. value: '确定',
  52. },
  53. rangePrompt: String,
  54. showRangePrompt: {
  55. type: Boolean,
  56. value: true,
  57. },
  58. defaultDate: {
  59. type: null,
  60. observer(val) {
  61. this.setData({ currentDate: val });
  62. this.scrollIntoView();
  63. },
  64. },
  65. allowSameDay: Boolean,
  66. type: {
  67. type: String,
  68. value: 'single',
  69. observer: 'reset',
  70. },
  71. minDate: {
  72. type: Number,
  73. value: initialMinDate,
  74. },
  75. maxDate: {
  76. type: Number,
  77. value: initialMaxDate,
  78. },
  79. position: {
  80. type: String,
  81. value: 'bottom',
  82. },
  83. rowHeight: {
  84. type: null,
  85. value: ROW_HEIGHT,
  86. },
  87. round: {
  88. type: Boolean,
  89. value: true,
  90. },
  91. poppable: {
  92. type: Boolean,
  93. value: true,
  94. },
  95. showMark: {
  96. type: Boolean,
  97. value: true,
  98. },
  99. showTitle: {
  100. type: Boolean,
  101. value: true,
  102. },
  103. showConfirm: {
  104. type: Boolean,
  105. value: true,
  106. },
  107. showSubtitle: {
  108. type: Boolean,
  109. value: true,
  110. },
  111. safeAreaInsetBottom: {
  112. type: Boolean,
  113. value: true,
  114. },
  115. closeOnClickOverlay: {
  116. type: Boolean,
  117. value: true,
  118. },
  119. maxRange: {
  120. type: null,
  121. value: null,
  122. },
  123. firstDayOfWeek: {
  124. type: Number,
  125. value: 0,
  126. },
  127. },
  128. data: {
  129. subtitle: '',
  130. currentDate: null,
  131. scrollIntoView: '',
  132. },
  133. created() {
  134. this.setData({
  135. currentDate: this.getInitialDate(this.data.defaultDate),
  136. });
  137. },
  138. mounted() {
  139. if (this.data.show || !this.data.poppable) {
  140. this.initRect();
  141. this.scrollIntoView();
  142. }
  143. },
  144. methods: {
  145. reset() {
  146. this.setData({ currentDate: this.getInitialDate() });
  147. this.scrollIntoView();
  148. },
  149. initRect() {
  150. if (this.contentObserver != null) {
  151. this.contentObserver.disconnect();
  152. }
  153. const contentObserver = this.createIntersectionObserver({
  154. thresholds: [0, 0.1, 0.9, 1],
  155. observeAll: true,
  156. });
  157. this.contentObserver = contentObserver;
  158. contentObserver.relativeTo('.van-calendar__body');
  159. contentObserver.observe('.month', (res) => {
  160. if (res.boundingClientRect.top <= res.relativeRect.top) {
  161. // @ts-ignore
  162. this.setData({ subtitle: formatMonthTitle(res.dataset.date) });
  163. }
  164. });
  165. },
  166. limitDateRange(date, minDate = null, maxDate = null) {
  167. minDate = minDate || this.data.minDate;
  168. maxDate = maxDate || this.data.maxDate;
  169. if (compareDay(date, minDate) === -1) {
  170. return minDate;
  171. }
  172. if (compareDay(date, maxDate) === 1) {
  173. return maxDate;
  174. }
  175. return date;
  176. },
  177. getInitialDate(defaultDate = null) {
  178. const { type, minDate, maxDate } = this.data;
  179. const now = getToday().getTime();
  180. if (type === 'range') {
  181. if (!Array.isArray(defaultDate)) {
  182. defaultDate = [];
  183. }
  184. const [startDay, endDay] = defaultDate || [];
  185. const start = this.limitDateRange(startDay || now, minDate, getPrevDay(new Date(maxDate)).getTime());
  186. const end = this.limitDateRange(endDay || now, getNextDay(new Date(minDate)).getTime());
  187. return [start, end];
  188. }
  189. if (type === 'multiple') {
  190. if (Array.isArray(defaultDate)) {
  191. return defaultDate.map((date) => this.limitDateRange(date));
  192. }
  193. return [this.limitDateRange(now)];
  194. }
  195. if (!defaultDate || Array.isArray(defaultDate)) {
  196. defaultDate = now;
  197. }
  198. return this.limitDateRange(defaultDate);
  199. },
  200. scrollIntoView() {
  201. requestAnimationFrame(() => {
  202. const { currentDate, type, show, poppable, minDate, maxDate, } = this.data;
  203. // @ts-ignore
  204. const targetDate = type === 'single' ? currentDate : currentDate[0];
  205. const displayed = show || !poppable;
  206. if (!targetDate || !displayed) {
  207. return;
  208. }
  209. const months = getMonths(minDate, maxDate);
  210. months.some((month, index) => {
  211. if (compareMonth(month, targetDate) === 0) {
  212. this.setData({ scrollIntoView: `month${index}` });
  213. return true;
  214. }
  215. return false;
  216. });
  217. });
  218. },
  219. onOpen() {
  220. this.$emit('open');
  221. },
  222. onOpened() {
  223. this.$emit('opened');
  224. },
  225. onClose() {
  226. this.$emit('close');
  227. },
  228. onClosed() {
  229. this.$emit('closed');
  230. },
  231. onClickDay(event) {
  232. const { date } = event.detail;
  233. const { type, currentDate, allowSameDay } = this.data;
  234. if (type === 'range') {
  235. // @ts-ignore
  236. const [startDay, endDay] = currentDate;
  237. if (startDay && !endDay) {
  238. const compareToStart = compareDay(date, startDay);
  239. if (compareToStart === 1) {
  240. this.select([startDay, date], true);
  241. }
  242. else if (compareToStart === -1) {
  243. this.select([date, null]);
  244. }
  245. else if (allowSameDay) {
  246. this.select([date, date]);
  247. }
  248. }
  249. else {
  250. this.select([date, null]);
  251. }
  252. }
  253. else if (type === 'multiple') {
  254. let selectedIndex;
  255. // @ts-ignore
  256. const selected = currentDate.some((dateItem, index) => {
  257. const equal = compareDay(dateItem, date) === 0;
  258. if (equal) {
  259. selectedIndex = index;
  260. }
  261. return equal;
  262. });
  263. if (selected) {
  264. // @ts-ignore
  265. const cancelDate = currentDate.splice(selectedIndex, 1);
  266. this.setData({ currentDate });
  267. this.unselect(cancelDate);
  268. }
  269. else {
  270. // @ts-ignore
  271. this.select([...currentDate, date]);
  272. }
  273. }
  274. else {
  275. this.select(date, true);
  276. }
  277. },
  278. unselect(dateArray) {
  279. const date = dateArray[0];
  280. if (date) {
  281. this.$emit('unselect', copyDates(date));
  282. }
  283. },
  284. select(date, complete) {
  285. if (complete && this.data.type === 'range') {
  286. const valid = this.checkRange(date);
  287. if (!valid) {
  288. // auto selected to max range if showConfirm
  289. if (this.data.showConfirm) {
  290. this.emit([
  291. date[0],
  292. getDayByOffset(date[0], this.data.maxRange - 1),
  293. ]);
  294. }
  295. else {
  296. this.emit(date);
  297. }
  298. return;
  299. }
  300. }
  301. this.emit(date);
  302. if (complete && !this.data.showConfirm) {
  303. this.onConfirm();
  304. }
  305. },
  306. emit(date) {
  307. const getTime = (date) => date instanceof Date ? date.getTime() : date;
  308. this.setData({
  309. currentDate: Array.isArray(date) ? date.map(getTime) : getTime(date),
  310. });
  311. this.$emit('select', copyDates(date));
  312. },
  313. checkRange(date) {
  314. const { maxRange, rangePrompt, showRangePrompt } = this.data;
  315. if (maxRange && calcDateNum(date) > maxRange) {
  316. if (showRangePrompt) {
  317. Toast({
  318. context: this,
  319. message: rangePrompt || `选择天数不能超过 ${maxRange} 天`,
  320. });
  321. }
  322. this.$emit('over-range');
  323. return false;
  324. }
  325. return true;
  326. },
  327. onConfirm() {
  328. if (this.data.type === 'range' &&
  329. !this.checkRange(this.data.currentDate)) {
  330. return;
  331. }
  332. wx.nextTick(() => {
  333. // @ts-ignore
  334. this.$emit('confirm', copyDates(this.data.currentDate));
  335. });
  336. },
  337. onClickSubtitle(event) {
  338. this.$emit('click-subtitle', event);
  339. },
  340. },
  341. });
  342. export default global['__wxComponents']['vant/dist/calendar/index']
  343. </script>
  344. <style platform="mp-weixin">
  345. @import '../common/index.css';.van-calendar{display:flex;flex-direction:column;height:100%;height:var(--calendar-height,100%);background-color:#fff;background-color:var(--calendar-background-color,#fff)}.van-calendar__close-icon{top:11px}.van-calendar__popup--bottom,.van-calendar__popup--top{height:80%;height:var(--calendar-popup-height,80%)}.van-calendar__popup--left,.van-calendar__popup--right{height:100%}.van-calendar__body{flex:1;overflow:auto;-webkit-overflow-scrolling:touch}.van-calendar__footer{flex-shrink:0;padding:0 16px;padding:0 var(--padding-md,16px)}.van-calendar__footer--safe-area-inset-bottom{padding-bottom:env(safe-area-inset-bottom)}.van-calendar__footer+.van-calendar__footer,.van-calendar__footer:empty{display:none}.van-calendar__footer:empty+.van-calendar__footer{display:block!important}.van-calendar__confirm{height:36px!important;height:var(--calendar-confirm-button-height,36px)!important;margin:7px 0!important;margin:var(--calendar-confirm-button-margin,7px 0)!important;line-height:34px!important;line-height:var(--calendar-confirm-button-line-height,34px)!important}
  346. </style>