Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix disabledTime not working when there is no default value #680

Merged
merged 10 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 26 additions & 27 deletions src/panels/DatetimePanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ 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);
for (let i = 0; i <= maxValidTime; i++) {
if (!rangeSet.has(i)) {
return i;
if (rangeSet.has(refValue)) {
for (let i = 0; i <= maxValidTime; i++) {
if (!rangeSet.has(i) && i >= refValue) {
// first not disabled time
return i;
}
}
}

return 0
}
return refValue;
};

function DatetimePanel<DateType>(props: DatetimePanelProps<DateType>) {
const {
Expand Down Expand Up @@ -104,33 +106,30 @@ function DatetimePanel<DateType>(props: DatetimePanelProps<DateType>) {
const onInternalSelect = (date: DateType, source: 'date' | 'time') => {
let selectedDate = date;

if (source === 'date' && !value && timeProps.defaultValue) {
// Date with time defaultValue
selectedDate = generateConfig.setHour(
selectedDate,
generateConfig.getHour(timeProps.defaultValue),
if (source === 'date') {
const disabledTimes = disabledTime?.(value || timeProps.defaultValue) || {};
const validHour = findValidTime(
generateConfig.getHour(selectedDate),
disabledTimes.disabledHours?.() || [-1],
23,
);
selectedDate = generateConfig.setMinute(
selectedDate,
generateConfig.getMinute(timeProps.defaultValue),
const validMinute = findValidTime(
generateConfig.getMinute(selectedDate),
disabledTimes.disabledMinutes?.(validHour) || [-1],
59,
);
selectedDate = generateConfig.setSecond(
selectedDate,
generateConfig.getSecond(timeProps.defaultValue),
const validSeconds = findValidTime(
generateConfig.getSecond(selectedDate),
disabledTimes.disabledSeconds?.(validHour, validMinute) || [-1],
59,
);
selectedDate = generateConfig.setHour(selectedDate, validHour);
selectedDate = generateConfig.setMinute(selectedDate, validMinute);
selectedDate = generateConfig.setSecond(selectedDate, validSeconds);
} 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) {
const disabledTimes = disabledTime(value)

const validHour = findValidTime(disabledTimes.disabledHours?.() || [-1], 23)
const validMinute = findValidTime(disabledTimes.disabledMinutes?.(validHour) || [-1], 59)
const validSeconds = findValidTime(disabledTimes.disabledSeconds?.(validHour, validMinute) || [-1], 59)
selectedDate = generateConfig.setHour(selectedDate, validHour)
selectedDate = generateConfig.setMinute(selectedDate, validMinute)
selectedDate = generateConfig.setSecond(selectedDate, validSeconds)
}

if (onSelect) {
Expand Down
26 changes: 26 additions & 0 deletions tests/disabledTime.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fireEvent, render } from '@testing-library/react';
import type { Moment } from 'moment';
import moment from 'moment';
import { resetWarned } from 'rc-util/lib/warning';
import React from 'react';
import {
Expand Down Expand Up @@ -159,6 +160,31 @@ describe('Picker.DisabledTime', () => {
expect(document.querySelector('.rc-picker-input > input').getAttribute('value')).toEqual('1989-10-31 05:00:00');
});

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: () => [h],
disabledMinutes: () => [m],
disabledSeconds: () => [s],
}));

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(
expected.format('YYYY-MM-DD HH:mm:ss'),
);
});

describe('warning for legacy props', () => {
it('single', () => {
resetWarned();
Expand Down
Loading