Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.

Commit aa2eb9b

Browse files
feat(hooks): introduce useConfigure (#3181)
1 parent 18297f8 commit aa2eb9b

File tree

6 files changed

+59
-1
lines changed

6 files changed

+59
-1
lines changed

examples/hooks/App.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import algoliasearch from 'algoliasearch/lite';
33
import React from 'react';
44
import { InstantSearch } from 'react-instantsearch-hooks';
55

6-
import { Hits, SearchBox, RefinementList } from './components';
6+
import { Hits, SearchBox, RefinementList, Configure } from './components';
77

88
import './App.css';
99

@@ -31,6 +31,8 @@ function Hit({ hit }: HitProps) {
3131
export function App() {
3232
return (
3333
<InstantSearch searchClient={searchClient} indexName="instant_search">
34+
<Configure hitsPerPage={15} />
35+
3436
<div
3537
style={{
3638
display: 'grid',
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { useConfigure, UseConfigureProps } from 'react-instantsearch-hooks';
2+
3+
export type ConfigureProps = UseConfigureProps;
4+
5+
export function Configure(props: ConfigureProps) {
6+
useConfigure(props);
7+
8+
return null;
9+
}

examples/hooks/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './Configure';
12
export * from './Hits';
23
export * from './RefinementList';
34
export * from './SearchBox';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { renderHook } from '@testing-library/react-hooks';
2+
3+
import { createInstantSearchTestWrapper } from '../../../../test/utils';
4+
import { useConfigure } from '../useConfigure';
5+
6+
describe('useConfigure', () => {
7+
test('returns the connector render state', async () => {
8+
const wrapper = createInstantSearchTestWrapper();
9+
const { result, waitForNextUpdate } = renderHook(
10+
() => useConfigure({ hitsPerPage: 40 }),
11+
{
12+
wrapper,
13+
}
14+
);
15+
16+
// Initial render state from manual `getWidgetRenderState`
17+
expect(result.current).toEqual({
18+
refine: expect.any(Function),
19+
});
20+
21+
await waitForNextUpdate();
22+
23+
// InstantSearch.js state from the `render` lifecycle step
24+
expect(result.current).toEqual({
25+
refine: expect.any(Function),
26+
});
27+
});
28+
});

packages/react-instantsearch-hooks/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export { default as version } from './version';
22
export * from './InstantSearch';
33
export * from './SearchIndex';
4+
export * from './useConfigure';
45
export * from './useConnector';
56
export * from './useHits';
67
export * from './useRefinementList';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import connectConfigure from 'instantsearch.js/es/connectors/configure/connectConfigure';
2+
3+
import { useConnector } from './useConnector';
4+
5+
import type {
6+
ConfigureConnectorParams,
7+
ConfigureWidgetDescription,
8+
} from 'instantsearch.js/es/connectors/configure/connectConfigure';
9+
10+
export type UseConfigureProps = ConfigureConnectorParams['searchParameters'];
11+
12+
export function useConfigure(props: UseConfigureProps) {
13+
return useConnector<ConfigureConnectorParams, ConfigureWidgetDescription>(
14+
connectConfigure,
15+
{ searchParameters: props }
16+
);
17+
}

0 commit comments

Comments
 (0)