Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NumberField] Correctly handle quick touches #1294

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,32 @@ describe('<NumberField.Decrement />', () => {

expect(input).to.have.value('99');
});

it('always decrements on quick touch (touchend that occurs before TOUCH_TIMEOUT)', async () => {
await render(
<NumberField.Root defaultValue={0}>
<NumberField.Decrement />
<NumberField.Input />
</NumberField.Root>,
);

const button = screen.getByRole('button');
const input = screen.getByRole('textbox');

fireEvent.touchStart(button);
fireEvent.mouseEnter(button);
fireEvent.pointerDown(button, { pointerType: 'touch' });
fireEvent.touchEnd(button);
fireEvent.click(button, { detail: 1 });

expect(input).to.have.value('-1');

fireEvent.touchStart(button);
// No mouseenter occurs after the first focus
fireEvent.pointerDown(button, { pointerType: 'touch' });
fireEvent.touchEnd(button);
fireEvent.click(button, { detail: 1 });

expect(input).to.have.value('-2');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,32 @@ describe('<NumberField.Increment />', () => {

expect(input).to.have.value('101');
});

it('always increments on quick touch (touchend that occurs before TOUCH_TIMEOUT)', async () => {
await render(
<NumberField.Root defaultValue={0}>
<NumberField.Increment />
<NumberField.Input />
</NumberField.Root>,
);

const button = screen.getByRole('button');
const input = screen.getByRole('textbox');

fireEvent.touchStart(button);
fireEvent.mouseEnter(button);
fireEvent.pointerDown(button, { pointerType: 'touch' });
fireEvent.click(button, { detail: 1 });
fireEvent.touchEnd(button);

expect(input).to.have.value('1');

fireEvent.touchStart(button);
// No mouseenter occurs after the first focus
fireEvent.pointerDown(button, { pointerType: 'touch' });
fireEvent.click(button, { detail: 1 });
fireEvent.touchEnd(button);

expect(input).to.have.value('2');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ export function useNumberFieldRoot(
const unsubscribeFromGlobalContextMenuRef = React.useRef<() => void>(() => {});
const isTouchingButtonRef = React.useRef(false);
const hasTouchedInputRef = React.useRef(false);
const ignoreClickRef = React.useRef(false);
const pointerTypeRef = React.useRef<'mouse' | 'touch' | 'pen' | ''>('');

useEnhancedEffect(() => {
if (validityData.initialValue === null && value !== validityData.initialValue) {
Expand Down Expand Up @@ -431,7 +433,7 @@ export function useNumberFieldRoot(
event.defaultPrevented ||
isDisabled ||
// If it's not a keyboard/virtual click, ignore.
event.detail !== 0
(pointerTypeRef.current === 'touch' ? ignoreClickRef.current : event.detail !== 0)
) {
return;
}
Expand All @@ -449,6 +451,8 @@ export function useNumberFieldRoot(
return;
}

pointerTypeRef.current = event.pointerType;
ignoreClickRef.current = false;
isPressedRef.current = true;
incrementDownCoordsRef.current = { x: event.clientX, y: event.clientY };

Expand All @@ -466,6 +470,7 @@ export function useNumberFieldRoot(
const moves = movesAfterTouchRef.current;
movesAfterTouchRef.current = 0;
if (moves < MAX_POINTER_MOVES_AFTER_TOUCH) {
ignoreClickRef.current = true;
startAutoChange(isIncrement);
} else {
stopAutoChange();
Expand Down
Loading