fix(laydate): 修复日期范围联动模式中,点击任意面板的边缘日期时,另一侧面板未能正确切换的问题 (#2005)

* fix(laydate)

* Update laydate.js

* refactor(laydate): 改进范围联动模式面板切换,并修复一些边缘情况

* chore(laydate): 优化日期范围联动选择模式中的边缘日期的处理

---------

Co-authored-by: sight <26325820+Sight-wcg@users.noreply.github.com>
Co-authored-by: 贤心 <3277200+sentsim@users.noreply.github.com>
pull/2018/head
地亚尔-IT 2024-06-17 18:38:53 +08:00 committed by GitHub
parent 2e5ad41aef
commit e87cae2172
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 72 additions and 30 deletions

View File

@ -1496,6 +1496,8 @@
//同步头部年月
lay(elemYM[0]).attr('lay-ym', dateTime.year + '-' + (dateTime.month + 1));
lay(elemYM[1]).attr('lay-ym', dateTime.year + '-' + (dateTime.month + 1));
if(!that.panelYM) that.panelYM = {};
that.panelYM[index] = {year: dateTime.year, month: dateTime.month};
if(options.lang === 'cn'){
lay(elemYM[0]).attr('lay-type', 'year').html(dateTime.year + ' 年')
@ -2155,6 +2157,45 @@
return that;
};
/**
* 判断日期是否存在面板用于处理日期范围选择的一些边缘情况
* @param {object} datetime 日期时间对象
* @param {number} index - 面板索引
* @returns
*/
Class.prototype.checkPanelDate = function(datetime, index){
var that = this;
var options = that.config;
// 年月范围选择不需要处理
if(options.type !== 'date' && options.type !== 'datetime') return;
var startPanel = index === 0;
var month = datetime.month + 1; // 点击的日期所在月份
var panelMonth = that.panelYM[index].month + 1; // 当前面板头部月份
// 边缘日期的处理
var firstRenderIndex = that.endState
// 二次点击(一般为结束日期)任意一侧面板时:
// 1. 左侧面板中,点击的月份属于下一个月时,应渲染右侧面板而不是左侧面板;
// 2. 左侧面板中,点击的月份属于上一个月时,应将两个面板都重新渲染(等效点击 prevMonth);
// 3. 右侧面板同理。
? function() {
return (startPanel && month > panelMonth) || (!startPanel && month < panelMonth)
? 1 - index
: index;
}()
// 初次点击(一般为开始日期)任意一侧面板时:
// 1. 让该面板自行切换,以保持日期的「选中状态」在该侧;
// 2. 另一侧面板则根据点击的面板进行响应式切换,以保持左右面板始终为连续月份。
: index;
// 为简化实现,只要点击的月份不等于当前面板顶部显示的月份时,就重新渲染两侧面板
return {
needFullRender: month !== panelMonth,
index: firstRenderIndex
}
}
//选择日期
Class.prototype.choose = function(td, index){
if(td.hasClass(DISABLED)) return;
@ -2246,16 +2287,12 @@
isChange && (options.dateTime = lay.extend({}, that.startDate));
}
if (that.rangeLinked) {
// 处理日期范围选择的一些边缘情况
var checkState = that.checkPanelDate(dateTime, panelIndex);
var dateTimeTemp = lay.extend({}, dateTime);
if (panelIndex && !index && !isChange) { // 处理可能出现的联动面板中点击右面板但是判定为开始日期这个时候点击头部的切换上下月第一次没有反应的问题
// 选择了右面板但是判断之后作为开始时间
var YM = that.getAsYM(dateTime.year, dateTime.month, 'sub');
lay.extend(options.dateTime, {
year: YM[0]
,month: YM[1]
});
}
that.calendar(dateTimeTemp, panelIndex, isChange ? 'init' : null);
var renderMode = (isChange || (checkState && checkState.needFullRender)) ? 'init' : null;
var panelIdx = checkState ? checkState.index : panelIndex;
that.calendar(dateTimeTemp, panelIdx, renderMode);
} else {
that.calendar(null, index, isChange ? 'init' : null);
}
@ -2407,32 +2444,37 @@
}
}
,prevMonth: function(){
if (that.rangeLinked) {
dateTime = options.dateTime;
}
var YM = that.getAsYM(dateTime.year, dateTime.month, 'sub');
lay.extend(dateTime, {
year: YM[0]
,month: YM[1]
});
// rangeLinked 模式非实时选择日期,不需要同步 options.dateTime应根据面板显示日期切换
if(that.rangeLinked){
var panelYM = that.panelYM[0];
var YM = that.getAsYM(panelYM.year, panelYM.month, 'sub');
var dateTimeTemp = lay.extend({}, options.dateTime, that.panelYM[0], {year: YM[0], month: YM[1]});
that.checkDate('limit').calendar(dateTimeTemp, null, 'init');
}else{
var YM = that.getAsYM(dateTime.year, dateTime.month, 'sub');
lay.extend(dateTime, {
year: YM[0]
,month: YM[1]
});
that.checkDate('limit').calendar(null, null, 'init');
if (!that.rangeLinked) {
that.checkDate('limit').calendar(null, null, 'init');
that.autoCalendarModel.auto ? that.choose(lay(elemCont).find('td.layui-this'), index) : that.done(null, 'change');
}
}
,nextMonth: function(){
if (that.rangeLinked) {
dateTime = options.dateTime;
}
var YM = that.getAsYM(dateTime.year, dateTime.month);
lay.extend(dateTime, {
year: YM[0]
,month: YM[1]
});
that.checkDate('limit').calendar(null, null, 'init');
if (!that.rangeLinked) {
if(that.rangeLinked){
var panelYM = that.panelYM[0];
var YM = that.getAsYM(panelYM.year, panelYM.month);
var dateTimeTemp = lay.extend({}, options.dateTime, that.panelYM[0], {year: YM[0],month: YM[1]});
that.checkDate('limit').calendar(dateTimeTemp, null, 'init');
}else{
var YM = that.getAsYM(dateTime.year, dateTime.month);
lay.extend(dateTime, {
year: YM[0]
,month: YM[1]
});
that.checkDate('limit').calendar(null, null, 'init');
that.autoCalendarModel.auto ? that.choose(lay(elemCont).find('td.layui-this'), index) : that.done(null, 'change');
}
}