index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. defaultDate: {
  38. type: [Number, Array],
  39. observer(val) {
  40. this.setData({ currentDate: val });
  41. this.scrollIntoView();
  42. },
  43. },
  44. allowSameDay: Boolean,
  45. confirmDisabledText: String,
  46. type: {
  47. type: String,
  48. value: 'single',
  49. observer: 'reset',
  50. },
  51. minDate: {
  52. type: null,
  53. value: Date.now(),
  54. },
  55. maxDate: {
  56. type: null,
  57. value: new Date(
  58. new Date().getFullYear(),
  59. new Date().getMonth() + 6,
  60. new Date().getDate()
  61. ).getTime(),
  62. },
  63. position: {
  64. type: String,
  65. value: 'bottom',
  66. },
  67. rowHeight: {
  68. type: [Number, String],
  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: [Number, String],
  105. value: null,
  106. },
  107. },
  108. data: {
  109. subtitle: '',
  110. currentDate: null,
  111. scrollIntoView: '',
  112. },
  113. created() {
  114. this.setData({
  115. currentDate: this.getInitialDate(),
  116. });
  117. },
  118. mounted() {
  119. if (this.data.show || !this.data.poppable) {
  120. this.initRect();
  121. this.scrollIntoView();
  122. }
  123. },
  124. methods: {
  125. reset() {
  126. this.setData({ currentDate: this.getInitialDate() });
  127. this.scrollIntoView();
  128. },
  129. initRect() {
  130. if (this.contentObserver != null) {
  131. this.contentObserver.disconnect();
  132. }
  133. const contentObserver = this.createIntersectionObserver({
  134. thresholds: [0, 0.1, 0.9, 1],
  135. observeAll: true,
  136. });
  137. this.contentObserver = contentObserver;
  138. contentObserver.relativeTo('.van-calendar__body');
  139. contentObserver.observe('.month', (res) => {
  140. if (res.boundingClientRect.top <= res.relativeRect.top) {
  141. // @ts-ignore
  142. this.setData({ subtitle: formatMonthTitle(res.dataset.date) });
  143. }
  144. });
  145. },
  146. getInitialDate() {
  147. const { type, defaultDate, minDate } = this.data;
  148. if (type === 'range') {
  149. const [startDay, endDay] = defaultDate || [];
  150. return [
  151. startDay || minDate,
  152. endDay || getNextDay(new Date(minDate)).getTime(),
  153. ];
  154. }
  155. if (type === 'multiple') {
  156. return defaultDate || [minDate];
  157. }
  158. return defaultDate || minDate;
  159. },
  160. scrollIntoView() {
  161. requestAnimationFrame(() => {
  162. const {
  163. currentDate,
  164. type,
  165. show,
  166. poppable,
  167. minDate,
  168. maxDate,
  169. } = this.data;
  170. const targetDate = type === 'single' ? currentDate : currentDate[0];
  171. const displayed = show || !poppable;
  172. if (!targetDate || !displayed) {
  173. return;
  174. }
  175. const months = getMonths(minDate, maxDate);
  176. months.some((month, index) => {
  177. if (compareMonth(month, targetDate) === 0) {
  178. this.setData({ scrollIntoView: `month${index}` });
  179. return true;
  180. }
  181. return false;
  182. });
  183. });
  184. },
  185. onOpen() {
  186. this.$emit('open');
  187. },
  188. onOpened() {
  189. this.$emit('opened');
  190. },
  191. onClose() {
  192. this.$emit('close');
  193. },
  194. onClosed() {
  195. this.$emit('closed');
  196. },
  197. onClickDay(event) {
  198. const { date } = event.detail;
  199. const { type, currentDate, allowSameDay } = this.data;
  200. if (type === 'range') {
  201. const [startDay, endDay] = currentDate;
  202. if (startDay && !endDay) {
  203. const compareToStart = compareDay(date, startDay);
  204. if (compareToStart === 1) {
  205. this.select([startDay, date], true);
  206. } else if (compareToStart === -1) {
  207. this.select([date, null]);
  208. } else if (allowSameDay) {
  209. this.select([date, date]);
  210. }
  211. } else {
  212. this.select([date, null]);
  213. }
  214. } else if (type === 'multiple') {
  215. let selectedIndex;
  216. const selected = currentDate.some((dateItem, index) => {
  217. const equal = compareDay(dateItem, date) === 0;
  218. if (equal) {
  219. selectedIndex = index;
  220. }
  221. return equal;
  222. });
  223. if (selected) {
  224. const cancelDate = currentDate.splice(selectedIndex, 1);
  225. this.setData({ currentDate });
  226. this.unselect(cancelDate);
  227. } else {
  228. this.select([...currentDate, date]);
  229. }
  230. } else {
  231. this.select(date, true);
  232. }
  233. },
  234. unselect(dateArray) {
  235. const date = dateArray[0];
  236. if (date) {
  237. this.$emit('unselect', copyDates(date));
  238. }
  239. },
  240. select(date, complete) {
  241. if (complete && this.data.type === 'range') {
  242. const valid = this.checkRange(date);
  243. if (!valid) {
  244. // auto selected to max range if showConfirm
  245. if (this.data.showConfirm) {
  246. this.emit([
  247. date[0],
  248. getDayByOffset(date[0], this.data.maxRange - 1),
  249. ]);
  250. } else {
  251. this.emit(date);
  252. }
  253. return;
  254. }
  255. }
  256. this.emit(date);
  257. if (complete && !this.data.showConfirm) {
  258. this.onConfirm();
  259. }
  260. },
  261. emit(date) {
  262. const getTime = (date) => (date instanceof Date ? date.getTime() : date);
  263. this.setData({
  264. currentDate: Array.isArray(date) ? date.map(getTime) : getTime(date),
  265. });
  266. this.$emit('select', copyDates(date));
  267. },
  268. checkRange(date) {
  269. const { maxRange, rangePrompt } = this.data;
  270. if (maxRange && calcDateNum(date) > maxRange) {
  271. Toast({
  272. context: this,
  273. message: rangePrompt || `选择天数不能超过 ${maxRange} 天`,
  274. });
  275. return false;
  276. }
  277. return true;
  278. },
  279. onConfirm() {
  280. if (
  281. this.data.type === 'range' &&
  282. !this.checkRange(this.data.currentDate)
  283. ) {
  284. return;
  285. }
  286. wx.nextTick(() => {
  287. this.$emit('confirm', copyDates(this.data.currentDate));
  288. });
  289. },
  290. },
  291. });