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 useNumericMenu (#3237)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Dec 10, 2021
1 parent 177ec56 commit e3056c9
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 9 deletions.
28 changes: 20 additions & 8 deletions examples/hooks/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import { InstantSearch, DynamicWidgets } from 'react-instantsearch-hooks';

import {
Configure,
CurrentRefinements,
HierarchicalMenu,
Highlight,
Hits,
HitsPerPage,
InfiniteHits,
Menu,
NumericMenu,
Pagination,
Panel,
QueryRuleContext,
QueryRuleCustomData,
RangeInput,
RefinementList,
Menu,
SearchBox,
SortBy,
HitsPerPage,
QueryRuleContext,
QueryRuleCustomData,
CurrentRefinements,
} from './components';
import { Tab, Tabs } from './components/layout';

Expand Down Expand Up @@ -53,7 +54,7 @@ export function App() {
indexName="instant_search"
routing={true}
>
<Configure hitsPerPage={15} />
<Configure ruleContexts={[]} />

<div className="Container">
<div>
Expand Down Expand Up @@ -82,6 +83,17 @@ export function App() {
<Panel header="Price">
<RangeInput attribute="price" />
</Panel>
<Panel header="Price range">
<NumericMenu
attribute="price"
items={[
{ label: 'All' },
{ label: 'Less than $500', end: 500 },
{ label: 'Between $500 - $1000', start: 500, end: 1000 },
{ label: 'More than $1000', start: 1000 },
]}
/>
</Panel>
</DynamicWidgets>
</div>
<div className="Search">
Expand All @@ -96,8 +108,8 @@ export function App() {
/>
<HitsPerPage
items={[
{ label: '4 hits per page', value: 4, default: true },
{ label: '8 hits per page', value: 8 },
{ label: '20 hits per page', value: 20, default: true },
{ label: '40 hits per page', value: 40 },
]}
/>
</div>
Expand Down
43 changes: 43 additions & 0 deletions examples/hooks/components/NumericMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react';
import { useNumericMenu, UseNumericMenuProps } from 'react-instantsearch-hooks';

import { cx } from '../cx';

export type NumericMenuProps = React.ComponentProps<'div'> &
UseNumericMenuProps;

export function NumericMenu(props: NumericMenuProps) {
const { hasNoResults, items, refine } = useNumericMenu(props);

return (
<div
className={cx(
'ais-NumericMenu',
hasNoResults && 'ais-NumericMenu--noRefinement',
props.className
)}
>
<ul className="ais-NumericMenu-list">
{items.map((item) => (
<li
key={item.value}
className={cx(
'ais-NumericMenu-item',
item.isRefined && 'ais-NumericMenu-item--selected'
)}
>
<label className="ais-NumericMenu-label">
<input
className="ais-NumericMenu-radio"
type="radio"
checked={item.isRefined}
onChange={() => refine(item.value)}
/>
<span className="ais-NumericMenu-labelText">{item.label}</span>
</label>
</li>
))}
</ul>
</div>
);
}
1 change: 1 addition & 0 deletions examples/hooks/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './Highlight';
export * from './Hits';
export * from './InfiniteHits';
export * from './Menu';
export * from './NumericMenu';
export * from './Pagination';
export * from './HitsPerPage';
export * from './Panel';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
},
{
"path": "packages/react-instantsearch-hooks/dist/umd/ReactInstantSearchHooks.min.js",
"maxSize": "36 kB"
"maxSize": "37 kB"
},
{
"path": "packages/react-instantsearch-dom/dist/umd/ReactInstantSearchDOM.min.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { renderHook } from '@testing-library/react-hooks';

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

describe('useNumericMenu', () => {
test('returns the connector render state', async () => {
const wrapper = createInstantSearchTestWrapper();
const { result, waitForNextUpdate } = renderHook(
() =>
useNumericMenu({
attribute: 'attribute',
items: [
{ label: 'All' },
{ label: 'Less than 500$', end: 500 },
{ label: 'Between 500$ - 1000$', start: 500, end: 1000 },
{ label: 'More than 1000$', start: 1000 },
],
}),
{
wrapper,
}
);

// Initial render state from manual `getWidgetRenderState`
expect(result.current).toEqual({
createURL: expect.any(Function),
hasNoResults: true,
items: [
{
isRefined: true,
label: 'All',
value: '%7B%7D',
},
{
isRefined: false,
label: 'Less than 500$',
value: '%7B%22end%22:500%7D',
},
{
isRefined: false,
label: 'Between 500$ - 1000$',
value: '%7B%22start%22:500,%22end%22:1000%7D',
},
{
isRefined: false,
label: 'More than 1000$',
value: '%7B%22start%22:1000%7D',
},
],
refine: expect.any(Function),
sendEvent: expect.any(Function),
});

await waitForNextUpdate();

// InstantSearch.js state from the `render` lifecycle step
expect(result.current).toEqual({
createURL: expect.any(Function),
hasNoResults: true,
items: [
{
isRefined: true,
label: 'All',
value: '%7B%7D',
},
{
isRefined: false,
label: 'Less than 500$',
value: '%7B%22end%22:500%7D',
},
{
isRefined: false,
label: 'Between 500$ - 1000$',
value: '%7B%22start%22:500,%22end%22:1000%7D',
},
{
isRefined: false,
label: 'More than 1000$',
value: '%7B%22start%22:1000%7D',
},
],
refine: expect.any(Function),
sendEvent: 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
Expand Up @@ -13,6 +13,7 @@ export * from './useHits';
export * from './useHitsPerPage';
export * from './useInfiniteHits';
export * from './useMenu';
export * from './useNumericMenu';
export * from './usePagination';
export * from './useQueryRules';
export * from './useRange';
Expand Down
17 changes: 17 additions & 0 deletions packages/react-instantsearch-hooks/src/useNumericMenu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import connectNumericMenu from 'instantsearch.js/es/connectors/numeric-menu/connectNumericMenu';

import { useConnector } from './useConnector';

import type {
NumericMenuConnectorParams,
NumericMenuWidgetDescription,
} from 'instantsearch.js/es/connectors/numeric-menu/connectNumericMenu';

export type UseNumericMenuProps = NumericMenuConnectorParams;

export function useNumericMenu(props: UseNumericMenuProps) {
return useConnector<NumericMenuConnectorParams, NumericMenuWidgetDescription>(
connectNumericMenu,
props
);
}

0 comments on commit e3056c9

Please sign in to comment.