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

Only e.preventDefault when Dropdown is active #307

Merged
merged 4 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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: 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
77 changes: 42 additions & 35 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 { 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,49 +100,57 @@ export function Dropdown(props: PropsWithChildren<DropdownProps>): JSX.Element {
toggleDropdown(false);
}, { disabled: !isActive });

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

if (!isActive) {
return;
}
useEffect(() => {
const dropdownContainer = containerRef.current;
dropdownContainer?.addEventListener('keydown', handleKeyDown);
return () => {
dropdownContainer?.removeEventListener('keydown', handleKeyDown);
};

if (e.key === 'ArrowDown') {
if (alwaysSelectOption && focusedIndex === items.length - 1) {
updateFocusedItem(0);
} else {
updateFocusedItem(focusedIndex + 1);
function handleKeyDown(e: globalThis.KeyboardEvent) {
if (!isActive) {
return;
}
} else if (e.key === 'ArrowUp') {
if (alwaysSelectOption && focusedIndex === 0) {
updateFocusedItem(items.length - 1);
} else {
updateFocusedItem(focusedIndex - 1);

if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
e.preventDefault();
}
} else if (e.key === 'Tab' && !e.shiftKey) {
if (items.length !== 0) {
if (focusedIndex >= items.length - 1) {
updateFocusedItem(-1);
toggleDropdown(false);

if (e.key === 'ArrowDown') {
if (alwaysSelectOption && focusedIndex === items.length - 1) {
updateFocusedItem(0);
} else {
updateFocusedItem(focusedIndex + 1);
}
} else if (e.key === 'ArrowUp') {
if (alwaysSelectOption && focusedIndex === 0) {
updateFocusedItem(items.length - 1);
} else {
updateFocusedItem(focusedIndex - 1);
}
} else if (e.key === 'Tab' && !e.shiftKey) {
if (items.length !== 0) {
if (focusedIndex >= items.length - 1) {
updateFocusedItem(-1);
toggleDropdown(false);
} else {
updateFocusedItem(focusedIndex + 1);
e.preventDefault();
}
}
} else if (e.key === 'Tab' && e.shiftKey) {
if (focusedIndex > 0 || (!alwaysSelectOption && focusedIndex === 0)) {
updateFocusedItem(focusedIndex - 1);
e.preventDefault();
} else {
updateFocusedItem(-1);
toggleDropdown(false);
}
} else if (!hasTyped) {
setHasTyped(true);
}
} else if (e.key === 'Tab' && e.shiftKey) {
if (focusedIndex > 0 || (!alwaysSelectOption && focusedIndex === 0)) {
updateFocusedItem(focusedIndex - 1);
e.preventDefault();
} else {
updateFocusedItem(-1);
toggleDropdown(false);
}
} else if (!hasTyped) {
setHasTyped(true);
}
});
}, [updateFocusedItem, alwaysSelectOption, isActive, focusedIndex, hasTyped, toggleDropdown, items.length]);

return (
<div ref={containerRef} className={isActive ? activeClassName : className}>
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.