Skip to content

Commit

Permalink
Only e.preventDefault when Dropdown is active (#307)
Browse files Browse the repository at this point in the history
Update the `Dropdown` component to only call `e.preventDefault` for the up/down arrow keys if the dropdown is active.

J=SLAP-2351
TEST=manual

Spin up the test-site, load results, and see that the up and down arrow keys scroll the page when the dropdown is closed. When the dropdown is active, up/down arrows work as expected to navigate through dropdown options. Tested both the `SearchBar` and `FilterSearch` dropdowns.
  • Loading branch information
nmanu1 authored Sep 22, 2022
1 parent 7a139fb commit 61abcdb
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 23 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/run-linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ name: Run Linter

on:
pull_request:
branches:
- main
branches: [main, develop]

jobs:
linting:
uses: yext/slapshot-reusable-workflows/.github/workflows/run-linting.yml@v1
uses: yext/slapshot-reusable-workflows/.github/workflows/run-linting.yml@v1
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
build_script: |
npm i -D [email protected] [email protected]
npm run build
node_matrix: '["14.x", "16.x", "18.x"]'
node_matrix: '["14.x", "16.x", "18.x"]'
11 changes: 11 additions & 0 deletions .github/workflows/sync_develop_and_main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Create PR from main to develop

on:
push:
branches: main

jobs:
call_sync_develop_and_main:
uses: yext/slapshot-reusable-workflows/.github/workflows/sync_develop_and_main.yml@v1
secrets:
caller_github_token: ${{ secrets.GITHUB_TOKEN }}
11 changes: 11 additions & 0 deletions .github/workflows/version_update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Update package version for release & hotfix branches

on:
push:
branches: [release/*, hotfix/*]

jobs:
call_version_update:
uses: yext/slapshot-reusable-workflows/.github/workflows/version_update.yml@v1
secrets:
caller_github_token: ${{ secrets.GITHUB_TOKEN }}
3 changes: 1 addition & 2 deletions .github/workflows/wcag_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ name: WCAG tests

on:
pull_request:
branches:
- main
branches: [main, develop]

jobs:
call_wcag_test:
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@
"dependencies": {
"@microsoft/api-documenter": "^7.15.3",
"@microsoft/api-extractor": "^7.19.4",
"@restart/hooks": "^0.4.5",
"@restart/ui": "^1.0.1",
"@tailwindcss/forms": "^0.5.0",
"@yext/analytics": "^0.2.0-beta.3",
Expand Down
17 changes: 8 additions & 9 deletions src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createElement, isValidElement, PropsWithChildren, ReactNode, useEffect, useMemo, useRef, useState } from 'react';
import { createElement, isValidElement, KeyboardEvent, PropsWithChildren, ReactNode, useEffect, useMemo, useRef, useState } from 'react';
import { DropdownContext, DropdownContextType } from './DropdownContext';
import { InputContext, InputContextType } from './InputContext';
import useGlobalListener from '@restart/hooks/useGlobalListener';
import useRootClose from '@restart/ui/useRootClose';
import { FocusContext, FocusContextType } from './FocusContext';
import { v4 as uuid } from 'uuid';
Expand Down Expand Up @@ -101,15 +100,15 @@ export function Dropdown(props: PropsWithChildren<DropdownProps>): JSX.Element {
toggleDropdown(false);
}, { disabled: !isActive });

useGlobalListener('keydown', e => {
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
e.preventDefault();
}

function handleKeyDown(e: KeyboardEvent<HTMLDivElement>) {
if (!isActive) {
return;
}

if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
e.preventDefault();
}

if (e.key === 'ArrowDown') {
if (alwaysSelectOption && focusedIndex === items.length - 1) {
updateFocusedItem(0);
Expand Down Expand Up @@ -143,10 +142,10 @@ export function Dropdown(props: PropsWithChildren<DropdownProps>): JSX.Element {
} else if (!hasTyped) {
setHasTyped(true);
}
});
}

return (
<div ref={containerRef} className={isActive ? activeClassName : className}>
<div ref={containerRef} className={isActive ? activeClassName : className} onKeyDown={handleKeyDown}>
<DropdownContext.Provider value={dropdownContext}>
<InputContext.Provider value={inputContext}>
<FocusContext.Provider value={focusContext}>
Expand Down
10 changes: 4 additions & 6 deletions test-site/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 61abcdb

Please sign in to comment.