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

[skeleton] Use datalist for query suggestions for autocomplete experience #2506

Open
wants to merge 1 commit 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
8 changes: 5 additions & 3 deletions templates/skeleton/app/components/PageLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Await, Link} from '@remix-run/react';
import {Suspense} from 'react';
import {Suspense, useId} from 'react';
import type {
CartApiQueryFragment,
FooterQuery,
Expand Down Expand Up @@ -70,6 +70,7 @@ function CartAside({cart}: {cart: PageLayoutProps['cart']}) {
}

function SearchAside() {
const queriesDatalistId = useId();
return (
<Aside type="search" heading="SEARCH">
<div className="predictive-search">
Expand All @@ -84,6 +85,7 @@ function SearchAside() {
placeholder="Search"
ref={inputRef}
type="search"
list={queriesDatalistId}
/>
&nbsp;
<button onClick={goToSearch}>Search</button>
Expand All @@ -92,7 +94,7 @@ function SearchAside() {
</SearchFormPredictive>

<SearchResultsPredictive>
{({items, total, term, state, inputRef, closeSearch}) => {
{({items, total, term, state, closeSearch}) => {
const {articles, collections, pages, products, queries} = items;

if (state === 'loading' && term.current) {
Expand All @@ -107,7 +109,7 @@ function SearchAside() {
<>
<SearchResultsPredictive.Queries
queries={queries}
inputRef={inputRef}
queriesDatalistId={queriesDatalistId}
/>
<SearchResultsPredictive.Products
products={products}
Expand Down
37 changes: 10 additions & 27 deletions templates/skeleton/app/components/SearchResultsPredictive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ type PredictiveSearchItems = PredictiveSearchReturn['result']['items'];
type UsePredictiveSearchReturn = {
term: React.MutableRefObject<string>;
total: number;
inputRef: React.MutableRefObject<HTMLInputElement | null>;
items: PredictiveSearchItems;
fetcher: Fetcher<PredictiveSearchReturn>;
queriesDatalistId: string;
};

type SearchResultsPredictiveArgs = Pick<
UsePredictiveSearchReturn,
'term' | 'total' | 'inputRef' | 'items'
'term' | 'total' | 'items' | 'queriesDatalistId'
> & {
state: Fetcher['state'];
closeSearch: () => void;
Expand Down Expand Up @@ -244,35 +244,18 @@ function SearchResultsPredictiveProducts({

function SearchResultsPredictiveQueries({
queries,
inputRef,
}: PartialPredictiveSearchResult<'queries', 'inputRef'>) {
queriesDatalistId,
}: PartialPredictiveSearchResult<'queries', 'queriesDatalistId'>) {
if (!queries.length) return null;

return (
<div className="predictive-search-result" key="queries">
<h5>Queries</h5>
<ul>
{queries.map((suggestion) => {
if (!suggestion) return null;
<datalist id={queriesDatalistId}>
{queries.map((suggestion) => {
if (!suggestion) return null;

return (
<li className="predictive-search-result-item" key={suggestion.text}>
<div
role="presentation"
onClick={() => {
if (!inputRef.current) return;
inputRef.current.value = suggestion.text;
inputRef.current.focus();
}}
dangerouslySetInnerHTML={{
__html: suggestion?.styledText,
}}
/>
</li>
);
})}
</ul>
</div>
return <option key={suggestion.text} value={suggestion.text} />;
})}
</datalist>
);
}

Expand Down
Loading