-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move focus to active document (#1070)
- Loading branch information
Showing
8 changed files
with
130 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 @@ | ||
export * from './useRefAutoFocus'; |
76 changes: 76 additions & 0 deletions
76
web/packages/shared/hooks/useRefAutoFocus/useRefAutoFocus.test.tsx
This file contains 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,76 @@ | ||
import React, { DependencyList } from 'react'; | ||
|
||
import { render } from 'design/utils/testing'; | ||
|
||
import { useRefAutoFocus } from './useRefAutoFocus'; | ||
|
||
test('focus automatically when allowed', () => { | ||
const element = { | ||
focus: jest.fn(), | ||
}; | ||
render(<Focusable element={element} shouldFocus={true} />); | ||
expect(element.focus).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('do nothing when focus in not allowed', () => { | ||
const element = { | ||
focus: jest.fn(), | ||
}; | ||
render(<Focusable element={element} shouldFocus={false} />); | ||
expect(element.focus).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('refocus when deps list changes', () => { | ||
const element = { | ||
focus: jest.fn(), | ||
}; | ||
const { rerender } = render( | ||
<Focusable | ||
element={element} | ||
shouldFocus={true} | ||
reFocusDeps={['old prop']} | ||
/> | ||
); | ||
rerender( | ||
<Focusable | ||
element={element} | ||
shouldFocus={true} | ||
reFocusDeps={['new prop']} | ||
/> | ||
); | ||
expect(element.focus).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
test('do not refocus when deps list does not change', () => { | ||
const element = { | ||
focus: jest.fn(), | ||
}; | ||
const { rerender } = render( | ||
<Focusable | ||
element={element} | ||
shouldFocus={true} | ||
reFocusDeps={['old prop']} | ||
/> | ||
); | ||
rerender( | ||
<Focusable | ||
element={element} | ||
shouldFocus={true} | ||
reFocusDeps={['old prop']} | ||
/> | ||
); | ||
expect(element.focus).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
const Focusable = (props: { | ||
element: { focus(): void }; | ||
shouldFocus: boolean; | ||
reFocusDeps?: DependencyList; | ||
}) => { | ||
const ref = useRefAutoFocus({ | ||
shouldFocus: props.shouldFocus, | ||
refocusDeps: props.reFocusDeps, | ||
}); | ||
ref.current = props.element; | ||
return null; | ||
}; |
20 changes: 20 additions & 0 deletions
20
web/packages/shared/hooks/useRefAutoFocus/useRefAutoFocus.ts
This file contains 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,20 @@ | ||
import { DependencyList, MutableRefObject, useEffect, useRef } from 'react'; | ||
|
||
/** | ||
* Returns `ref` object that is automatically focused when `shouldFocus` is `true`. | ||
* Focus can be also re triggered by changing any of the `refocusDeps`. | ||
*/ | ||
export function useRefAutoFocus<T extends { focus(): void }>(options: { | ||
shouldFocus: boolean; | ||
refocusDeps?: DependencyList; | ||
}): MutableRefObject<T> { | ||
const ref = useRef<T>(); | ||
|
||
useEffect(() => { | ||
if (options.shouldFocus) { | ||
ref.current?.focus(); | ||
} | ||
}, [options.shouldFocus, ref, ...(options.refocusDeps || [])]); | ||
|
||
return ref; | ||
} |
This file contains 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 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 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 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