From 846dea5d7120cd86bc230c684079f5c0cee42377 Mon Sep 17 00:00:00 2001 From: YannLynn <101160769+YannLynn@users.noreply.github.com> Date: Fri, 15 Nov 2024 16:48:34 +0800 Subject: [PATCH] =?UTF-8?q?Fix=EF=BC=9ADatepicker=20disabledDate=20+=20mon?= =?UTF-8?q?thRange=20does=20not=20behave=20as=20expected=20(#2569)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Correct the current date to a valid date in autoSelectMonth * feat: add demo --- .../datePicker/yearAndMonthFoundation.ts | 38 ++++++++++++++----- .../datePicker/_story/datePicker.stories.jsx | 8 ++++ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/packages/semi-foundation/datePicker/yearAndMonthFoundation.ts b/packages/semi-foundation/datePicker/yearAndMonthFoundation.ts index f5fb449dc8..78694f029d 100644 --- a/packages/semi-foundation/datePicker/yearAndMonthFoundation.ts +++ b/packages/semi-foundation/datePicker/yearAndMonthFoundation.ts @@ -105,9 +105,9 @@ export default class YearAndMonthFoundation extends BaseFoundation month[right]) { - month[right] = item.month + 1; + month[right] = item.month ; } this._adapter.setCurrentMonth(month); @@ -121,8 +121,19 @@ export default class YearAndMonthFoundation extends BaseFoundation month === currentMonth[panelType]); let validMonth: typeof months[number]; @@ -131,15 +142,24 @@ export default class YearAndMonthFoundation extends BaseFoundation !disabledDate(setMonth(currentDate, month - 1))); } - if (validMonth) { - const month = copy(currentMonth); - month[panelType] = validMonth.month; - - // change year and month same time - this._adapter.setCurrentYearAndMonth(year, month); - this._adapter.notifySelectYearAndMonth(year, month); + if (validMonth && !isOppositeMonthDisabled) { + // only currentPanel Date is illegal + // just need to modify the month of the current panel + finalMonth[panelType] = validMonth.month; + } else if (validMonth && isOppositeMonthDisabled) { + // all panel Date is illegal + // change the value to the legal value calculated by the current panel + finalYear = { 'left': item.year, 'right': item.year }; + finalMonth = { 'left': validMonth.month, 'right': validMonth.month }; } + } else if (!isCurrentMonthDisabled && isOppositeMonthDisabled) { + // only opposite panel Date is illegal + // change the value to the legal value in the current panel + finalYear = { 'left': item.year, 'right': item.year }; + finalMonth = { 'left': currentMonth[panelType], 'right': currentMonth[panelType] }; } + this._adapter.setCurrentYearAndMonth(finalYear, finalMonth); + this._adapter.notifySelectYearAndMonth(finalYear, finalMonth); } backToMain() { diff --git a/packages/semi-ui/datePicker/_story/datePicker.stories.jsx b/packages/semi-ui/datePicker/_story/datePicker.stories.jsx index 11e1a7f3b1..97466f6cda 100644 --- a/packages/semi-ui/datePicker/_story/datePicker.stories.jsx +++ b/packages/semi-ui/datePicker/_story/datePicker.stories.jsx @@ -605,6 +605,12 @@ export const MonthRangePicker = () => { return date.getTime() < deadDate.getTime(); }; + const disabledRangeDate = date => { + const deadDate = new Date('2025/3/1 00:00:00'); + const startDate = new Date('2024/11/1 00:00:00'); + return date.getTime() < startDate.getTime() || date.getTime() > deadDate.getTime(); + }; + return ( <>
@@ -640,6 +646,8 @@ export const MonthRangePicker = () => {
年月禁用:禁用2023年3月前的所有年月
+
年月禁用:禁用2024年11月前 & 2025年2月后的所有年月
+

validateStatus