Skip to content

Commit

Permalink
fix(select): disable keydown event propogation
Browse files Browse the repository at this point in the history
  • Loading branch information
w0wka91 committed Jun 15, 2019
1 parent f059acf commit a3f54f7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 47 deletions.
15 changes: 15 additions & 0 deletions src/components/select/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,18 @@ it('should delete the current value on escape', async () => {
fireEvent.keyDown(getByLabelText('Superheroes'), { keyCode: 27 })
expect(getByLabelText('Superheroes')).toHaveValue('')
})

it('should not propogate the keydown event', async () => {
const func = jest.fn()
const { getByLabelText, container } = render(
<Select
label="Superheroes"
value="Iron Man"
options={options}
onChange={func}
/>
)
container.addEventListener('keydown', func, false)
fireEvent.keyDown(getByLabelText('Superheroes'), { keyCode: 27 })
expect(func).toBeCalledTimes(0)
})
101 changes: 54 additions & 47 deletions src/components/select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,60 @@ function Select({
const filteredOptions = options.filter(o =>
o.label.toLocaleLowerCase().includes(state.inputValue.toLocaleLowerCase())
)
useEffect(() => {
const handleKeyDown = (evt: KeyboardEvent) => {
evt.stopPropagation()
switch (evt.keyCode) {
case 40:
dispatch({
type: 'highlight-option',
index:
state.collapsed ||
filteredOptions.length - 1 === state.highlightedOption
? 0
: state.highlightedOption + 1,
})
break
case 38:
dispatch({
type: 'highlight-option',
index:
state.highlightedOption === 0
? filteredOptions.length - 1
: state.highlightedOption - 1,
})
break
case 27:
if (!state.collapsed) {
dispatch({
type: 'collapse',
})
} else {
dispatch({
type: 'value-change',
inputValue: '',
})
}
break
case 13:
if (!state.collapsed) {
dispatch({
type: 'selection-change',
inputValue: filteredOptions[state.highlightedOption].label,
selectedKey: filteredOptions[state.highlightedOption].value,
})
}
break
}
}
if (inputRef.current)
inputRef.current.addEventListener('keydown', handleKeyDown, false)
return () => {
if (inputRef.current) {
inputRef.current.removeEventListener('keydown', handleKeyDown, false)
}
}
}, [state.highlightedOption, state.collapsed])
useEffect(() => {
if (!state.collapsed && inputRef.current && state.focused)
inputRef.current.focus()
Expand All @@ -121,7 +175,6 @@ function Select({
inputRef.current.focus()
}
}, [state.selectedKey])

return (
<div
className={css`
Expand Down Expand Up @@ -165,52 +218,6 @@ function Select({
`}
id={`input-${id}`}
ref={inputRef}
onKeyDown={evt => {
switch (evt.keyCode) {
case 40:
dispatch({
type: 'highlight-option',
index:
state.collapsed ||
filteredOptions.length - 1 === state.highlightedOption
? 0
: state.highlightedOption + 1,
})
break
case 38:
dispatch({
type: 'highlight-option',
index:
state.highlightedOption === 0
? filteredOptions.length - 1
: state.highlightedOption - 1,
})
break
case 27:
if (!state.collapsed) {
dispatch({
type: 'collapse',
})
} else {
dispatch({
type: 'value-change',
inputValue: '',
})
}
break
case 13:
if (!state.collapsed) {
dispatch({
type: 'selection-change',
inputValue:
filteredOptions[state.highlightedOption].label,
selectedKey:
filteredOptions[state.highlightedOption].value,
})
}
break
}
}}
onFocus={evt => {
dispatch({ type: 'focus' })
if (onFocus) onFocus(evt)
Expand Down

0 comments on commit a3f54f7

Please sign in to comment.