index.vue 14 KB

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