Skip to content

Commit

Permalink
fix: External elements shouldn't receive keypress events when Select …
Browse files Browse the repository at this point in the history
…is focused (#569)
  • Loading branch information
dogmar authored Feb 1, 2024
1 parent 0072d11 commit 57da95f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/components/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,12 @@ function Select({
})

return (
<SelectInner className="selectInner">
<SelectInner
className="selectInner"
onKeyDown={(e) => {
e.stopPropagation()
}}
>
<HiddenSelect
state={state}
triggerRef={ref}
Expand Down
24 changes: 23 additions & 1 deletion src/stories/Select.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { type ComponentProps, type Key, forwardRef, useState } from 'react'
import {
type ComponentProps,
type Key,
forwardRef,
useEffect,
useState,
} from 'react'
import styled, { useTheme } from 'styled-components'

import {
Expand Down Expand Up @@ -202,8 +208,24 @@ const IconFrameTrigger = forwardRef((props: any, ref) => (
/>
))

function useTestKeyCapture() {
useEffect(() => {
const handler = (e: KeyboardEvent) => {
if (e.target === document.body) return
alert(`Select box isn’t properly stopping keypress propagation`)
}

window.addEventListener('keydown', handler)

return () => window.removeEventListener('keydown', handler)
}, [])
}

function Template({ onFillLevel }: { onFillLevel: any }) {
const theme = useTheme()

useTestKeyCapture()

const [selectedKey, setSelectedKey] = useState<Key>()
const shownStep = 4
const [shownLimit, setShownLimit] = useState<number>(shownStep)
Expand Down

0 comments on commit 57da95f

Please sign in to comment.