u-calendar.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <template>
  2. <u-popup
  3. :show="show"
  4. mode="bottom"
  5. closeable
  6. @close="close"
  7. :round="round"
  8. :closeOnClickOverlay="closeOnClickOverlay"
  9. >
  10. <view class="u-calendar">
  11. <uHeader
  12. :title="title"
  13. :subtitle="subtitle"
  14. :showSubtitle="showSubtitle"
  15. :showTitle="showTitle"
  16. ></uHeader>
  17. <scroll-view
  18. :style="{
  19. height: $u.addUnit(listHeight)
  20. }"
  21. scroll-y
  22. @scroll="onScroll"
  23. :scrollIntoView="scrollIntoView"
  24. >
  25. <uMonth
  26. :color="color"
  27. :rowHeight="rowHeight"
  28. :showMark="showMark"
  29. :months="months"
  30. :mode="mode"
  31. :maxCount="maxCount"
  32. :startText="startText"
  33. :endText="endText"
  34. :defaultDate="defaultDate"
  35. :minDate="minDate"
  36. :maxDate="maxDate"
  37. :maxMonth="maxMonth"
  38. :readonly="readonly"
  39. :maxRange="maxRange"
  40. :rangePrompt="rangePrompt"
  41. :showRangePrompt="showRangePrompt"
  42. :allowSameDay="allowSameDay"
  43. ref="month"
  44. @monthSelected="monthSelected"
  45. @updateMonthTop="updateMonthTop"
  46. ></uMonth>
  47. </scroll-view>
  48. <slot name="footer" v-if="showConfirm">
  49. <view class="u-calendar__confirm">
  50. <u-button
  51. shape="circle"
  52. :text="buttonDisabled ? confirmDisabledText : confirmText"
  53. :color="color"
  54. @click="confirm"
  55. :disabled="buttonDisabled"
  56. ></u-button>
  57. </view>
  58. </slot>
  59. </view>
  60. </u-popup>
  61. </template>
  62. <script>
  63. import uHeader from './header.vue';
  64. import uMonth from './month.vue';
  65. import props from './props.js';
  66. import util from './util.js';
  67. import dayjs from '../../libs/util/dayjs.js';
  68. import Calendar from '../../libs/util/calendar.js';
  69. /**
  70. * Calendar 日历
  71. * @description 此组件用于单个选择日期,范围选择日期等,日历被包裹在底部弹起的容器中.
  72. * @tutorial https://www.uviewui.com/components/calendar.html
  73. *
  74. * @property {String} title 标题内容 (默认 日期选择 )
  75. * @property {Boolean} showTitle 是否显示标题 (默认 true )
  76. * @property {Boolean} showSubtitle 是否显示副标题 (默认 true )
  77. * @property {String} mode 日期类型选择 single-选择单个日期,multiple-可以选择多个日期,range-选择日期范围 ( 默认 'single' )
  78. * @property {String} startText mode=range时,第一个日期底部的提示文字 (默认 '开始' )
  79. * @property {String} endText mode=range时,最后一个日期底部的提示文字 (默认 '结束' )
  80. * @property {Array} customList 自定义列表
  81. * @property {String} color 主题色,对底部按钮和选中日期有效 (默认 ‘#3c9cff' )
  82. * @property {String | Number} minDate 最小的可选日期 (默认 0 )
  83. * @property {String | Number} maxDate 最大可选日期 (默认 0 )
  84. * @property {Array | String| Date} defaultDate 默认选中的日期,mode为multiple或range是必须为数组格式
  85. * @property {String | Number} maxCount mode=multiple时,最多可选多少个日期 (默认 Number.MAX_SAFE_INTEGER )
  86. * @property {String | Number} rowHeight 日期行高 (默认 56 )
  87. * @property {Function} formatter 日期格式化函数
  88. * @property {Boolean} showLunar 是否显示农历 (默认 false )
  89. * @property {Boolean} showMark 是否显示月份背景色 (默认 true )
  90. * @property {String} confirmText 确定按钮的文字 (默认 '确定' )
  91. * @property {String} confirmDisabledText 确认按钮处于禁用状态时的文字 (默认 '确定' )
  92. * @property {Boolean} show 是否显示日历弹窗 (默认 false )
  93. * @property {Boolean} closeOnClickOverlay 是否允许点击遮罩关闭日历 (默认 false )
  94. * @property {Boolean} readonly 是否为只读状态,只读状态下禁止选择日期 (默认 false )
  95. * @property {String | Number} maxRange 日期区间最多可选天数,默认无限制,mode = range时有效
  96. * @property {String} rangePrompt 范围选择超过最多可选天数时的提示文案,mode = range时有效
  97. * @property {Boolean} showRangePrompt 范围选择超过最多可选天数时,是否展示提示文案,mode = range时有效 (默认 true )
  98. * @property {Boolean} allowSameDay 是否允许日期范围的起止时间为同一天,mode = range时有效 (默认 false )
  99. * @property {Number|String} round 圆角值,默认无圆角 (默认 0 )
  100. *
  101. * @event {Function()} confirm 点击确定按钮时触发 选择日期相关的返回参数
  102. * @event {Function()} close 日历关闭时触发 可定义页面关闭时的回调事件
  103. * @example <u-calendar :defaultDate="defaultDateMultiple" :show="show" mode="multiple" @confirm="confirm">
  104. </u-calendar>
  105. * */
  106. export default {
  107. name: 'u-calendar',
  108. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  109. components: {
  110. uHeader,
  111. uMonth
  112. },
  113. data() {
  114. return {
  115. // 需要显示的月份的数组
  116. months: [],
  117. // 在月份滚动区域中,当前视图中月份的index索引
  118. monthIndex: 0,
  119. // 月份滚动区域的高度
  120. listHeight: 0,
  121. // month组件中选择的日期数组
  122. selected: [],
  123. // 如果没有设置最大可选日期,默认为往后推3个月
  124. maxMonth: 3,
  125. scrollIntoView: '',
  126. // 过滤处理方法
  127. innerFormatter: value => value
  128. }
  129. },
  130. watch: {
  131. selectedChange: {
  132. immediate: true,
  133. handler(n) {
  134. this.setMonth()
  135. }
  136. },
  137. // 打开弹窗时,设置月份数据
  138. show: {
  139. immediate: true,
  140. handler(n) {
  141. this.setMonth()
  142. }
  143. },
  144. },
  145. computed: {
  146. // 多个条件的变化,会引起选中日期的变化,这里统一管理监听
  147. selectedChange() {
  148. return [this.minDate, this.maxDate, this.defaultDate]
  149. },
  150. subtitle() {
  151. // 初始化时,this.months为空数组,所以需要特别判断处理
  152. if (this.months.length) {
  153. return `${this.months[this.monthIndex].year}年${this.months[this.monthIndex].month}月`
  154. } else {
  155. return ''
  156. }
  157. },
  158. buttonDisabled() {
  159. // 如果为range类型,且选择的日期个数不足1个时,让底部的按钮出于disabled状态
  160. if (this.mode === 'range') {
  161. if (this.selected.length <= 1) {
  162. return true
  163. } else {
  164. return false
  165. }
  166. } else {
  167. return false
  168. }
  169. }
  170. },
  171. mounted() {
  172. this.start = Date.now()
  173. this.init()
  174. },
  175. methods: {
  176. // 在微信小程序中,不支持将函数当做props参数,故只能通过ref形式调用
  177. setFormatter(e) {
  178. this.innerFormatter = e
  179. },
  180. // month组件内部选择日期后,通过事件通知给父组件
  181. monthSelected(e) {
  182. this.selected = e
  183. if(!this.showConfirm) {
  184. // 在不需要确认按钮的情况下,如果为单选,或者范围多选且已选长度大于2,则直接进行返还
  185. if (this.mode === 'multiple' || this.mode === 'single' || this.mode === 'range' && this.selected.length >= 2) {
  186. this.$emit('confirm', this.selected)
  187. }
  188. }
  189. },
  190. init() {
  191. // 滚动区域的高度
  192. this.listHeight = this.rowHeight * 5 + 30
  193. this.setMonth()
  194. },
  195. close() {
  196. this.$emit('close')
  197. },
  198. // 点击确定按钮
  199. confirm() {
  200. if (!this.buttonDisabled) {
  201. this.$emit('confirm', this.selected)
  202. }
  203. },
  204. // 设置月份数据
  205. setMonth() {
  206. // 最小日期的毫秒数
  207. const minDate = this.minDate || dayjs().valueOf()
  208. // 如果没有指定最大日期,则往后推3个月
  209. const maxDate = this.maxDate || dayjs(minDate).add(this.maxMonth - 1, 'month').valueOf()
  210. // 最小与最大月份
  211. let minMonth = dayjs(minDate).month() + 1
  212. let maxMonth = dayjs(maxDate).month() + 1
  213. // 如果maxMonth小于minMonth,则意味着maxMonth为下一年的月份,需要加上12,为的是计算出两个月份之间间隔着多少月份
  214. maxMonth = minMonth > maxMonth ? maxMonth + 12 : maxMonth
  215. // 最大最小月份之间的共有多少个月份
  216. const months = Math.abs(minMonth - maxMonth)
  217. // 先清空数组
  218. this.months = []
  219. for (let i = 0; i <= months; i++) {
  220. this.months.push({
  221. date: new Array(dayjs(minDate).add(i, 'month').daysInMonth()).fill(1).map((item,
  222. index) => {
  223. // 日期,取值1-31
  224. let day = index + 1
  225. // 星期,0-6,0为周日
  226. const week = dayjs(minDate).add(i, "month").date(day).day()
  227. const date = dayjs(minDate).add(i, "month").date(day).format("YYYY-MM-DD")
  228. let bottomInfo = ''
  229. if (this.showLunar) {
  230. // 将日期转为农历格式
  231. const lunar = Calendar.solar2lunar(dayjs(date).year(), dayjs(date)
  232. .month() + 1, dayjs(date).date())
  233. bottomInfo = lunar.IDayCn
  234. }
  235. let config = {
  236. day,
  237. week,
  238. // 小于最小允许的日期,或者大于最大的日期,则设置为disabled状态
  239. disabled: dayjs(date).isBefore(dayjs(minDate).format("YYYY-MM-DD")) ||
  240. dayjs(date).isAfter(dayjs(maxDate).format("YYYY-MM-DD")),
  241. // 返回一个日期对象,供外部的formatter获取当前日期的年月日等信息,进行加工处理
  242. date: new Date(date),
  243. bottomInfo,
  244. dot: false,
  245. month: dayjs(minDate).add(i, "month").month() + 1
  246. }
  247. const formatter = this.formatter || this.innerFormatter
  248. return formatter(config)
  249. }),
  250. // 当前所属的月份
  251. month: dayjs(minDate).add(i, "month").month() + 1,
  252. // 当前年份
  253. year: dayjs(minDate).add(i, "month").year()
  254. });
  255. }
  256. },
  257. // scroll-view滚动监听
  258. onScroll(event) {
  259. // 不允许小于0的滚动值,如果scroll-view到顶了,继续下拉,会出现负数值
  260. const scrollTop = Math.max(0, event.detail.scrollTop)
  261. // 将当前滚动条数值,除以滚动区域的高度,可以得出当前滚动到了哪一个月份的索引
  262. for (let i = 0; i < this.months.length; i++) {
  263. if (scrollTop >= (this.months[i].top || this.listHeight)) {
  264. this.monthIndex = i
  265. }
  266. }
  267. },
  268. // 更新月份的top值
  269. updateMonthTop(topArr = []) {
  270. // 设置对应月份的top值,用于onScroll方法更新月份
  271. topArr.map((item, index) => {
  272. this.months[index].top = item
  273. })
  274. }
  275. },
  276. }
  277. </script>
  278. <style lang="scss" scoped>
  279. @import "../../libs/css/components.scss";
  280. .u-calendar {
  281. &__confirm {
  282. padding: 7px 18px;
  283. }
  284. }
  285. </style>