index.vue 14 KB

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