index.js 12 KB

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