Calendar.mjs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. import { createVNode as _createVNode, mergeProps as _mergeProps } from "vue";
  2. import { ref, watch, computed, defineComponent } from "vue";
  3. import { pick, isDate, truthProp, numericProp, getScrollTop, makeStringProp, makeNumericProp } from "../utils/index.mjs";
  4. import { t, bem, name, getToday, cloneDate, cloneDates, getPrevDay, getNextDay, compareDay, calcDateNum, compareMonth, getDayByOffset } from "./utils.mjs";
  5. import { raf, useRect, onMountedOrActivated } from "@vant/use";
  6. import { useRefs } from "../composables/use-refs.mjs";
  7. import { useExpose } from "../composables/use-expose.mjs";
  8. import { Popup } from "../popup/index.mjs";
  9. import { Button } from "../button/index.mjs";
  10. import { Toast } from "../toast/index.mjs";
  11. import CalendarMonth from "./CalendarMonth.mjs";
  12. import CalendarHeader from "./CalendarHeader.mjs";
  13. const calendarProps = {
  14. show: Boolean,
  15. type: makeStringProp("single"),
  16. title: String,
  17. color: String,
  18. round: truthProp,
  19. readonly: Boolean,
  20. poppable: truthProp,
  21. maxRange: makeNumericProp(null),
  22. position: makeStringProp("bottom"),
  23. teleport: [String, Object],
  24. showMark: truthProp,
  25. showTitle: truthProp,
  26. formatter: Function,
  27. rowHeight: numericProp,
  28. confirmText: String,
  29. rangePrompt: String,
  30. lazyRender: truthProp,
  31. showConfirm: truthProp,
  32. defaultDate: [Date, Array],
  33. allowSameDay: Boolean,
  34. showSubtitle: truthProp,
  35. closeOnPopstate: truthProp,
  36. showRangePrompt: truthProp,
  37. confirmDisabledText: String,
  38. closeOnClickOverlay: truthProp,
  39. safeAreaInsetTop: Boolean,
  40. safeAreaInsetBottom: truthProp,
  41. minDate: {
  42. type: Date,
  43. validator: isDate,
  44. default: getToday
  45. },
  46. maxDate: {
  47. type: Date,
  48. validator: isDate,
  49. default: () => {
  50. const now = getToday();
  51. return new Date(now.getFullYear(), now.getMonth() + 6, now.getDate());
  52. }
  53. },
  54. firstDayOfWeek: {
  55. type: numericProp,
  56. default: 0,
  57. validator: (val) => val >= 0 && val <= 6
  58. }
  59. };
  60. var stdin_default = defineComponent({
  61. name,
  62. props: calendarProps,
  63. emits: ["select", "confirm", "unselect", "month-show", "over-range", "update:show", "click-subtitle"],
  64. setup(props, {
  65. emit,
  66. slots
  67. }) {
  68. const limitDateRange = (date, minDate = props.minDate, maxDate = props.maxDate) => {
  69. if (compareDay(date, minDate) === -1) {
  70. return minDate;
  71. }
  72. if (compareDay(date, maxDate) === 1) {
  73. return maxDate;
  74. }
  75. return date;
  76. };
  77. const getInitialDate = (defaultDate = props.defaultDate) => {
  78. const {
  79. type,
  80. minDate,
  81. maxDate,
  82. allowSameDay
  83. } = props;
  84. if (defaultDate === null) {
  85. return defaultDate;
  86. }
  87. const now = getToday();
  88. if (type === "range") {
  89. if (!Array.isArray(defaultDate)) {
  90. defaultDate = [];
  91. }
  92. const start = limitDateRange(defaultDate[0] || now, minDate, allowSameDay ? maxDate : getPrevDay(maxDate));
  93. const end = limitDateRange(defaultDate[1] || now, allowSameDay ? minDate : getNextDay(minDate));
  94. return [start, end];
  95. }
  96. if (type === "multiple") {
  97. if (Array.isArray(defaultDate)) {
  98. return defaultDate.map((date) => limitDateRange(date));
  99. }
  100. return [limitDateRange(now)];
  101. }
  102. if (!defaultDate || Array.isArray(defaultDate)) {
  103. defaultDate = now;
  104. }
  105. return limitDateRange(defaultDate);
  106. };
  107. let bodyHeight;
  108. const bodyRef = ref();
  109. const subtitle = ref("");
  110. const currentDate = ref(getInitialDate());
  111. const [monthRefs, setMonthRefs] = useRefs();
  112. const dayOffset = computed(() => props.firstDayOfWeek ? +props.firstDayOfWeek % 7 : 0);
  113. const months = computed(() => {
  114. const months2 = [];
  115. const cursor = new Date(props.minDate);
  116. cursor.setDate(1);
  117. do {
  118. months2.push(new Date(cursor));
  119. cursor.setMonth(cursor.getMonth() + 1);
  120. } while (compareMonth(cursor, props.maxDate) !== 1);
  121. return months2;
  122. });
  123. const buttonDisabled = computed(() => {
  124. if (currentDate.value) {
  125. if (props.type === "range") {
  126. return !currentDate.value[0] || !currentDate.value[1];
  127. }
  128. if (props.type === "multiple") {
  129. return !currentDate.value.length;
  130. }
  131. }
  132. return !currentDate.value;
  133. });
  134. const getSelectedDate = () => currentDate.value;
  135. const onScroll = () => {
  136. const top = getScrollTop(bodyRef.value);
  137. const bottom = top + bodyHeight;
  138. const heights = months.value.map((item, index) => monthRefs.value[index].getHeight());
  139. const heightSum = heights.reduce((a, b) => a + b, 0);
  140. if (bottom > heightSum && top > 0) {
  141. return;
  142. }
  143. let height = 0;
  144. let currentMonth;
  145. const visibleRange = [-1, -1];
  146. for (let i = 0; i < months.value.length; i++) {
  147. const month = monthRefs.value[i];
  148. const visible = height <= bottom && height + heights[i] >= top;
  149. if (visible) {
  150. visibleRange[1] = i;
  151. if (!currentMonth) {
  152. currentMonth = month;
  153. visibleRange[0] = i;
  154. }
  155. if (!monthRefs.value[i].showed) {
  156. monthRefs.value[i].showed = true;
  157. emit("month-show", {
  158. date: month.date,
  159. title: month.getTitle()
  160. });
  161. }
  162. }
  163. height += heights[i];
  164. }
  165. months.value.forEach((month, index) => {
  166. const visible = index >= visibleRange[0] - 1 && index <= visibleRange[1] + 1;
  167. monthRefs.value[index].setVisible(visible);
  168. });
  169. if (currentMonth) {
  170. subtitle.value = currentMonth.getTitle();
  171. }
  172. };
  173. const scrollToDate = (targetDate) => {
  174. raf(() => {
  175. months.value.some((month, index) => {
  176. if (compareMonth(month, targetDate) === 0) {
  177. if (bodyRef.value) {
  178. monthRefs.value[index].scrollToDate(bodyRef.value, targetDate);
  179. }
  180. return true;
  181. }
  182. return false;
  183. });
  184. onScroll();
  185. });
  186. };
  187. const scrollToCurrentDate = () => {
  188. if (props.poppable && !props.show) {
  189. return;
  190. }
  191. if (currentDate.value) {
  192. const targetDate = props.type === "single" ? currentDate.value : currentDate.value[0];
  193. if (isDate(targetDate)) {
  194. scrollToDate(targetDate);
  195. }
  196. } else {
  197. raf(onScroll);
  198. }
  199. };
  200. const init = () => {
  201. if (props.poppable && !props.show) {
  202. return;
  203. }
  204. raf(() => {
  205. bodyHeight = Math.floor(useRect(bodyRef).height);
  206. });
  207. scrollToCurrentDate();
  208. };
  209. const reset = (date = getInitialDate()) => {
  210. currentDate.value = date;
  211. scrollToCurrentDate();
  212. };
  213. const checkRange = (date) => {
  214. const {
  215. maxRange,
  216. rangePrompt,
  217. showRangePrompt
  218. } = props;
  219. if (maxRange && calcDateNum(date) > maxRange) {
  220. if (showRangePrompt) {
  221. Toast(rangePrompt || t("rangePrompt", maxRange));
  222. }
  223. emit("over-range");
  224. return false;
  225. }
  226. return true;
  227. };
  228. const onConfirm = () => {
  229. var _a;
  230. return emit("confirm", (_a = currentDate.value) != null ? _a : cloneDates(currentDate.value));
  231. };
  232. const select = (date, complete) => {
  233. const setCurrentDate = (date2) => {
  234. currentDate.value = date2;
  235. emit("select", cloneDates(date2));
  236. };
  237. if (complete && props.type === "range") {
  238. const valid = checkRange(date);
  239. if (!valid) {
  240. setCurrentDate([date[0], getDayByOffset(date[0], +props.maxRange - 1)]);
  241. return;
  242. }
  243. }
  244. setCurrentDate(date);
  245. if (complete && !props.showConfirm) {
  246. onConfirm();
  247. }
  248. };
  249. const getDisabledDate = (disabledDays2, startDay, date) => {
  250. var _a;
  251. return (_a = disabledDays2.find((day) => compareDay(startDay, day.date) === -1 && compareDay(day.date, date) === -1)) == null ? void 0 : _a.date;
  252. };
  253. const disabledDays = computed(() => monthRefs.value.reduce((arr, ref2) => {
  254. var _a, _b;
  255. arr.push(...(_b = (_a = ref2.disabledDays) == null ? void 0 : _a.value) != null ? _b : []);
  256. return arr;
  257. }, []));
  258. const onClickDay = (item) => {
  259. if (props.readonly || !item.date) {
  260. return;
  261. }
  262. const {
  263. date
  264. } = item;
  265. const {
  266. type
  267. } = props;
  268. if (type === "range") {
  269. if (!currentDate.value) {
  270. select([date]);
  271. return;
  272. }
  273. const [startDay, endDay] = currentDate.value;
  274. if (startDay && !endDay) {
  275. const compareToStart = compareDay(date, startDay);
  276. if (compareToStart === 1) {
  277. const disabledDay = getDisabledDate(disabledDays.value, startDay, date);
  278. if (disabledDay) {
  279. const endDay2 = getPrevDay(disabledDay);
  280. if (compareDay(startDay, endDay2) === -1) {
  281. select([startDay, endDay2]);
  282. } else {
  283. select([date]);
  284. }
  285. } else {
  286. select([startDay, date], true);
  287. }
  288. } else if (compareToStart === -1) {
  289. select([date]);
  290. } else if (props.allowSameDay) {
  291. select([date, date], true);
  292. }
  293. } else {
  294. select([date]);
  295. }
  296. } else if (type === "multiple") {
  297. if (!currentDate.value) {
  298. select([date]);
  299. return;
  300. }
  301. const dates = currentDate.value;
  302. const selectedIndex = dates.findIndex((dateItem) => compareDay(dateItem, date) === 0);
  303. if (selectedIndex !== -1) {
  304. const [unselectedDate] = dates.splice(selectedIndex, 1);
  305. emit("unselect", cloneDate(unselectedDate));
  306. } else if (props.maxRange && dates.length >= props.maxRange) {
  307. Toast(props.rangePrompt || t("rangePrompt", props.maxRange));
  308. } else {
  309. select([...dates, date]);
  310. }
  311. } else {
  312. select(date, true);
  313. }
  314. };
  315. const updateShow = (value) => emit("update:show", value);
  316. const renderMonth = (date, index) => {
  317. const showMonthTitle = index !== 0 || !props.showSubtitle;
  318. return _createVNode(CalendarMonth, _mergeProps({
  319. "ref": setMonthRefs(index),
  320. "date": date,
  321. "currentDate": currentDate.value,
  322. "showMonthTitle": showMonthTitle,
  323. "firstDayOfWeek": dayOffset.value
  324. }, pick(props, ["type", "color", "minDate", "maxDate", "showMark", "formatter", "rowHeight", "lazyRender", "showSubtitle", "allowSameDay"]), {
  325. "onClick": onClickDay
  326. }), pick(slots, ["top-info", "bottom-info"]));
  327. };
  328. const renderFooterButton = () => {
  329. if (slots.footer) {
  330. return slots.footer();
  331. }
  332. if (props.showConfirm) {
  333. const slot = slots["confirm-text"];
  334. const disabled = buttonDisabled.value;
  335. const text = disabled ? props.confirmDisabledText : props.confirmText;
  336. return _createVNode(Button, {
  337. "round": true,
  338. "block": true,
  339. "type": "danger",
  340. "color": props.color,
  341. "class": bem("confirm"),
  342. "disabled": disabled,
  343. "nativeType": "button",
  344. "onClick": onConfirm
  345. }, {
  346. default: () => [slot ? slot({
  347. disabled
  348. }) : text || t("confirm")]
  349. });
  350. }
  351. };
  352. const renderFooter = () => _createVNode("div", {
  353. "class": [bem("footer"), {
  354. "van-safe-area-bottom": props.safeAreaInsetBottom
  355. }]
  356. }, [renderFooterButton()]);
  357. const renderCalendar = () => _createVNode("div", {
  358. "class": bem()
  359. }, [_createVNode(CalendarHeader, {
  360. "title": props.title,
  361. "subtitle": subtitle.value,
  362. "showTitle": props.showTitle,
  363. "showSubtitle": props.showSubtitle,
  364. "firstDayOfWeek": dayOffset.value,
  365. "onClick-subtitle": (event) => emit("click-subtitle", event)
  366. }, pick(slots, ["title", "subtitle"])), _createVNode("div", {
  367. "ref": bodyRef,
  368. "class": bem("body"),
  369. "onScroll": onScroll
  370. }, [months.value.map(renderMonth)]), renderFooter()]);
  371. watch(() => props.show, init);
  372. watch(() => [props.type, props.minDate, props.maxDate], () => reset(getInitialDate(currentDate.value)));
  373. watch(() => props.defaultDate, (value = null) => {
  374. currentDate.value = value;
  375. scrollToCurrentDate();
  376. });
  377. useExpose({
  378. reset,
  379. scrollToDate,
  380. getSelectedDate
  381. });
  382. onMountedOrActivated(init);
  383. return () => {
  384. if (props.poppable) {
  385. return _createVNode(Popup, {
  386. "show": props.show,
  387. "class": bem("popup"),
  388. "round": props.round,
  389. "position": props.position,
  390. "closeable": props.showTitle || props.showSubtitle,
  391. "teleport": props.teleport,
  392. "closeOnPopstate": props.closeOnPopstate,
  393. "safeAreaInsetTop": props.safeAreaInsetTop,
  394. "closeOnClickOverlay": props.closeOnClickOverlay,
  395. "onUpdate:show": updateShow
  396. }, {
  397. default: renderCalendar
  398. });
  399. }
  400. return renderCalendar();
  401. };
  402. }
  403. });
  404. export {
  405. stdin_default as default
  406. };