Skip to content

Commit

Permalink
fix: solve the issue that input-number's value auto change after fast…
Browse files Browse the repository at this point in the history
… click step handle in safari (#585)

* fix: solve the issue that input-number's value auto change after fast click step handle in safari

* feat: add test case
  • Loading branch information
kiner-tang authored Jul 3, 2023
1 parent 8750366 commit 204f286
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/StepHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as React from 'react';
import classNames from 'classnames';
import useMobile from 'rc-util/lib/hooks/useMobile';
import raf from 'rc-util/lib/raf';

/**
* When click and hold on a button - the speed of auto changing the value.
Expand Down Expand Up @@ -32,13 +33,20 @@ export default function StepHandler({
}: StepHandlerProps) {
// ======================== Step ========================
const stepTimeoutRef = React.useRef<any>();
const frameIds = React.useRef<number[]>([]);

const onStepRef = React.useRef<StepHandlerProps['onStep']>();
onStepRef.current = onStep;

const onStopStep = () => {
clearTimeout(stepTimeoutRef.current);
};


// We will interval update step when hold mouse down
const onStepMouseDown = (e: React.MouseEvent, up: boolean) => {
e.preventDefault();
onStopStep();

onStepRef.current(up);

Expand All @@ -53,11 +61,10 @@ export default function StepHandler({
stepTimeoutRef.current = setTimeout(loopStep, STEP_DELAY);
};

const onStopStep = () => {
clearTimeout(stepTimeoutRef.current);
};

React.useEffect(() => onStopStep, []);
React.useEffect(() => () => {
onStopStep();
frameIds.current.forEach(id => raf.cancel(id));
}, []);

// ======================= Render =======================
const isMobile = useMobile();
Expand All @@ -74,11 +81,18 @@ export default function StepHandler({
[`${handlerClassName}-down-disabled`]: downDisabled,
});

// fix: https://github.com/ant-design/ant-design/issues/43088
// In Safari, When we fire onmousedown and onmouseup events in quick succession,
// there may be a problem that the onmouseup events are executed first,
// resulting in a disordered program execution.
// So, we need to use requestAnimationFrame to ensure that the onmouseup event is executed after the onmousedown event.
const safeOnStopStep = () => frameIds.current.push(raf(onStopStep));

const sharedHandlerProps = {
unselectable: 'on' as const,
role: 'button',
onMouseUp: onStopStep,
onMouseLeave: onStopStep,
onMouseUp: safeOnStopStep,
onMouseLeave: safeOnStopStep,
};

return (
Expand Down
32 changes: 32 additions & 0 deletions tests/longPress.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,36 @@ describe('InputNumber.LongPress', () => {
});
await waitFor(() => expect(onChange).toHaveBeenCalledWith(14));
});

it('Simulates event calls out of order in Safari', async () => {
const onChange = jest.fn();
const { container } = render(<InputNumber defaultValue={20} onChange={onChange} />);
fireEvent.mouseDown(container.querySelector('.rc-input-number-handler-up'));
act(() => {
jest.advanceTimersByTime(10);
});
fireEvent.mouseUp(container.querySelector('.rc-input-number-handler-up'));
act(() => {
jest.advanceTimersByTime(10);
});
fireEvent.mouseUp(container.querySelector('.rc-input-number-handler-up'));
act(() => {
jest.advanceTimersByTime(10);
});
fireEvent.mouseDown(container.querySelector('.rc-input-number-handler-up'));
act(() => {
jest.advanceTimersByTime(10);
});
fireEvent.mouseDown(container.querySelector('.rc-input-number-handler-up'));
act(() => {
jest.advanceTimersByTime(10);
});
fireEvent.mouseUp(container.querySelector('.rc-input-number-handler-up'));

act(() => {
jest.advanceTimersByTime(600 + 200 * 5 + 100);
});

await waitFor(() => expect(onChange).toBeCalledTimes(3));
});
});

1 comment on commit 204f286

@vercel
Copy link

@vercel vercel bot commented on 204f286 Jul 3, 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.