index.js 11 KB

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