-
-
Notifications
You must be signed in to change notification settings - Fork 106
fix(focus): add ignoreElement support and update deps (#557) #572
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { cleanup, render, act } from '@testing-library/react'; | ||
| import React from 'react'; | ||
| import ReactDOM from 'react-dom'; | ||
| import Drawer from '../src'; | ||
|
|
||
| // Mock useLockFocus to track calls | ||
| jest.mock('@rc-component/util/lib/Dom/focus', () => { | ||
| const actual = jest.requireActual('@rc-component/util/lib/Dom/focus'); | ||
|
|
||
| const useLockFocus = (visible: boolean, ...rest: any[]) => { | ||
| (globalThis as any).__useLockFocusVisible = visible; | ||
| const hooks = actual.useLockFocus(visible, ...rest); | ||
| const hooksArray = Array.isArray(hooks) ? hooks : [hooks]; | ||
| const proxyIgnoreElement = (ele: HTMLElement) => { | ||
| (globalThis as any).__ignoredElement = ele; | ||
| hooksArray[0](ele); | ||
| }; | ||
| return [proxyIgnoreElement, ...hooksArray.slice(1)] as ReturnType< | ||
| typeof actual.useLockFocus | ||
| >; | ||
|
Comment on lines
+12
to
+20
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The implementation of the mocked |
||
| }; | ||
|
|
||
| return { | ||
| ...actual, | ||
| useLockFocus, | ||
| }; | ||
| }); | ||
|
|
||
| describe('Drawer.Focus', () => { | ||
| beforeEach(() => { | ||
| jest.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.useRealTimers(); | ||
| cleanup(); | ||
| }); | ||
|
Comment on lines
+34
to
+37
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The mock for |
||
|
|
||
| it('should call ignoreElement when input in portal is focused', () => { | ||
| render( | ||
| <Drawer open> | ||
| <input type="text" id="drawer-input" /> | ||
| {ReactDOM.createPortal( | ||
| <input type="text" id="portal-input" />, | ||
| document.body, | ||
| )} | ||
| </Drawer>, | ||
| ); | ||
|
|
||
| act(() => { | ||
| jest.runAllTimers(); | ||
| }); | ||
|
|
||
| const input = document.getElementById('portal-input') as HTMLElement; | ||
| act(() => { | ||
| input.focus(); | ||
| }); | ||
|
|
||
| expect((globalThis as any).__ignoredElement).toBe(input); | ||
| }); | ||
| }); | ||
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.
The
e.targetof a focus event is of typeEventTarget, which is not assignable to theHTMLElementtype expected byignoreElement. This could lead to runtime errors. It's safer to add a type check to ensuree.targetis anHTMLElementbefore passing it toignoreElement.