u-calendar.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template>
  2. <uView
  3. v-if="insert"
  4. ref="calendarView"
  5. :title="title"
  6. :showTitle="showTitle"
  7. :showSubtitle="showSubtitle"
  8. :mode="mode"
  9. :startText="startText"
  10. :endText="endText"
  11. :customList="customList"
  12. :color="color"
  13. :minDate="minDate"
  14. :maxDate="maxDate"
  15. :defaultDate="defaultDate"
  16. :maxCount="maxCount"
  17. :rowHeight="rowHeight"
  18. :formatter="formatter"
  19. :showLunar="showLunar"
  20. :showMark="showMark"
  21. :readonly="readonly"
  22. :maxRange="maxRange"
  23. :rangePrompt="rangePrompt"
  24. :showRangePrompt="showRangePrompt"
  25. :allowSameDay="allowSameDay"
  26. :monthNum="monthNum"
  27. :shape="shape"
  28. :disabledDate="disabledDate"
  29. :disabledFun="disabledFun"
  30. :weekdays="weekdays"
  31. @monthSelected="monthSelected"
  32. />
  33. <u-popup
  34. v-else
  35. :show="show"
  36. mode="bottom"
  37. closeable
  38. @close="close"
  39. @open="open"
  40. :round="round"
  41. :closeOnClickOverlay="closeOnClickOverlay"
  42. >
  43. <uView
  44. ref="calendarView"
  45. :title="title"
  46. :showTitle="showTitle"
  47. :showSubtitle="showSubtitle"
  48. :mode="mode"
  49. :startText="startText"
  50. :endText="endText"
  51. :customList="customList"
  52. :color="color"
  53. :minDate="minDate"
  54. :maxDate="maxDate"
  55. :defaultDate="defaultDate"
  56. :maxCount="maxCount"
  57. :rowHeight="rowHeight"
  58. :formatter="formatter"
  59. :showLunar="showLunar"
  60. :showMark="showMark"
  61. :readonly="readonly"
  62. :maxRange="maxRange"
  63. :rangePrompt="rangePrompt"
  64. :showRangePrompt="showRangePrompt"
  65. :allowSameDay="allowSameDay"
  66. :monthNum="monthNum"
  67. :shape="shape"
  68. :disabledDate="disabledDate"
  69. :disabledFun="disabledFun"
  70. :weekdays="weekdays"
  71. @monthSelected="monthSelected"
  72. />
  73. <slot name="footer" v-if="showConfirm">
  74. <view class="u-calendar__confirm">
  75. <u-button
  76. shape="circle"
  77. :text="buttonDisabled ? confirmDisabledText : confirmText"
  78. :color="color"
  79. @click="confirm"
  80. :disabled="buttonDisabled"
  81. ></u-button>
  82. </view>
  83. </slot>
  84. </u-popup>
  85. </template>
  86. <script>
  87. import props from './props.js';
  88. import mixin from '../../libs/mixin/mixin';
  89. import mpMixin from '../../libs/mixin/mpMixin';
  90. import uView from './view.vue';
  91. /**
  92. * Calendar 日历
  93. * @description 此组件用于单个选择日期,范围选择日期等,日历被包裹在底部弹起的容器中.
  94. * @tutorial https://uview.d3u.cn/components/calendar.html
  95. *
  96. * @property {String} title 标题内容 (默认 日期选择 )
  97. * @property {Boolean} showTitle 是否显示标题 (默认 true )
  98. * @property {String} shape 选中的日期的形状,circle(圆形),square(带圆角) (默认 'square' )
  99. * @property {Boolean} showSubtitle 是否显示副标题 (默认 true )
  100. * @property {String} mode 日期类型选择 single-选择单个日期,multiple-可以选择多个日期,range-选择日期范围 ( 默认 'single' )
  101. * @property {String} startText mode=range时,第一个日期底部的提示文字 (默认 '开始' )
  102. * @property {String} endText mode=range时,最后一个日期底部的提示文字 (默认 '结束' )
  103. * @property {Array} customList 自定义列表
  104. * @property {String} color 主题色,对底部按钮和选中日期有效 (默认 ‘#3c9cff' )
  105. * @property {String | Number} minDate 最小的可选日期 (默认 0 )
  106. * @property {String | Number} maxDate 最大可选日期 (默认 0 )
  107. * @property {Array | String| Date} defaultDate 默认选中的日期,mode为multiple或range是必须为数组格式
  108. * @property {String | Number} maxCount mode=multiple时,最多可选多少个日期 (默认 Number.MAX_SAFE_INTEGER )
  109. * @property {String | Number} rowHeight 日期行高 (默认 56 )
  110. * @property {Function} formatter 日期格式化函数
  111. * @property {Boolean} showLunar 是否显示农历 (默认 false )
  112. * @property {Boolean} showMark 是否显示月份背景色 (默认 true )
  113. * @property {String} confirmText 确定按钮的文字 (默认 '确定' )
  114. * @property {String} confirmDisabledText 确认按钮处于禁用状态时的文字 (默认 '确定' )
  115. * @property {Boolean} show 是否显示日历弹窗 (默认 false )
  116. * @property {Boolean} closeOnClickOverlay 是否允许点击遮罩关闭日历 (默认 false )
  117. * @property {Boolean} readonly 是否为只读状态,只读状态下禁止选择日期 (默认 false )
  118. * @property {String | Number} maxRange 日期区间最多可选天数,默认无限制,mode = range时有效
  119. * @property {String} rangePrompt 范围选择超过最多可选天数时的提示文案,mode = range时有效
  120. * @property {Boolean} showRangePrompt 范围选择超过最多可选天数时,是否展示提示文案,mode = range时有效 (默认 true )
  121. * @property {Boolean} allowSameDay 是否允许日期范围的起止时间为同一天,mode = range时有效 (默认 false )
  122. * @property {Number|String} round 圆角值,默认无圆角 (默认 0 )
  123. * @property {Number|String} monthNum 最多展示的月份数量 (默认 3 )
  124. * @property {Boolean} insert 是否插入模式 (默认 false )
  125. * @property {Array | String| Date} disabledDate 禁止选择的日期
  126. * @property {Function} disabledFun 禁止选择的日期函数
  127. *
  128. * @event {Function()} confirm 点击确定按钮时触发 选择日期相关的返回参数
  129. * @event {Function()} close 日历关闭时触发 可定义页面关闭时的回调事件
  130. * @event {Function()} change 插入模式下,选择日期时触发
  131. * @example <u-calendar :defaultDate="defaultDateMultiple" :show="show" mode="multiple" @confirm="confirm">
  132. </u-calendar>
  133. * */
  134. export default {
  135. name: 'u-calendar',
  136. mixins: [mpMixin, mixin, props],
  137. components: {
  138. uView,
  139. },
  140. data() {
  141. return {
  142. // month组件中选择的日期数组
  143. selected: [],
  144. };
  145. },
  146. computed: {
  147. buttonDisabled() {
  148. // 如果为range类型,且选择的日期个数不足1个时,让底部的按钮出于disabled状态
  149. if (this.mode === 'range') {
  150. if (this.selected.length <= 1) {
  151. return true;
  152. } else {
  153. return false;
  154. }
  155. } else {
  156. return false;
  157. }
  158. },
  159. },
  160. // #ifdef VUE3
  161. emits: ['confirm', 'close', 'change'],
  162. // #endif
  163. methods: {
  164. close() {
  165. this.$emit('close');
  166. },
  167. open(){
  168. this.$nextTick(() => {
  169. this.$refs.calendarView.setMonth();
  170. });
  171. },
  172. // 点击确定按钮
  173. confirm() {
  174. if (!this.buttonDisabled) {
  175. this.$emit('confirm', this.selected);
  176. }
  177. },
  178. // month组件内部选择日期后,通过事件通知给父组件
  179. monthSelected(e) {
  180. this.selected = e;
  181. if (this.insert) {
  182. this.$emit('change', e);
  183. } else {
  184. if (!this.showConfirm) {
  185. // 在不需要确认按钮的情况下,如果为单选,或者范围多选且已选长度大于2,则直接进行返还
  186. if (
  187. this.mode === 'multiple' ||
  188. this.mode === 'single' ||
  189. (this.mode === 'range' && this.selected.length >= 2)
  190. ) {
  191. this.$emit('confirm', this.selected);
  192. }
  193. }
  194. }
  195. },
  196. },
  197. };
  198. </script>
  199. <style lang="scss" scoped>
  200. @import '../../libs/css/components.scss';
  201. .u-calendar {
  202. &__confirm {
  203. padding: 7px 18px;
  204. }
  205. }
  206. </style>