index.vue 13 KB

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