PluginProbe
ElasticPress / 4.2.1
ElasticPress v4.2.1
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / assets / js / instant-results / components / layout / results.js

results.js in ElasticPress 4.2.1, at assets/js/instant-results/components/layout/results.js

94 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Internal depenencies.
3 */
4 import { useContext, useEffect, useRef, WPElement } from '@wordpress/element';
5 import { _n, sprintf } from '@wordpress/i18n';
6
7 /**
8 * Internal dependencies.
9 */
10 import Context from '../../context';
11 import Pagination from '../results/pagination';
12 import Result from '../results/result';
13 import Sort from '../tools/sort';
14
15 /**
16 * Search results component.
17 *
18 * @returns {WPElement} Component element.
19 */
20 export default () => {
21 const {
22 state: {
23 args: { offset, per_page },
24 searchResults,
25 searchedTerm,
26 totalResults,
27 },
28 dispatch,
29 } = useContext(Context);
30
31 const headingRef = useRef();
32
33 /**
34 * Handle clicking next.
35 */
36 const onNext = () => {
37 dispatch({ type: 'NEXT_PAGE' });
38 };
39
40 /**
41 * Handle clicking previous.
42 */
43 const onPrevious = () => {
44 dispatch({ type: 'PREVIOUS_PAGE' });
45 };
46
47 /**
48 * Effects.
49 */
50 useEffect(() => {
51 headingRef.current.scrollIntoView({ behavior: 'smooth' });
52 }, [offset]);
53
54 return (
55 <div className="ep-search-results">
56 <header className="ep-search-results__header">
57 <h1 className="ep-search-results__title" ref={headingRef} role="status">
58 {searchedTerm
59 ? sprintf(
60 /* translators: %1$d: results count. %2$s: Search term. */
61 _n(
62 '%1$d result for ā€œ%2$sā€œ',
63 '%1$d results for ā€œ%2$sā€œ',
64 totalResults,
65 'elasticpress',
66 ),
67 totalResults,
68 searchedTerm,
69 )
70 : sprintf(
71 /* translators: %d: results count. */
72 _n('%d result', '%d results', totalResults, 'elasticpress'),
73 totalResults,
74 )}
75 </h1>
76
77 <Sort />
78 </header>
79
80 {searchResults.map((hit) => (
81 <Result key={hit._id} hit={hit} />
82 ))}
83
84 <Pagination
85 offset={offset}
86 onNext={onNext}
87 onPrevious={onPrevious}
88 perPage={per_page}
89 total={totalResults}
90 />
91 </div>
92 );
93 };
94