From e9754416ab0ddcdebb3510ce82343de53f03631d Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Fri, 29 Sep 2023 17:06:57 +0800 Subject: [PATCH] fix --- src/panels/DatetimePanel/index.tsx | 31 +++++++++++++++++++++++------- tests/disabledTime.spec.tsx | 19 ++++++++++++------ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/panels/DatetimePanel/index.tsx b/src/panels/DatetimePanel/index.tsx index 117a6279d..b5a2d126f 100644 --- a/src/panels/DatetimePanel/index.tsx +++ b/src/panels/DatetimePanel/index.tsx @@ -18,10 +18,14 @@ export type DatetimePanelProps = { const ACTIVE_PANEL = tuple('date', 'time'); type ActivePanelType = (typeof ACTIVE_PANEL)[number]; -const findValidTime = (disabledRange: number[], maxValidTime: number) => { +const findValidTime = (refValue: number, disabledRange: number[], maxValidTime: number) => { const rangeSet = new Set(disabledRange); + if (!rangeSet.has(refValue)) { + return refValue; + } for (let i = 0; i <= maxValidTime; i++) { - if (!rangeSet.has(i)) { + if (!rangeSet.has(i) && i >= refValue) { + // first not disabled time return i; } } @@ -101,12 +105,24 @@ function DatetimePanel(props: DatetimePanelProps) { }; const disableTimeCheck = (date: DateType): DateType => { - let selectedDate = date + if (!disabledTime) { + return date; + } + let selectedDate = date; const disabledTimes = disabledTime(selectedDate); - const validHour = findValidTime(disabledTimes.disabledHours?.() || [-1], 23); - const validMinute = findValidTime(disabledTimes.disabledMinutes?.(validHour) || [-1], 59); + const validHour = findValidTime( + generateConfig.getHour(selectedDate), + disabledTimes.disabledHours?.() || [-1], + 23, + ); + const validMinute = findValidTime( + generateConfig.getMinute(selectedDate), + disabledTimes.disabledMinutes?.(validHour) || [-1], + 59, + ); const validSeconds = findValidTime( + generateConfig.getSecond(selectedDate), disabledTimes.disabledSeconds?.(validHour, validMinute) || [-1], 59, ); @@ -134,12 +150,13 @@ function DatetimePanel(props: DatetimePanelProps) { selectedDate, generateConfig.getSecond(timeProps.defaultValue), ); - selectedDate = disableTimeCheck(selectedDate); } else if (source === 'time' && !value && defaultValue) { selectedDate = generateConfig.setYear(selectedDate, generateConfig.getYear(defaultValue)); selectedDate = generateConfig.setMonth(selectedDate, generateConfig.getMonth(defaultValue)); selectedDate = generateConfig.setDate(selectedDate, generateConfig.getDate(defaultValue)); - } else if (source === 'date' && value && disabledTime) { + } + + if (showTime) { selectedDate = disableTimeCheck(selectedDate); } diff --git a/tests/disabledTime.spec.tsx b/tests/disabledTime.spec.tsx index b8b2e74f8..46a4a056f 100644 --- a/tests/disabledTime.spec.tsx +++ b/tests/disabledTime.spec.tsx @@ -161,20 +161,27 @@ describe('Picker.DisabledTime', () => { }); it('disabledTime should reset correctly when date changed by click for no default value', function () { + const now = moment(); + const h = now.hours(); + const m = now.minutes(); + const s = now.seconds(); + const disabledTime = jest.fn((_: Moment | null, __: 'start' | 'end') => ({ - disabledHours: () => [0, 1, 2, 3, 4], - disabledMinutes: () => [0, 1, 2, 3, 4], - disabledSeconds: () => [0, 1, 2, 3, 4], + disabledHours: () => [h], + disabledMinutes: () => [m], + disabledSeconds: () => [s], })); - const now = moment().hour(6).minute(6).second(6); - + const firstDayInMonth = now.startOf('month'); + const firstDayInCalendar = firstDayInMonth.clone().subtract(firstDayInMonth.days(), 'days'); + const expected = firstDayInCalendar.clone().hour(h + 1 % 24).minute(m + 1 % 60).second(s + 1 % 60); + render(); fireEvent.click(document.querySelectorAll('.rc-picker-cell-inner')[0]); expect(document.querySelector('.rc-picker-input > input').getAttribute('value')).toEqual( - now.format('YYYY-MM-DD HH:mm:ss'), + expected.format('YYYY-MM-DD HH:mm:ss'), ); });