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

Add keyboard shortcut ("/" key) to focus search input #380

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
"@mui/icons-material": "5.11.0",
"@mui/lab": "5.0.0-alpha.114",
"@mui/material": "5.11.2",
"@use-it/event-listener": "^0.1.7",
"commonmark": "0.30.0",
"keycode": "^2.2.1",
"svg-pan-zoom": "3.6.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/components/doc-explorer/DocExplorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export default class DocExplorer extends Component<
<div className="type-doc" key={navStack.length}>
{renderNavigation(previousNav, currentNav)}
<SearchBox
placeholder={`Search ${name}...`}
placeholder={`Search ${name}... [ / ]`}
value={currentNav.searchValue}
onSearch={this.handleSearch}
/>
Expand Down
19 changes: 18 additions & 1 deletion src/components/utils/SearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import Box from '@mui/material/Box';
import IconButton from '@mui/material/IconButton';
import Input from '@mui/material/Input';
import InputAdornment from '@mui/material/InputAdornment';
import { useEffect, useState } from 'react';
import useEventListener from '@use-it/event-listener';
import keycode from 'keycode';
import { useEffect, useRef, useState } from 'react';

interface SearchBoxProps {
placeholder: string;
Expand All @@ -15,18 +17,33 @@ export default function SearchBox(props: SearchBoxProps) {
const [value, setValue] = useState(props.value ?? '');
const { placeholder, onSearch } = props;

const inputRef = useRef<HTMLInputElement>();

useEffect(() => {
const timeout = setTimeout(() => onSearch(value), 200);
return () => clearTimeout(timeout);
}, [onSearch, value]);

useEventListener('keydown', (event: KeyboardEvent) => {
if (
inputRef.current &&
['/', 's'].includes(keycode(event)) &&
document.activeElement?.nodeName.toLowerCase() === 'body' &&
document.activeElement !== inputRef.current
) {
event.preventDefault();
inputRef.current.focus();
}
});

return (
<Box paddingLeft={2} paddingRight={2}>
<Input
fullWidth
placeholder={placeholder}
value={value}
onChange={(event) => setValue(event.target.value)}
inputRef={inputRef}
type="text"
className="search-box"
endAdornment={
Expand Down
41 changes: 41 additions & 0 deletions tests/demo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,44 @@ test('use search params to pass url', async ({ page }) => {
'display-schema-from-url.png',
);
});

test.describe('search input focus with hotkey', () => {
test('pressing / key to focus the search input', async ({ page }) => {
const voyagerPage = await gotoVoyagerPage(page);

await voyagerPage.waitForGraphToBeLoaded();
await expect(voyagerPage.page).toHaveScreenshot(
'before-search-input-focus.png',
);

await voyagerPage.page.keyboard.press('/');
await expect(voyagerPage.page).toHaveScreenshot(
'after-search-input-focus.png',
);

await voyagerPage.page.keyboard.press('/');
await expect(voyagerPage.page).toHaveScreenshot(
'after-search-input-typing-forward-slash.png',
);
});

test('should not focus on the search input if any other input is focused', async ({
page,
}) => {
const voyagerPage = await gotoVoyagerPage(page);
const { changeSchemaDialog } = voyagerPage;
const { sdlTab } = changeSchemaDialog;
await voyagerPage.waitForGraphToBeLoaded();

await changeSchemaDialog.openButton.click();
await sdlTab.tab.click();
await expect(voyagerPage.page).toHaveScreenshot(
'sdl-tab-before-typing-forward-slash.png',
);

await sdlTab.sdlTextArea.fill('/');
await expect(voyagerPage.page).toHaveScreenshot(
'sdl-tab-after-typing-forward-slash.png',
);
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/demo.spec.ts-snapshots/demo-after-resize-Demo-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/demo.spec.ts-snapshots/demo-before-resize-Demo-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/demo.spec.ts-snapshots/display-introspection-Demo-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/demo.spec.ts-snapshots/display-schema-from-url-Demo-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/demo.spec.ts-snapshots/display-sdl-Demo-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/demo.spec.ts-snapshots/loaded-demo-Demo-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/demo.spec.ts-snapshots/loading-animation-Demo-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/demo.spec.ts-snapshots/show-github-preset-Demo-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/demo.spec.ts-snapshots/show-star-wars-preset-Demo-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/demo.spec.ts-snapshots/show-yelp-preset-Demo-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.