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

Commit

Permalink
feat(hooks): introduce useConfigure (#3181)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Oct 27, 2021
1 parent 18297f8 commit aa2eb9b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 1 deletion.
4 changes: 3 additions & 1 deletion examples/hooks/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import algoliasearch from 'algoliasearch/lite';
import React from 'react';
import { InstantSearch } from 'react-instantsearch-hooks';

import { Hits, SearchBox, RefinementList } from './components';
import { Hits, SearchBox, RefinementList, Configure } from './components';

import './App.css';

Expand Down Expand Up @@ -31,6 +31,8 @@ function Hit({ hit }: HitProps) {
export function App() {
return (
<InstantSearch searchClient={searchClient} indexName="instant_search">
<Configure hitsPerPage={15} />

<div
style={{
display: 'grid',
Expand Down
9 changes: 9 additions & 0 deletions examples/hooks/components/Configure.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useConfigure, UseConfigureProps } from 'react-instantsearch-hooks';

export type ConfigureProps = UseConfigureProps;

export function Configure(props: ConfigureProps) {
useConfigure(props);

return null;
}
1 change: 1 addition & 0 deletions examples/hooks/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './Configure';
export * from './Hits';
export * from './RefinementList';
export * from './SearchBox';
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { renderHook } from '@testing-library/react-hooks';

import { createInstantSearchTestWrapper } from '../../../../test/utils';
import { useConfigure } from '../useConfigure';

describe('useConfigure', () => {
test('returns the connector render state', async () => {
const wrapper = createInstantSearchTestWrapper();
const { result, waitForNextUpdate } = renderHook(
() => useConfigure({ hitsPerPage: 40 }),
{
wrapper,
}
);

// Initial render state from manual `getWidgetRenderState`
expect(result.current).toEqual({
refine: expect.any(Function),
});

await waitForNextUpdate();

// InstantSearch.js state from the `render` lifecycle step
expect(result.current).toEqual({
refine: expect.any(Function),
});
});
});
1 change: 1 addition & 0 deletions packages/react-instantsearch-hooks/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { default as version } from './version';
export * from './InstantSearch';
export * from './SearchIndex';
export * from './useConfigure';
export * from './useConnector';
export * from './useHits';
export * from './useRefinementList';
Expand Down
17 changes: 17 additions & 0 deletions packages/react-instantsearch-hooks/src/useConfigure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import connectConfigure from 'instantsearch.js/es/connectors/configure/connectConfigure';

import { useConnector } from './useConnector';

import type {
ConfigureConnectorParams,
ConfigureWidgetDescription,
} from 'instantsearch.js/es/connectors/configure/connectConfigure';

export type UseConfigureProps = ConfigureConnectorParams['searchParameters'];

export function useConfigure(props: UseConfigureProps) {
return useConnector<ConfigureConnectorParams, ConfigureWidgetDescription>(
connectConfigure,
{ searchParameters: props }
);
}

0 comments on commit aa2eb9b

Please sign in to comment.