Repository navigation
fix: toggle hover popups on a tap, not on the touch pointer's events #653
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
Open
esetnik
wants to merge
1
commit into
react-component:master
Choose a base branch
from
esetnik:fix/touch-tap-toggle
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import { act, fireEvent, render } from '@testing-library/react'; | ||
| import React from 'react'; | ||
| import Trigger from '../src'; | ||
| import { placementAlignMap } from './util'; | ||
|
|
||
| /** | ||
| * Dispatches a pointer event with a `pointerType`. jsdom has no | ||
| * `PointerEvent`, so it is a mouse event carrying the property. React builds | ||
| * `onPointerEnter` and `onPointerLeave` from `pointerover` and `pointerout`. | ||
| */ | ||
| function firePointer(target: Element, type: string, pointerType: string) { | ||
| const event = new MouseEvent(type, { bubbles: true, cancelable: true }); | ||
| Object.defineProperty(event, 'pointerType', { value: pointerType }); | ||
| fireEvent(target, event); | ||
| } | ||
|
|
||
| /** | ||
| * The events a browser sends for a tap, in the order Chrome sends them: the | ||
| * touch pointer enters, presses and leaves, then the compatibility mouse | ||
| * events follow and the tap clicks. | ||
| */ | ||
| function tap(target: Element, pointerType = 'touch') { | ||
| firePointer(target, 'pointerover', pointerType); | ||
| firePointer(target, 'pointerdown', pointerType); | ||
| fireEvent.touchStart(target); | ||
| firePointer(target, 'pointerup', pointerType); | ||
| firePointer(target, 'pointerout', pointerType); | ||
| fireEvent.touchEnd(target); | ||
| fireEvent.mouseEnter(target); | ||
| fireEvent.mouseDown(target); | ||
| fireEvent.mouseUp(target); | ||
| fireEvent.click(target); | ||
| } | ||
|
|
||
| describe('Trigger.Touch', () => { | ||
| beforeEach(() => { | ||
| jest.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllTimers(); | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| function flush() { | ||
| act(() => { | ||
| jest.runAllTimers(); | ||
| }); | ||
| } | ||
|
|
||
| function renderHover() { | ||
| render( | ||
| <Trigger | ||
| action={['hover']} | ||
| popupAlign={placementAlignMap.left} | ||
| popup={<strong>trigger</strong>} | ||
| > | ||
| <div className="target" /> | ||
| </Trigger>, | ||
| ); | ||
|
|
||
| return document.querySelector('.target'); | ||
| } | ||
|
|
||
| function popup() { | ||
| return document.querySelector('.rc-trigger-popup'); | ||
| } | ||
|
|
||
| it('opens a hover popup on the first tap', () => { | ||
| tap(renderHover()); | ||
| flush(); | ||
|
|
||
| expect(popup()).toBeTruthy(); | ||
| expect(popup()).not.toHaveClass('rc-trigger-popup-hidden'); | ||
| }); | ||
|
|
||
| it('opens on the first tap of a pen that also fires touch events', () => { | ||
| tap(renderHover(), 'pen'); | ||
| flush(); | ||
|
|
||
| expect(popup()).not.toHaveClass('rc-trigger-popup-hidden'); | ||
| }); | ||
|
|
||
| it('closes on a second tap', () => { | ||
| const target = renderHover(); | ||
| tap(target); | ||
| flush(); | ||
|
|
||
| tap(target); | ||
| flush(); | ||
|
|
||
| expect(popup()).toHaveClass('rc-trigger-popup-hidden'); | ||
| }); | ||
|
|
||
| it('opens on a mouse hover after a tap', () => { | ||
| const target = renderHover(); | ||
| tap(target); | ||
| flush(); | ||
| tap(target); | ||
| flush(); | ||
| expect(popup()).toHaveClass('rc-trigger-popup-hidden'); | ||
|
|
||
| firePointer(target, 'pointerover', 'mouse'); | ||
| fireEvent.mouseEnter(target); | ||
| flush(); | ||
|
|
||
| expect(popup()).not.toHaveClass('rc-trigger-popup-hidden'); | ||
| }); | ||
|
|
||
| it('never renders the popup for a touch that turns into a scroll', () => { | ||
| const target = renderHover(); | ||
|
|
||
| firePointer(target, 'pointerover', 'touch'); | ||
| firePointer(target, 'pointerdown', 'touch'); | ||
| fireEvent.touchStart(target); | ||
| // The browser takes the touch over to scroll: no click follows. | ||
| firePointer(target, 'pointercancel', 'touch'); | ||
| firePointer(target, 'pointerout', 'touch'); | ||
| fireEvent.touchEnd(target); | ||
| flush(); | ||
|
|
||
| expect(popup()).toBeFalsy(); | ||
| }); | ||
|
|
||
| it('opens on a tap when it wraps another trigger', () => { | ||
| render( | ||
| <Trigger | ||
| action={['hover']} | ||
| popupAlign={placementAlignMap.left} | ||
| popup={<strong className="outer">outer</strong>} | ||
| > | ||
| <Trigger | ||
| action={['click']} | ||
| popupAlign={placementAlignMap.left} | ||
| popup={<strong className="inner">inner</strong>} | ||
| > | ||
| <div className="target" /> | ||
| </Trigger> | ||
| </Trigger>, | ||
| ); | ||
|
|
||
| tap(document.querySelector('.target')); | ||
| flush(); | ||
|
|
||
| expect(document.querySelector('.outer').parentElement).not.toHaveClass( | ||
| 'rc-trigger-popup-hidden', | ||
| ); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同时向嵌套 Trigger 转发鼠标指针进入和离开事件。
当外层 hover Trigger 包裹内层 click Trigger 时,连续两次触摸会关闭外层弹层,并留下外层
touchedRef=true。随后移动鼠标时,内层不会转发外层的onPointerEnter,因为passedEventList缺少该事件。已转发的onMouseEnter没有pointerType,因此仍被过滤。内层onClick也不会清除外层标记。请将
onPointerEnter和onPointerLeave加入转发列表。增加“嵌套触发器触摸关闭后,再用鼠标悬停打开”的回归测试。当前嵌套测试只验证首次触摸打开。