index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import { VantComponent } from '../common/component';
  2. import {
  3. ROW_HEIGHT,
  4. getNextDay,
  5. compareDay,
  6. copyDates,
  7. calcDateNum,
  8. formatMonthTitle,
  9. compareMonth,
  10. getMonths,
  11. getDayByOffset,
  12. } from './utils';
  13. import Toast from '../toast/toast';
  14. import { requestAnimationFrame } from '../common/utils';
  15. VantComponent({
  16. props: {
  17. title: {
  18. type: String,
  19. value: '日期选择',
  20. },
  21. color: String,
  22. show: {
  23. type: Boolean,
  24. observer(val) {
  25. if (val) {
  26. this.initRect();
  27. this.scrollIntoView();
  28. }
  29. },
  30. },
  31. formatter: null,
  32. confirmText: {
  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. confirmDisabledText: String,
  50. type: {
  51. type: String,
  52. value: 'single',
  53. observer: 'reset',
  54. },
  55. minDate: {
  56. type: null,
  57. value: Date.now(),
  58. },
  59. maxDate: {
  60. type: null,
  61. value: new Date(
  62. new Date().getFullYear(),
  63. new Date().getMonth() + 6,
  64. new Date().getDate()
  65. ).getTime(),
  66. },
  67. position: {
  68. type: String,
  69. value: 'bottom',
  70. },
  71. rowHeight: {
  72. type: null,
  73. value: ROW_HEIGHT,
  74. },
  75. round: {
  76. type: Boolean,
  77. value: true,
  78. },
  79. poppable: {
  80. type: Boolean,
  81. value: true,
  82. },
  83. showMark: {
  84. type: Boolean,
  85. value: true,
  86. },
  87. showTitle: {
  88. type: Boolean,
  89. value: true,
  90. },
  91. showConfirm: {
  92. type: Boolean,
  93. value: true,
  94. },
  95. showSubtitle: {
  96. type: Boolean,
  97. value: true,
  98. },
  99. safeAreaInsetBottom: {
  100. type: Boolean,
  101. value: true,
  102. },
  103. closeOnClickOverlay: {
  104. type: Boolean,
  105. value: true,
  106. },
  107. maxRange: {
  108. type: null,
  109. value: null,
  110. },
  111. firstDayOfWeek: {
  112. type: Number,
  113. value: 0,
  114. },
  115. },
  116. data: {
  117. subtitle: '',
  118. currentDate: null,
  119. scrollIntoView: '',
  120. },
  121. created() {
  122. this.setData({
  123. currentDate: this.getInitialDate(),
  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. getInitialDate() {
  155. const { type, defaultDate, minDate } = this.data;
  156. if (type === 'range') {
  157. const [startDay, endDay] = defaultDate || [];
  158. return [
  159. startDay || minDate,
  160. endDay || getNextDay(new Date(minDate)).getTime(),
  161. ];
  162. }
  163. if (type === 'multiple') {
  164. return defaultDate || [minDate];
  165. }
  166. return defaultDate || minDate;
  167. },
  168. scrollIntoView() {
  169. requestAnimationFrame(() => {
  170. const {
  171. currentDate,
  172. type,
  173. show,
  174. poppable,
  175. minDate,
  176. maxDate,
  177. } = this.data;
  178. // @ts-ignore
  179. const targetDate = type === 'single' ? currentDate : currentDate[0];
  180. const displayed = show || !poppable;
  181. if (!targetDate || !displayed) {
  182. return;
  183. }
  184. const months = getMonths(minDate, maxDate);
  185. months.some((month, index) => {
  186. if (compareMonth(month, targetDate) === 0) {
  187. this.setData({ scrollIntoView: `month${index}` });
  188. return true;
  189. }
  190. return false;
  191. });
  192. });
  193. },
  194. onOpen() {
  195. this.$emit('open');
  196. },
  197. onOpened() {
  198. this.$emit('opened');
  199. },
  200. onClose() {
  201. this.$emit('close');
  202. },
  203. onClosed() {
  204. this.$emit('closed');
  205. },
  206. onClickDay(event) {
  207. const { date } = event.detail;
  208. const { type, currentDate, allowSameDay } = this.data;
  209. if (type === 'range') {
  210. // @ts-ignore
  211. const [startDay, endDay] = currentDate;
  212. if (startDay && !endDay) {
  213. const compareToStart = compareDay(date, startDay);
  214. if (compareToStart === 1) {
  215. this.select([startDay, date], true);
  216. } else if (compareToStart === -1) {
  217. this.select([date, null]);
  218. } else if (allowSameDay) {
  219. this.select([date, date]);
  220. }
  221. } else {
  222. this.select([date, null]);
  223. }
  224. } else if (type === 'multiple') {
  225. let selectedIndex;
  226. // @ts-ignore
  227. const selected = currentDate.some((dateItem, index) => {
  228. const equal = compareDay(dateItem, date) === 0;
  229. if (equal) {
  230. selectedIndex = index;
  231. }
  232. return equal;
  233. });
  234. if (selected) {
  235. // @ts-ignore
  236. const cancelDate = currentDate.splice(selectedIndex, 1);
  237. this.setData({ currentDate });
  238. this.unselect(cancelDate);
  239. } else {
  240. // @ts-ignore
  241. this.select([...currentDate, date]);
  242. }
  243. } else {
  244. this.select(date, true);
  245. }
  246. },
  247. unselect(dateArray) {
  248. const date = dateArray[0];
  249. if (date) {
  250. this.$emit('unselect', copyDates(date));
  251. }
  252. },
  253. select(date, complete) {
  254. if (complete && this.data.type === 'range') {
  255. const valid = this.checkRange(date);
  256. if (!valid) {
  257. // auto selected to max range if showConfirm
  258. if (this.data.showConfirm) {
  259. this.emit([
  260. date[0],
  261. getDayByOffset(date[0], this.data.maxRange - 1),
  262. ]);
  263. } else {
  264. this.emit(date);
  265. }
  266. return;
  267. }
  268. }
  269. this.emit(date);
  270. if (complete && !this.data.showConfirm) {
  271. this.onConfirm();
  272. }
  273. },
  274. emit(date) {
  275. const getTime = (date) => (date instanceof Date ? date.getTime() : date);
  276. this.setData({
  277. currentDate: Array.isArray(date) ? date.map(getTime) : getTime(date),
  278. });
  279. this.$emit('select', copyDates(date));
  280. },
  281. checkRange(date) {
  282. const { maxRange, rangePrompt, showRangePrompt } = this.data;
  283. if (maxRange && calcDateNum(date) > maxRange) {
  284. if (showRangePrompt) {
  285. Toast({
  286. duration: 0,
  287. context: this,
  288. message: rangePrompt || `选择天数不能超过 ${maxRange} 天`,
  289. });
  290. }
  291. this.$emit('over-range');
  292. return false;
  293. }
  294. return true;
  295. },
  296. onConfirm() {
  297. if (
  298. this.data.type === 'range' &&
  299. !this.checkRange(this.data.currentDate)
  300. ) {
  301. return;
  302. }
  303. wx.nextTick(() => {
  304. // @ts-ignore
  305. this.$emit('confirm', copyDates(this.data.currentDate));
  306. });
  307. },
  308. },
  309. });