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

Fix a bug in the useMnemonics hook to make it compatible with textareas #2317

Merged
merged 5 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/mean-bees-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

useMnemonics hook ignores keydown events from textarea elements
14 changes: 14 additions & 0 deletions src/__tests__/hooks/useMnemonics.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {useMnemonics} from '../../hooks'
const Fixture = ({
onSelect = () => null,
hasInput = false,
hasTextarea = false,
refNotAttached = false
}: {
onSelect?: (event: React.KeyboardEvent<HTMLButtonElement>) => void
hasInput?: boolean
hasTextarea?: boolean
refNotAttached?: boolean
}) => {
const containerRef = React.createRef<HTMLDivElement>()
Expand All @@ -19,6 +21,7 @@ const Fixture = ({
<div ref={refNotAttached ? undefined : containerRef} data-testid="container">
{/* eslint-disable-next-line jsx-a11y/no-autofocus */}
{hasInput && <input autoFocus type="text" placeholder="Filter options" />}
{hasTextarea && <textarea autoFocus placeholder="Filter options" />}
<button onKeyDown={onSelect}>button 1</button>
<button onKeyDown={onSelect}>Button 2</button>
<button onKeyDown={onSelect}>third button</button>
Expand Down Expand Up @@ -154,6 +157,17 @@ describe('useTypeaheadFocus', () => {
expect(input).toEqual(document.activeElement)
})

it('Text area: when a textarea has focus, typeahead should not do anything', async () => {
const {getByTestId, getByPlaceholderText} = render(<Fixture hasTextarea={true} />)
const container = getByTestId('container')

const textArea = getByPlaceholderText('Filter options')
expect(textArea).toEqual(document.activeElement)

fireEvent.keyDown(container, {key: 'b', code: 'b'})
expect(textArea).toEqual(document.activeElement)
})

it('Missing ref: when a ref is not attached, typeahead should break the component', async () => {
const {getByTestId, getByText} = render(<Fixture refNotAttached={true} />)
const container = getByTestId('container')
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useMnemonics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const useMnemonics = (open: boolean, providedRef?: React.RefObject<HTMLEl
const handler = (event: KeyboardEvent) => {
// skip if a TextInput has focus
const activeElement = document.activeElement as HTMLElement
if (activeElement.tagName === 'INPUT') return
if (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA') return

// skip if used with modifier to preserve shortcuts like ⌘ + F
const hasModifier = event.ctrlKey || event.altKey || event.metaKey
Expand Down