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

refactor(v2): improve UX of search page #2761

Merged
merged 1 commit into from
May 17, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

/* eslint-disable jsx-a11y/no-autofocus */

import React, {useEffect, useState, useReducer, useRef} from 'react';

import algoliaSearch from 'algoliasearch/lite';
Expand All @@ -20,6 +22,10 @@ import Layout from '@theme/Layout';

import styles from './styles.module.css';

function pluralize(count, word) {
return count > 1 ? `${word}s` : word;
}

function Search() {
const {
siteConfig: {
Expand Down Expand Up @@ -237,6 +243,7 @@ function Search() {
onChange={handleSearchInputChange}
value={searchQuery}
autoComplete="off"
autoFocus
/>
</div>

Expand Down Expand Up @@ -266,7 +273,10 @@ function Search() {
<div
className={classnames('col', 'col--8', styles.searchResultsColumn)}>
{!!searchResultState.totalResults && (
<strong>{searchResultState.totalResults} documents found</strong>
<strong>
{searchResultState.totalResults}{' '}
{pluralize(searchResultState.totalResults, 'document')} found
</strong>
)}
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const Search = (props) => {
// Override algolia's default selection event, allowing us to do client-side
// navigation and avoiding a full page refresh.
handleSelected: (_input, _event, suggestion) => {
_event.stopPropagation();

// Use an anchor tag to parse the absolute url into a relative url
// Alternatively, we can use new URL(suggestion.url) but it's not supported in IE.
const a = document.createElement('a');
Expand Down Expand Up @@ -93,7 +95,7 @@ const Search = (props) => {
});

const handleSearchInputPressEnter = useCallback((e) => {
if (e.key === 'Enter') {
if (!e.defaultPrevented && e.key === 'Enter') {
navigateToSearchPage(e.target.value);
}
});
Expand Down