index.js 11 KB

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