Skip to content

Commit 52ec790

Browse files
authored
Fix:Range onClick event not working (#340)
1 parent bdd943b commit 52ec790

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/RangePicker.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,11 @@ export type RangePickerSharedProps<DateType> = {
103103
onPanelChange?: (values: RangeValue<DateType>, modes: [PanelMode, PanelMode]) => void;
104104
onFocus?: React.FocusEventHandler<HTMLInputElement>;
105105
onBlur?: React.FocusEventHandler<HTMLInputElement>;
106+
onMouseDown?: React.MouseEventHandler<HTMLDivElement>;
107+
onMouseUp?: React.MouseEventHandler<HTMLDivElement>;
106108
onMouseEnter?: React.MouseEventHandler<HTMLDivElement>;
107109
onMouseLeave?: React.MouseEventHandler<HTMLDivElement>;
110+
onClick?: React.MouseEventHandler<HTMLDivElement>;
108111
onOk?: (dates: RangeValue<DateType>) => void;
109112
direction?: 'ltr' | 'rtl';
110113
autoComplete?: string;
@@ -210,8 +213,11 @@ function InnerRangePicker<DateType>(props: RangePickerProps<DateType>) {
210213
onCalendarChange,
211214
onFocus,
212215
onBlur,
216+
onMouseDown,
217+
onMouseUp,
213218
onMouseEnter,
214219
onMouseLeave,
220+
onClick,
215221
onOk,
216222
onKeyDown,
217223
components,
@@ -647,9 +653,12 @@ function InnerRangePicker<DateType>(props: RangePickerProps<DateType>) {
647653
});
648654

649655
// ========================== Click Picker ==========================
650-
const onPickerClick = (e: MouseEvent) => {
656+
const onPickerClick = (e: React.MouseEvent<HTMLDivElement>) => {
651657
// When click inside the picker & outside the picker's input elements
652658
// the panel should still be opened
659+
if (onClick) {
660+
onClick(e);
661+
}
653662
if (
654663
!mergedOpen &&
655664
!startInputRef.current.contains(e.target as Node) &&
@@ -663,8 +672,11 @@ function InnerRangePicker<DateType>(props: RangePickerProps<DateType>) {
663672
}
664673
};
665674

666-
const onPickerMouseDown = (e: MouseEvent) => {
675+
const onPickerMouseDown = (e: React.MouseEvent<HTMLDivElement>) => {
667676
// shouldn't affect input elements if picker is active
677+
if (onMouseDown) {
678+
onMouseDown(e);
679+
}
668680
if (
669681
mergedOpen &&
670682
(startFocused || endFocused) &&
@@ -1107,6 +1119,7 @@ function InnerRangePicker<DateType>(props: RangePickerProps<DateType>) {
11071119
onMouseEnter={onMouseEnter}
11081120
onMouseLeave={onMouseLeave}
11091121
onMouseDown={onPickerMouseDown}
1122+
onMouseUp={onMouseUp}
11101123
{...getDataOrAriaProps(props)}
11111124
>
11121125
<div

tests/range.spec.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,4 +1521,19 @@ describe('Picker.Range', () => {
15211521
wrapper.keyDown(KeyCode.ENTER);
15221522
expect(onCalendarChange).not.toHaveBeenCalled();
15231523
});
1524+
1525+
// https://github.com/ant-design/ant-design/issues/33662
1526+
it('range picker should have onClick event', () => {
1527+
const handleClick = jest.fn();
1528+
const wrapper = mount(<MomentRangePicker onClick={handleClick} />);
1529+
wrapper.simulate('click');
1530+
expect(handleClick).toHaveBeenCalled();
1531+
});
1532+
1533+
it('range picker should have onMouseDown event', () => {
1534+
const handleMouseDown = jest.fn();
1535+
const wrapper = mount(<MomentRangePicker onMouseDown={handleMouseDown} />);
1536+
wrapper.simulate('mousedown');
1537+
expect(handleMouseDown).toHaveBeenCalled();
1538+
});
15241539
});

0 commit comments

Comments
 (0)