This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hooks): introduce
useSortBy
(#3190)
* feat(hooks): introduce `useSortBy` * chore(examples): add useSortBy in hooks example Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com>
- Loading branch information
1 parent
dd37ab4
commit 5cce33b
Showing
7 changed files
with
130 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from 'react'; | ||
|
||
import { useSortBy, UseSortByProps } from 'react-instantsearch-hooks'; | ||
|
||
export type SortByProps = React.ComponentProps<'div'> & UseSortByProps; | ||
export function SortBy(props: SortByProps) { | ||
const { currentRefinement, options, refine } = useSortBy(props); | ||
|
||
return ( | ||
<div className="ais-SortBy"> | ||
<select | ||
className="ais-SortBy-select" | ||
onChange={(event) => refine(event.target.value)} | ||
defaultValue={currentRefinement} | ||
> | ||
{options.map(({ label, value }) => ( | ||
<option className="ais-SortBy-option" key={value} value={value}> | ||
{label} | ||
</option> | ||
))} | ||
</select> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/react-instantsearch-hooks/src/__tests__/useSortBy.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
|
||
import { createInstantSearchTestWrapper } from '../../../../test/utils'; | ||
import { useSortBy } from '../useSortBy'; | ||
|
||
const items = [ | ||
{ label: 'Featured', value: 'indexName' }, | ||
{ label: 'Price (asc)', value: 'indexName_price_asc' }, | ||
{ label: 'Price (desc)', value: 'indexName_price_desc' }, | ||
]; | ||
|
||
describe('useSortBy', () => { | ||
test('returns the connector render state', async () => { | ||
const wrapper = createInstantSearchTestWrapper(); | ||
const { result, waitForNextUpdate } = renderHook( | ||
() => useSortBy({ items }), | ||
{ wrapper } | ||
); | ||
|
||
expect(result.current).toEqual({ | ||
currentRefinement: 'indexName', | ||
options: items, | ||
refine: expect.any(Function), | ||
hasNoResults: expect.any(Boolean), | ||
}); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual({ | ||
currentRefinement: 'indexName', | ||
options: items, | ||
refine: expect.any(Function), | ||
hasNoResults: expect.any(Boolean), | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import connectSortBy from 'instantsearch.js/es/connectors/sort-by/connectSortBy'; | ||
|
||
import { useConnector } from './useConnector'; | ||
|
||
import type { | ||
SortByConnectorParams, | ||
SortByWidgetDescription, | ||
} from 'instantsearch.js/es/connectors/sort-by/connectSortBy'; | ||
|
||
export type UseSortByProps = SortByConnectorParams; | ||
|
||
export function useSortBy(props: UseSortByProps) { | ||
return useConnector<SortByConnectorParams, SortByWidgetDescription>( | ||
connectSortBy, | ||
props | ||
); | ||
} |