Skip to content

Commit

Permalink
fix: the RangePicker sets disabledDate, the date cannot be modified (#…
Browse files Browse the repository at this point in the history
…888)

* fix: After the RangePicker  sets disabledDate, the date cannot be modified.

* Add test case
  • Loading branch information
Zyf665 authored Nov 4, 2024
1 parent 281bf37 commit 4724a58
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/PickerInput/hooks/useRangeValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,9 @@ export default function useRangeValue<ValueType extends DateType[], DateType ext
// >>> Invalid
const validateDates =
// Validate start
(!start || !isInvalidateDate(start, { activeIndex: 0 })) &&
(disabled[0] || !start || !isInvalidateDate(start, { activeIndex: 0 })) &&
// Validate end
(!end || !isInvalidateDate(end, { from: start, activeIndex: 1 }));

(disabled[1] || !end || !isInvalidateDate(end, { from: start, activeIndex: 1 }));
// >>> Result
const allPassed =
// Null value is from clear button
Expand Down
46 changes: 39 additions & 7 deletions tests/range.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,43 @@ describe('Picker.Range', () => {
expect(baseElement.querySelector('.rc-picker-dropdown-hidden')).toBeTruthy();
});

it('should not be checked if the startDate is disabled', () => {
const onChange = jest.fn();
const { container } = render(
<DayRangePicker
disabled={[true, false]}
defaultValue={[getDay('2024-10-28'), getDay('2024-11-20')]}
disabledDate={(date: Dayjs) => date <= dayjs('2024-11-20').endOf('day')}
onChange={onChange}
/>,
);

openPicker(container, 1);
selectCell('21', 1);
expect(onChange).toHaveBeenCalledWith(
[expect.anything(), expect.anything()],
['2024-10-28', '2024-11-21'],
);
});
it('should not be checked if the endDate is disabled', () => {
const onChange = jest.fn();
const { container } = render(
<DayRangePicker
disabled={[false, true]}
defaultValue={[getDay('2024-10-28'), getDay('2024-11-20')]}
disabledDate={(date: Dayjs) => date >= dayjs('2024-11-10').endOf('day')}
onChange={onChange}
/>,
);

openPicker(container, 0);
selectCell('21', 0);
expect(onChange).toHaveBeenCalledWith(
[expect.anything(), expect.anything()],
['2024-10-21', '2024-11-20'],
);
});

it('should close panel when finish first choose with showTime = true and disabled = [false, true]', () => {
const { baseElement } = render(<DayRangePicker showTime disabled={[false, true]} />);
expect(baseElement.querySelectorAll('.rc-picker-input')).toHaveLength(2);
Expand Down Expand Up @@ -541,7 +578,7 @@ describe('Picker.Range', () => {
it('pass tabIndex', () => {
const { container } = render(
<div>
<DayRangePicker tabIndex={-1}/>
<DayRangePicker tabIndex={-1} />
</div>,
);

Expand Down Expand Up @@ -705,12 +742,7 @@ describe('Picker.Range', () => {
});

it('prefix', () => {
render(
<DayRangePicker
prefix={<span className="prefix" />}
allowClear
/>,
);
render(<DayRangePicker prefix={<span className="prefix" />} allowClear />);
expect(document.querySelector('.prefix')).toBeInTheDocument();
});

Expand Down

0 comments on commit 4724a58

Please sign in to comment.