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
useNumericMenu
(#3237)
- Loading branch information
1 parent
177ec56
commit e3056c9
Showing
7 changed files
with
170 additions
and
9 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
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> | ||
); | ||
} |
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
87 changes: 87 additions & 0 deletions
87
packages/react-instantsearch-hooks/src/__tests__/useNumericMenu.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,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), | ||
}); | ||
}); | ||
}); |
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 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 | ||
); | ||
} |