Skip to content

Commit

Permalink
Split search sort into component
Browse files Browse the repository at this point in the history
  • Loading branch information
robearlam committed Mar 24, 2023
1 parent 8eb1bd2 commit 4d7bd6d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useSearchResults, widget, WidgetDataType } from '@sitecore-discover/react';
import { WidgetComponentProps } from '@sitecore-discover/react/types';
import { SortSelect } from '@sitecore-discover/ui';
import Image from 'next/image';
import { ComponentType } from 'react';
import Loader from './Loader';
import QuerySummary from './QuerySummary';
import SearchPagination from './SearchPagination';
import SearchSort from './SearchSort';

export interface SearchResultsType extends WidgetComponentProps {
initialKeyphrase?: string;
Expand All @@ -29,8 +29,6 @@ export const SearchResults = (props: SearchResultsType) => {
};
});

const selectedSortIndex = sortChoices.findIndex((s) => s.name === sortType);

return (
<>
{isLoading && <Loader />}
Expand All @@ -42,21 +40,7 @@ export const SearchResults = (props: SearchResultsType) => {
<label>Sorting by: </label>
</div>
<div>
<SortSelect.Root defaultValue={sortChoices[selectedSortIndex]?.name} onValueChange={onSortChange}>
<SortSelect.Trigger>
<SortSelect.SelectValue>{selectedSortIndex > -1 ? sortChoices[selectedSortIndex].label : ''}</SortSelect.SelectValue>
<SortSelect.Icon />
</SortSelect.Trigger>
<SortSelect.Content>
<SortSelect.Viewport>
{sortChoices.map((option) => (
<SortSelect.Option value={option} key={option.name}>
<SortSelect.OptionText>{option.label}</SortSelect.OptionText>
</SortSelect.Option>
))}
</SortSelect.Viewport>
</SortSelect.Content>
</SortSelect.Root>
<SearchSort onSortChange={onSortChange} sortChoices={sortChoices} sortType={sortType} />
</div>
</div>
{articles.length && <QuerySummary currentPage={page} resultsPerPage={itemsPerPage} totalResults={totalItems} title={initialKeyphrase} />}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { SearchResponseSortChoice } from '@sitecore-discover/react';
import { SortSelect } from '@sitecore-discover/ui';

export interface SearchSortType {
onSortChange: (sortType: any) => void;
sortChoices: SearchResponseSortChoice[];
sortType: string;
}

export const SearchSort = (props: SearchSortType) => {
const { onSortChange, sortChoices, sortType } = props;
const selectedSortIndex = sortChoices.findIndex((s) => s.name === sortType);

return (
<SortSelect.Root defaultValue={sortChoices[selectedSortIndex]?.name} onValueChange={onSortChange}>
<SortSelect.Trigger>
<SortSelect.SelectValue>{selectedSortIndex > -1 ? sortChoices[selectedSortIndex].label : ''}</SortSelect.SelectValue>
<SortSelect.Icon />
</SortSelect.Trigger>
<SortSelect.Content>
<SortSelect.Viewport>
{sortChoices.map((option) => (
<SortSelect.Option value={option} key={option.name}>
<SortSelect.OptionText>{option.label}</SortSelect.OptionText>
</SortSelect.Option>
))}
</SortSelect.Viewport>
</SortSelect.Content>
</SortSelect.Root>
);
};

export default SearchSort;

0 comments on commit 4d7bd6d

Please sign in to comment.