Skip to content

Commit

Permalink
fix(search): keyboard support for enter key (#1545)
Browse files Browse the repository at this point in the history
* fix(search): keyboard support for enter key

* chore: cleanup
  • Loading branch information
alisonjoseph authored Nov 21, 2024
1 parent 2b680ed commit eaf0a12
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { graphql, navigate, useStaticQuery } from 'gatsby';
import cx from 'classnames';
import NavContext from '../../util/context/NavContext';
import { useOnClickOutside } from '../../util/hooks';
import { convertFilePathToUrl } from '../../util/convertFilePathToUrl';

import {
container,
Expand Down Expand Up @@ -118,7 +119,7 @@ const GlobalSearchInput = () => {
case 'Enter': {
e.preventDefault();
if (results[focusedItem]) {
navigate(results[focusedItem].path);
navigate(convertFilePathToUrl(results[focusedItem].path));
}
break;
}
Expand Down
18 changes: 2 additions & 16 deletions packages/gatsby-theme-carbon/src/components/GlobalSearch/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
tab,
} from './GlobalSearch.module.scss';

import { convertFilePathToUrl } from '../../util/convertFilePathToUrl';

export const MenuContext = createContext();

const Menu = ({ results, onKeyDown }) => {
Expand Down Expand Up @@ -77,22 +79,6 @@ const MenuItem = ({ page, index, onKeyDown, id }) => {
[active]: focusedItem === index,
});

function convertFilePathToUrl(filePath) {
const pagesIndex = filePath.lastIndexOf('/pages/');
if (filePath.lastIndexOf('/pages/') === -1) return null;

const fileName = filePath.slice(
pagesIndex + '/pages/'.length,
-'.mdx'.length
);
const normalizedFileName = fileName.endsWith('/index')
? fileName.slice(0, -'/index'.length)
: fileName;
const urlPath = `/${normalizedFileName.split('/').join('/')}/`;

return urlPath;
}

const url = convertFilePathToUrl(page.path);

return (
Expand Down
17 changes: 17 additions & 0 deletions packages/gatsby-theme-carbon/src/util/convertFilePathToUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function convertFilePathToUrl(filePath) {
if (!filePath) return null;

const pagesIndex = filePath.lastIndexOf('/pages/');
if (pagesIndex === -1) return null;

const fileName = filePath.slice(
pagesIndex + '/pages/'.length,
filePath.lastIndexOf('.')
);
const normalizedFileName = fileName.endsWith('/index')
? fileName.slice(0, -'/index'.length)
: fileName;
const urlPath = `/${normalizedFileName}/`;

return urlPath;
}

0 comments on commit eaf0a12

Please sign in to comment.