Skip to content

Commit

Permalink
fix: blur to trigger change (#838)
Browse files Browse the repository at this point in the history
* fix: not trigger change if click on the panel

* test: add test case
  • Loading branch information
zombieJ authored Jul 3, 2024
1 parent bf95656 commit 6485b33
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/PickerInput/Popup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export interface PopupProps<DateType extends object = any, PresetValue = DateTyp
needConfirm: boolean;
isInvalid: (date: DateType | DateType[]) => boolean;
onOk: VoidFunction;

onPanelMouseDown?: React.MouseEventHandler<HTMLDivElement>;
}

export default function Popup<DateType extends object = any>(props: PopupProps<DateType>) {
Expand All @@ -68,6 +70,7 @@ export default function Popup<DateType extends object = any>(props: PopupProps<D
// Focus
onFocus,
onBlur,
onPanelMouseDown,

// Direction
direction,
Expand Down Expand Up @@ -187,6 +190,7 @@ export default function Popup<DateType extends object = any>(props: PopupProps<D
// Container
let renderNode = (
<div
onMouseDown={onPanelMouseDown}
tabIndex={-1}
className={classNames(
containerPrefixCls,
Expand All @@ -213,6 +217,7 @@ export default function Popup<DateType extends object = any>(props: PopupProps<D
const offsetUnit = getoffsetUnit(realPlacement, rtl);
renderNode = (
<div
onMouseDown={onPanelMouseDown}
ref={wrapperRef}
className={classNames(`${prefixCls}-range-wrapper`, `${prefixCls}-${picker}-range-wrapper`)}
>
Expand Down
10 changes: 7 additions & 3 deletions src/PickerInput/RangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,13 @@ function RangePicker<DateType extends object = any>(
onSharedFocus(event);
};

// >>> Calendar
const onPanelSelect: PickerPanelProps<DateType>['onChange'] = (date: DateType) => {
// >>> MouseDown
const onPanelMouseDown: React.MouseEventHandler<HTMLDivElement> = () => {
lastOperation('panel');
};

// >>> Calendar
const onPanelSelect: PickerPanelProps<DateType>['onChange'] = (date: DateType) => {
const clone: RangeValueType<DateType> = fillIndex(calendarValue, activeIndex, date);

// Only trigger calendar event but not update internal `calendarValue` state
Expand Down Expand Up @@ -571,6 +574,7 @@ function RangePicker<DateType extends object = any>(
// Focus
onFocus={onPanelFocus}
onBlur={onSharedBlur}
onPanelMouseDown={onPanelMouseDown}
// Mode
picker={picker}
mode={mergedMode}
Expand Down Expand Up @@ -641,7 +645,7 @@ function RangePicker<DateType extends object = any>(

const onSelectorBlur: SelectorProps['onBlur'] = (event, index) => {
triggerOpen(false);
if (!needConfirm) {
if (!needConfirm && lastOperation() === 'input') {
const nextIndex = nextActiveIndex(calendarValue);
flushSubmit(activeIndex, nextIndex === null);
}
Expand Down
14 changes: 8 additions & 6 deletions tests/new-range.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -676,9 +676,9 @@ describe('NewPicker.Range', () => {

// Change start time (manually focus since fireEvent.focus not change activeElement)
openPicker(container);
fireEvent.click(
document.querySelector('.rc-picker-time-panel-column').querySelectorAll('li')[6],
);
const li6 = document.querySelector('.rc-picker-time-panel-column').querySelectorAll('li')[6];
fireEvent.mouseDown(li6);
fireEvent.click(li6);
document.querySelector<HTMLDivElement>('.rc-picker-panel-container').focus();

act(() => {
Expand All @@ -696,9 +696,11 @@ describe('NewPicker.Range', () => {
expect(isOpen()).toBeTruthy();

// Select end time
fireEvent.click(
document.querySelector('.rc-picker-time-panel-column').querySelectorAll('li')[11],
);
const li11 = document
.querySelector('.rc-picker-time-panel-column')
.querySelectorAll('li')[11];
fireEvent.mouseDown(li11);
fireEvent.click(li11);

act(() => {
jest.runAllTimers();
Expand Down

0 comments on commit 6485b33

Please sign in to comment.