fix: adjust styles and search behavior when there are no hits (#38073)

fix search bar to show "No tutorials found" and not redirect to search page without hits. 
change styles to match news.
This commit is contained in:
Kristofer Koishigawa
2020-01-20 16:47:06 +09:00
committed by Ahmad Abdolsaheb
parent c408cb9c1a
commit d29bfcd1e9
5 changed files with 46 additions and 23 deletions

View File

@ -112,8 +112,9 @@ export class SearchBar extends Component {
// return navigate('/search'); // return navigate('/search');
// Temporary redirect to News search results page // Temporary redirect to News search results page
// when non-empty search input submitted // when non-empty search input submitted and there
return query // are hits besides the footer
return query && hits.length > 1
? window.location.assign( ? window.location.assign(
`https://www.freecodecamp.org/news/search/?query=${encodeURIComponent( `https://www.freecodecamp.org/news/search/?query=${encodeURIComponent(
query query

View File

@ -13,22 +13,29 @@ const CustomHits = connectHits(
selectedIndex, selectedIndex,
handleHits handleHits
}) => { }) => {
const noHits = isEmpty(hits);
const footer = [ const footer = [
{ {
objectID: `default-hit-${searchQuery}`, objectID: `footer-${searchQuery}`,
query: searchQuery, query: searchQuery,
url: `https://www.freecodecamp.org/news/search/?query=${encodeURIComponent( url: noHits
searchQuery ? null
)}`, : `https://www.freecodecamp.org/news/search/?query=${encodeURIComponent(
title: `See all results for ${searchQuery}`, searchQuery
)}`,
title: noHits
? 'No tutorials found'
: `See all results for ${searchQuery}`,
_highlightResult: { _highlightResult: {
query: { query: {
value: ` value: noHits
See all results for ? 'No tutorials found'
<ais-highlight-0000000000> : `
${searchQuery} <ais-highlight-0000000000>
</ais-highlight-0000000000> See all results for
` ${searchQuery}
</ais-highlight-0000000000>
`
} }
} }
} }

View File

@ -1,11 +1,19 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { Highlight } from 'react-instantsearch-dom'; import { Highlight } from 'react-instantsearch-dom';
import { isEmpty } from 'lodash';
const Suggestion = ({ hit, handleMouseEnter, handleMouseLeave }) => { const Suggestion = ({ hit, handleMouseEnter, handleMouseLeave }) => {
const dropdownFooter = hit.objectID.includes('default-hit-'); const dropdownFooter = hit.objectID.includes('footer-');
return isEmpty(hit) || isEmpty(hit.objectID) ? null : ( const noHits = hit.title === 'No tutorials found';
return noHits ? (
<div
className={'no-hits-footer fcc_suggestion_item'}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
>
<span className='hit-name'>{hit.title}</span>
</div>
) : (
<a <a
className={ className={
dropdownFooter dropdownFooter

View File

@ -626,10 +626,6 @@ a[class^='ais-'] {
-webkit-transform: translateY(-50%); -webkit-transform: translateY(-50%);
transform: translateY(-50%); transform: translateY(-50%);
} }
.ais-SearchBox-submit {
left: 0.3rem;
top: 57%;
}
.ais-SearchBox-reset { .ais-SearchBox-reset {
right: 0.3rem; right: 0.3rem;
} }

View File

@ -15,13 +15,18 @@
} }
.ais-SearchBox-input { .ais-SearchBox-input {
padding: 1px 10px 1px 30px; padding: 0 10px 0 30px;
font-size: 18px; font-size: 18px;
display: inline-block; display: inline-block;
margin-top: 6px; margin-top: 6px;
height: 26px; height: 26px;
} }
.ais-SearchBox-submit {
left: 0.3rem;
top: 59.5%;
}
.fcc_searchBar .ais-SearchBox-input, .fcc_searchBar .ais-SearchBox-input,
.fcc_searchBar .ais-Hits { .fcc_searchBar .ais-Hits {
z-index: 100; z-index: 100;
@ -61,7 +66,7 @@
.fcc_suggestion_item { .fcc_suggestion_item {
display: block; display: block;
padding: 8px; padding: 5px;
color: var(--gray-00) !important; color: var(--gray-00) !important;
text-decoration: none; text-decoration: none;
} }
@ -104,13 +109,19 @@ and arrow keys */
/* Dropdown footer */ /* Dropdown footer */
.fcc_suggestion_footer { .fcc_suggestion_footer {
padding: 6.5px 8px 8px;
border-top: 1.5px solid var(--gray-00); border-top: 1.5px solid var(--gray-00);
} }
.fcc_suggestion_footer .hit-name .ais-Highlight .ais-Highlight-nonHighlighted { .fcc_suggestion_footer .hit-name .ais-Highlight {
font-weight: bold; font-weight: bold;
} }
.no-hits-footer {
border-top: 0px;
font-weight: 300;
}
.ais-SearchBox-input { .ais-SearchBox-input {
width: calc(100vw - 30px); width: calc(100vw - 30px);
} }