u-date-strip.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <template>
  2. <view class="u-date-strip" :style="[containerStyle]">
  3. <!-- 周模式 - swiper切换 -->
  4. <template v-if="mode === 'week'">
  5. <swiper
  6. class="u-date-strip__swiper"
  7. :current="currentIndex"
  8. @change="onSwiperChange"
  9. >
  10. <swiper-item v-for="(week, index) in weekList" :key="index">
  11. <view class="u-date-strip__wrapper">
  12. <view
  13. v-for="(item, idx) in week"
  14. :key="idx"
  15. class="u-date-strip__item"
  16. >
  17. <view class="u-date-strip__item-content" :style="[itemStyle('week',item)]" @click="selectDate(item)">
  18. <view class="u-date-strip__weekday">
  19. {{ item.weekday }}
  20. </view>
  21. <view class="u-date-strip__date" :style="[itemStyle('date',item)]">
  22. <text class="u-date-strip__date-text">{{ item.date }}</text>
  23. <text v-if="showLunar && item.lunar" class="u-date-strip__lunar" :style="[itemStyle('lunar',item)]">{{ item.lunar }}</text>
  24. </view>
  25. </view>
  26. </view>
  27. </view>
  28. </swiper-item>
  29. </swiper>
  30. </template>
  31. <!-- 平铺模式 - scroll-view -->
  32. <template v-else>
  33. <scroll-view
  34. class="u-date-strip__scroll"
  35. scroll-x
  36. :scroll-into-view="scrollIntoView"
  37. >
  38. <view class="u-date-strip__scroll-wrapper">
  39. <view
  40. v-for="(item, index) in allDays"
  41. :key="index"
  42. :id="`item-${index}`"
  43. class="u-date-strip__item"
  44. >
  45. <view class="u-date-strip__item-content" :style="[itemStyle('week',item)]" @click="selectDate(item)">
  46. <text class="u-date-strip__weekday" :style="[itemStyle('weekday',item)]">
  47. {{ item.weekday }}
  48. </text>
  49. <view class="u-date-strip__date" :style="[itemStyle('date',item)]">
  50. <text class="u-date-strip__date-text">{{ item.date }}</text>
  51. <text v-if="item.bottomInfo" class="u-date-strip__lunar" :style="[itemStyle('lunar',item)]">{{ item.bottomInfo }}</text>
  52. </view>
  53. </view>
  54. </view>
  55. </view>
  56. </scroll-view>
  57. </template>
  58. </view>
  59. </template>
  60. <script>
  61. import props from './props.js';
  62. import mixin from '../../libs/mixin/mixin'
  63. import mpMixin from '../../libs/mixin/mpMixin';
  64. import dayjs from '../../libs/util/dayjs.js';
  65. import Calendar from '../../libs/util/calendar.js';
  66. /**
  67. * dateStrip 日期横条
  68. * @description 用于展示周日历或一组日历信息
  69. * @tutorial https://uviewui.com/components/dateStrip.html
  70. *
  71. * @property {Number} v-model/modelValue 选中的日期时间戳
  72. * @property {Number} defaultDate 默认选中的日期时间戳
  73. * @property {String} mode 切换模式 week|none (默认 'week' )
  74. * @property {String} activeMode 高亮模式 both|date|text (默认 'both' )
  75. * @property {Number} minDate 可选择的最小日期时间戳
  76. * @property {Number} maxDate 可选择的最大日期时间戳
  77. * @property {String} height 组件高度 (默认 '86px' )
  78. * @property {String} itemWidth 每格日期宽度 (默认 '50px' )
  79. * @property {String} itemRound 每格日期圆角 (默认 '6px' )
  80. * @property {String} activeBgColor 选中框背景色
  81. * @property {String} activeColor 选中框文本色
  82. * @property {String} bgColor 横条背景色
  83. * @property {String} round 选中框圆角
  84. * @property {Number} firstDayOfWeek 第一天从星期几开始 0-6 (默认 0 )
  85. * @property {Number} monthNum 最多展示月份数量 (默认 3 )
  86. * @property {Array|String} disabledDate 禁止选择的日期
  87. * @property {Function} disabledFun 禁止选择的日期函数
  88. * @property {String} disabledColor 禁用日期的文字颜色
  89. * @property {Boolean} showLunar 是否显示农历
  90. * @property {Boolean} padZero 是否对小于10的数字补0
  91. * @property {Function} formatter 日期格式化函数
  92. * @property {Object} customStyle 定义需要用到的外部样式
  93. * @event {Function} change 点击日期时触发
  94. * @example <u-date-strip v-model="date" @change="onChange"></u-date-strip>
  95. */
  96. export default {
  97. name: 'u-date-strip',
  98. mixins: [mpMixin, mixin, props],
  99. data() {
  100. return {
  101. currentIndex: 0,
  102. weekList: [],
  103. allDays: [],
  104. scrollIntoView: '',
  105. weekdays: ['日', '一', '二', '三', '四', '五', '六'],
  106. innerSelected: null, // 内部选中的时间戳
  107. innerFormatter: (value) => value
  108. };
  109. },
  110. computed: {
  111. // 当前选中的值
  112. currentValue() {
  113. // #ifdef VUE2
  114. return this.value || this.defaultDate;
  115. // #endif
  116. // #ifdef VUE3
  117. return this.modelValue || this.defaultDate;
  118. // #endif
  119. },
  120. // 容器样式
  121. containerStyle() {
  122. const style = {};
  123. if (this.bgColor) {
  124. style.backgroundColor = this.bgColor;
  125. }
  126. if (this.height) {
  127. style.height = this.height;
  128. }
  129. if(this.round){
  130. style.borderRadius = this.round;
  131. }
  132. return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle));
  133. },
  134. // 获取日期数字样式的计算属性
  135. itemStyle() {
  136. return (type, item) => {
  137. let style = {};
  138. if(type == 'week'){
  139. style.width = this.itemWidth;
  140. }
  141. // 禁用日期的样式
  142. if (item.disabled) {
  143. style.color = this.disabledColor;
  144. style.pointerEvents = 'none';
  145. }
  146. if (this.isSameDay(dayjs(this.innerSelected), dayjs(item.timestamp))) {
  147. style.borderRadius = this.itemRound;
  148. if(type == 'week'){
  149. // 整个容器的高亮样式
  150. if (this.activeMode == 'both') {
  151. style.backgroundColor = this.activeBgColor;
  152. style.color = this.activeColor;
  153. style = uni.$u.deepMerge(style, uni.$u.addStyle(this.activeStyle))
  154. }else if(this.activeMode == 'text'){
  155. style.color = this.activeBgColor;
  156. }
  157. }
  158. if(type == 'date'){
  159. if (this.activeMode == 'date') {
  160. //只高亮日期部分
  161. style.backgroundColor = this.activeBgColor;
  162. style.color = this.activeColor;
  163. style = uni.$u.deepMerge(style, uni.$u.addStyle(this.activeStyle))
  164. } else if (this.activeMode == 'both') {
  165. // 只高亮文字颜色
  166. style.color = this.activeColor;
  167. }else if(this.activeMode == 'text'){
  168. style.color = this.activeBgColor;
  169. }
  170. }
  171. if(type == 'lunar'){
  172. style.color = this.activeColor;
  173. }
  174. }
  175. return style;
  176. };
  177. },
  178. },
  179. watch: {
  180. currentValue: {
  181. immediate: true,
  182. handler(newVal) {
  183. this.innerSelected = newVal;
  184. },
  185. }
  186. },
  187. mounted() {
  188. this.$nextTick(() => {
  189. this.init();
  190. });
  191. },
  192. methods: {
  193. // 初始化
  194. init() {
  195. if (this.mode === 'week') {
  196. this.initWeekMode();
  197. } else {
  198. this.initScrollMode();
  199. }
  200. },
  201. // 获取日期范围
  202. getDateRange() {
  203. const monthNum = parseInt(this.monthNum);
  204. const minDate = this.minDate ? dayjs(this.minDate) : dayjs().subtract(monthNum, 'month');
  205. const maxDate = this.maxDate ? dayjs(this.maxDate) : dayjs().add(monthNum, 'month');
  206. return { minDate, maxDate };
  207. },
  208. // 初始化周模式
  209. initWeekMode() {
  210. this.weekList = [];
  211. const current = dayjs(this.currentValue);
  212. const currentWeekStart = this.getWeekStart(current);
  213. const { minDate, maxDate } = this.getDateRange();
  214. // 计算需要生成的周数
  215. const startWeek = this.getWeekStart(minDate);
  216. const endWeek = this.getWeekStart(maxDate);
  217. const totalWeeks = endWeek.diff(startWeek, 'week') + 1;
  218. // 生成指定范围内的周数据
  219. for (let i = 0; i < totalWeeks; i++) {
  220. const weekStart = startWeek.add(i, 'week');
  221. const week = this.generateWeek(weekStart);
  222. this.weekList.push(week);
  223. }
  224. // 设置当前周的索引
  225. const currentWeekIndex = currentWeekStart.diff(startWeek, 'week');
  226. this.currentIndex = Math.max(0, currentWeekIndex + 1);
  227. },
  228. // 初始化滚动模式
  229. initScrollMode() {
  230. this.allDays = [];
  231. const { minDate, maxDate } = this.getDateRange();
  232. let currentDate = minDate;
  233. let index = 0;
  234. let selectedIndex = -1;
  235. while (currentDate.isBefore(maxDate) || currentDate.isSame(maxDate, 'day')) {
  236. const day = this.generateDay(currentDate);
  237. this.allDays.push(day);
  238. // 记录选中日期的索引
  239. if (this.isSameDay(currentDate, dayjs(this.currentValue))) {
  240. selectedIndex = index;
  241. }
  242. currentDate = currentDate.add(1, 'day');
  243. index++;
  244. }
  245. // 滚动到选中的日期
  246. if (selectedIndex >= 0) {
  247. this.$nextTick(() => {
  248. this.scrollIntoView = `item-${selectedIndex}`;
  249. });
  250. }
  251. },
  252. // 获取周的开始日期
  253. getWeekStart(date) {
  254. const day = date.day();
  255. const diff = (day - this.firstDayOfWeek + 7) % 7;
  256. return date.subtract(diff, 'day');
  257. },
  258. // 生成一周的数据
  259. generateWeek(weekStart) {
  260. const week = [];
  261. for (let i = 0; i < 7; i++) {
  262. const date = weekStart.add(i, 'day');
  263. week.push(this.generateDay(date));
  264. }
  265. return week;
  266. },
  267. // 生成单天数据
  268. generateDay(date) {
  269. const day = {
  270. date: this.padZero && date.date() < 10 ? `0${date.date()}` : date.date(),
  271. weekday: this.weekdays[date.day()],
  272. timestamp: date.valueOf(),
  273. disabled: false,
  274. bottomInfo: '',
  275. };
  276. // 计算农历
  277. if (this.showLunar) {
  278. const lunar = Calendar.solar2lunar(
  279. date.year(),
  280. date.month() + 1,
  281. date.date(),
  282. );
  283. day.bottomInfo = lunar.IDayCn;
  284. }
  285. // 检查是否在可选范围内
  286. if (this.minDate && date.isBefore(this.minDate)) {
  287. day.disabled = true;
  288. }
  289. if (this.maxDate && date.isAfter(this.maxDate)) {
  290. day.disabled = true;
  291. }
  292. // 检查是否在禁用日期列表中
  293. if (this.disabledDate) {
  294. if (uni.$u.test.string(this.disabledDate)) {
  295. this.disabledDate = [
  296. this.disabledDate,
  297. ];
  298. }
  299. this.disabledDate.forEach((item) => {
  300. if (this.isSameDay(dayjs(item), date)) {
  301. day.disabled = true;
  302. }
  303. });
  304. }
  305. if (this.disabledFun && uni.$u.test.func(this.disabledFun)) {
  306. let result = this.disabledFun(day);
  307. if (uni.$u.test.array(result)) {
  308. day.disabled = result[0];
  309. day.bottomInfo = result[1];
  310. } else {
  311. day.disabled = result;
  312. }
  313. }
  314. // 应用格式化函数
  315. const formatter = this.formatter || this.innerFormatter;
  316. return formatter(day);
  317. },
  318. // 判断是否是同一天
  319. isSameDay(date1, date2) {
  320. return date1.isSame(date2, 'day');
  321. },
  322. // 在微信小程序中,不支持将函数当做props参数,故只能通过ref形式调用
  323. setFormatter(e) {
  324. this.innerFormatter = e
  325. },
  326. // 选择日期
  327. selectDate(item) {
  328. if (item.disabled) return;
  329. this.innerSelected = item.timestamp;
  330. // #ifdef VUE2
  331. this.$emit('input', item.timestamp);
  332. // #endif
  333. // #ifdef VUE3
  334. this.$emit('update:modelValue', item.timestamp);
  335. // #endif
  336. this.$emit('change', {
  337. weekday: item.weekday,
  338. date: item.date,
  339. timestamp: item.timestamp,
  340. lunar: item.lunar
  341. });
  342. },
  343. onSwiperChange(e) {
  344. this.currentIndex = e.detail.current;
  345. },
  346. },
  347. };
  348. </script>
  349. <style lang="scss" scoped>
  350. @import '../../libs/css/components.scss';
  351. .u-date-strip {
  352. padding: 8px 0;
  353. &__swiper {
  354. width: 100%;
  355. height: 100%;
  356. }
  357. &__scroll {
  358. width: 100%;
  359. height: 100%;
  360. white-space: nowrap;
  361. &-wrapper {
  362. @include flex(row);
  363. align-items: center;
  364. height: 100%;
  365. }
  366. }
  367. &__wrapper {
  368. @include flex(row);
  369. align-items: center;
  370. box-sizing: border-box;
  371. height: 100%;
  372. }
  373. &__item {
  374. flex: 1;
  375. height: 100%;
  376. @include flex(column);
  377. align-items: center;
  378. justify-content: center;
  379. &-content{
  380. @include flex(column);
  381. align-items: center;
  382. flex-shrink: 0;
  383. box-sizing: border-box;
  384. padding: 5px 0;
  385. height: 100%;
  386. }
  387. }
  388. &__weekday {
  389. font-size: 14px;
  390. padding-bottom: 5px;
  391. }
  392. &__date {
  393. flex: 1;
  394. @include flex(column);
  395. align-items: center;
  396. justify-content: center;
  397. flex-shrink: 0;
  398. width: 100%;
  399. &-text{
  400. font-size: 16px;
  401. font-weight: bold;
  402. text-align: center;
  403. }
  404. }
  405. &__lunar {
  406. font-size: 12px;
  407. color: #909399;
  408. text-align: center;
  409. margin-bottom: 5px;
  410. }
  411. }
  412. </style>