Skip to content

Commit

Permalink
fix: DatePicker 中disabledTime 禁用问题 (#666)
Browse files Browse the repository at this point in the history
* fix: fix-bug

* chore: remove whitespace

* feat: change logic

* feat: change logic

* feat: change logic

* test: add test case

* test: add test case

* test: add test case

* fix: fix incorrect time
  • Loading branch information
SnowingFox authored Sep 27, 2023
1 parent 8ebebae commit 6dcf4b9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ lib
es
yarn.lock
package-lock.json
pnpm-lock.yaml
coverage/
.doc
.history
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/time.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default () => {
picker="date"
showTime
disabledTime={() => ({
disabledHours: () => [1, 2, 3, 4, 5, 6],
disabledHours: () => [0, 1, 2, 3, 4, 5, 6, 20, 21],
})}
locale={zhCN}
generateConfig={momentGenerateConfig}
Expand Down
20 changes: 20 additions & 0 deletions src/panels/DatetimePanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ export type DatetimePanelProps<DateType> = {
const ACTIVE_PANEL = tuple('date', 'time');
type ActivePanelType = (typeof ACTIVE_PANEL)[number];

const findValidTime = (disabledRange: number[], maxValidTime: number) => {
const rangeSet = new Set(disabledRange);
for (let i = 0; i <= maxValidTime; i++) {
if (!rangeSet.has(i)) {
return i;
}
}

return 0
}

function DatetimePanel<DateType>(props: DatetimePanelProps<DateType>) {
const {
prefixCls,
Expand Down Expand Up @@ -111,6 +122,15 @@ function DatetimePanel<DateType>(props: DatetimePanelProps<DateType>) {
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
21 changes: 21 additions & 0 deletions tests/disabledTime.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,27 @@ describe('Picker.DisabledTime', () => {
jest.useRealTimers();
});

it('disabledTime should reset correctly when date changed by click', function () {
const disabledTime = jest.fn((_: Moment | null, __: 'start' | 'end') => ({
disabledHours: () => [0, 1, 2, 3, 4, 10],
}));

render(
<MomentRangePicker
open
showTime
disabledTime={disabledTime}
defaultValue={[getMoment('1989-11-28'), getMoment('1990-09-03')]}
/>,
);

expect(document.querySelector('.rc-picker-input > input').getAttribute('value')).toEqual('1989-11-28 00:00:00');

fireEvent.click(document.querySelectorAll('.rc-picker-cell-inner')[2]);

expect(document.querySelector('.rc-picker-input > input').getAttribute('value')).toEqual('1989-10-31 05:00:00');
});

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

1 comment on commit 6dcf4b9

@vercel
Copy link

@vercel vercel bot commented on 6dcf4b9 Sep 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.