All files / src/defaults Autocomplete.tsx

100% Statements 13/13
82.25% Branches 51/62
100% Functions 7/7
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175                                                      1x 56x 56x 56x   56x 8x     48x                                                     36x         72x                                                                 12x       60x                                               12x       60x                                                                                  
import { SearchKeyword, SearchProduct } from "../api/search"
 
export interface AutocompleteProps {
    /**
     * The response from the autocomplete API.
     */
    response?: {
        keywords: {
            hits: SearchKeyword[]
        }
        products: {
            hits: SearchProduct[]
        }
    }
    history?: {
        item: string
    }[]
}
 
/**
 * The default autocomplete component.
 *
 * @param props Autocomplete props.
 * @returns The JSX element.
 * @group Autocomplete
 * @category React
 */
export function Autocomplete({ response, history }: AutocompleteProps) {
    const hasKeywords = !!response?.keywords?.hits?.length
    const hasProducts = !!response?.products?.hits?.length
    const hasHistory = !!history?.length
 
    if (!hasKeywords && !hasProducts && !hasHistory) {
        return null
    }
 
    return (
        <div className="ns-autocomplete-results">
            {!hasKeywords && !hasProducts && hasHistory ? (
                <History history={history} />
            ) : hasKeywords || hasProducts ? (
                <>
                    {hasKeywords && (
                        <Keywords keywords={response.keywords.hits} />
                    )}
                    {hasProducts && (
                        <Products products={response.products.hits} />
                    )}
                    <div className="ns-autocomplete-submit">
                        <button
                            type="submit"
                            className="ns-autocomplete-button"
                        >
                            See all search results
                        </button>
                    </div>
                </>
            ) : null}
        </div>
    )
}
 
function History({ history }: { history: AutocompleteProps["history"] }) {
    return (
        <>
            <div className="ns-autocomplete-history">
                <div className="ns-autocomplete-header">Recently searched</div>
                {history?.map((hit, index) => {
                    return (
                        <div
                            className="ns-autocomplete-history-item"
                            data-ns-hit={JSON.stringify(hit)}
                            data-testid="history"
                            key={index}
                        >
                            {hit.item}
                            <a
                                href="#"
                                className="ns-autocomplete-history-item-remove"
                                data-ns-remove-history={hit.item}
                            >
                                &#x2715;
                            </a>
                        </div>
                    )
                })}
            </div>
            <div className="ns-autocomplete-history-clear">
                <button
                    type="button"
                    className="ns-autocomplete-button"
                    data-ns-remove-history="all"
                >
                    Clear history
                </button>
            </div>
        </>
    )
}
 
function Keywords({ keywords }: { keywords: SearchKeyword[] }) {
    return (
        <div className="ns-autocomplete-keywords">
            <div className="ns-autocomplete-header">Keywords</div>
            {keywords?.map((hit, index) => {
                return (
                    <div
                        className="ns-autocomplete-keyword"
                        data-ns-hit={JSON.stringify(hit)}
                        data-testid="keyword"
                        key={index}
                    >
                        {hit._highlight && hit._highlight.keyword ? (
                            <span
                                dangerouslySetInnerHTML={{
                                    __html: hit._highlight.keyword,
                                }}
                            ></span>
                        ) : (
                            <span>{hit.keyword}</span>
                        )}
                    </div>
                )
            })}
        </div>
    )
}
 
function Products({ products }: { products: SearchProduct[] }) {
    return (
        <div className="ns-autocomplete-products">
            <div className="ns-autocomplete-header">Products</div>
            {products.map(hit => {
                return (
                    <a
                        className="ns-autocomplete-product"
                        href="#"
                        key={hit.productId}
                        data-ns-hit={JSON.stringify(hit)}
                        data-testid="product"
                    >
                        <img
                            className="ns-autocomplete-product-image"
                            src={hit.imageUrl}
                            alt={hit.name}
                            width="60"
                            height="40"
                        />
                        <div className="ns-autocomplete-product-info">
                            {hit.brand && (
                                <div className="ns-autocomplete-product-brand">
                                    {hit.brand}
                                </div>
                            )}
                            <div className="ns-autocomplete-product-name">
                                {hit.name}
                            </div>
                            <div>
                                <span>{hit.price}&euro;</span>
                                {hit.listPrice &&
                                    hit.listPrice !== hit.price && (
                                        <span className="ns-autocomplete-product-list-price">
                                            {hit.listPrice}
                                            &euro;
                                        </span>
                                    )}
                            </div>
                        </div>
                    </a>
                )
            })}
        </div>
    )
}