Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo837 committed Sep 29, 2023
1 parent 968065b commit e975441
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
31 changes: 24 additions & 7 deletions src/panels/DatetimePanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ export type DatetimePanelProps<DateType> = {
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;
}
}
Expand Down Expand Up @@ -101,12 +105,24 @@ function DatetimePanel<DateType>(props: DatetimePanelProps<DateType>) {
};

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,
);
Expand Down Expand Up @@ -134,12 +150,13 @@ function DatetimePanel<DateType>(props: DatetimePanelProps<DateType>) {
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);
}

Expand Down
19 changes: 13 additions & 6 deletions tests/disabledTime.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<MomentRangePicker open showTime disabledTime={disabledTime} />);

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'),
);
});

Expand Down

0 comments on commit e975441

Please sign in to comment.