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): add useMenu * fix: not optional * increase bundlesize with healthy margin
- Loading branch information
Showing
8 changed files
with
259 additions
and
6 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,62 @@ | ||
import React from 'react'; | ||
import { useMenu, UseMenuProps } from 'react-instantsearch-hooks'; | ||
|
||
import { cx } from '../cx'; | ||
import { isSpecialClick } from '../isSpecialClick'; | ||
|
||
export type MenuProps = React.ComponentProps<'div'> & UseMenuProps; | ||
|
||
export function Menu(props: MenuProps) { | ||
const { | ||
canToggleShowMore, | ||
isShowingMore, | ||
items, | ||
refine, | ||
createURL, | ||
toggleShowMore, | ||
} = useMenu(props); | ||
|
||
return ( | ||
<div className={cx('ais-Menu', props.className)}> | ||
<ul className="ais-Menu-list"> | ||
{items.map((item) => ( | ||
<li | ||
key={item.value} | ||
className={cx( | ||
'ais-Menu-item', | ||
item.isRefined && 'ais-Menu-item--selected' | ||
)} | ||
> | ||
<a | ||
className="ais-Menu-link" | ||
onClick={(event) => { | ||
if (isSpecialClick(event)) { | ||
return; | ||
} | ||
event.preventDefault(); | ||
refine(item.value); | ||
}} | ||
href={createURL(item.value)} | ||
> | ||
<span className="ais-Menu-label">{item.label}</span> | ||
<span className="ais-Menu-count">{item.count}</span> | ||
</a> | ||
</li> | ||
))} | ||
</ul> | ||
|
||
{props.showMore && ( | ||
<button | ||
className={cx( | ||
'ais-Menu-showMore', | ||
!canToggleShowMore && 'ais-Menu-showMore--disabled' | ||
)} | ||
disabled={!canToggleShowMore} | ||
onClick={toggleShowMore} | ||
> | ||
{isShowingMore ? 'Show less' : 'Show more'} | ||
</button> | ||
)} | ||
</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
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
42 changes: 42 additions & 0 deletions
42
packages/react-instantsearch-hooks/src/__tests__/useMenu.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,42 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
|
||
import { createInstantSearchTestWrapper } from '../../../../test/utils'; | ||
import { useMenu } from '../useMenu'; | ||
|
||
describe('useMenu', () => { | ||
test('returns the connector render state', async () => { | ||
const wrapper = createInstantSearchTestWrapper(); | ||
const { result, waitForNextUpdate } = renderHook( | ||
() => useMenu({ attribute: 'attribute' }), | ||
{ | ||
wrapper, | ||
} | ||
); | ||
|
||
// Initial render state from manual `getWidgetRenderState` | ||
expect(result.current).toEqual({ | ||
canRefine: false, | ||
canToggleShowMore: false, | ||
createURL: expect.any(Function), | ||
isShowingMore: false, | ||
items: [], | ||
refine: expect.any(Function), | ||
sendEvent: expect.any(Function), | ||
toggleShowMore: expect.any(Function), | ||
}); | ||
|
||
await waitForNextUpdate(); | ||
|
||
// InstantSearch.js state from the `render` lifecycle step | ||
expect(result.current).toEqual({ | ||
canRefine: false, | ||
canToggleShowMore: false, | ||
createURL: expect.any(Function), | ||
isShowingMore: false, | ||
items: [], | ||
refine: expect.any(Function), | ||
sendEvent: expect.any(Function), | ||
toggleShowMore: 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 connectMenu from 'instantsearch.js/es/connectors/menu/connectMenu'; | ||
|
||
import { useConnector } from './useConnector'; | ||
|
||
import type { | ||
MenuConnectorParams, | ||
MenuWidgetDescription, | ||
} from 'instantsearch.js/es/connectors/menu/connectMenu'; | ||
|
||
export type UseMenuProps = MenuConnectorParams; | ||
|
||
export function useMenu(props: UseMenuProps) { | ||
return useConnector<MenuConnectorParams, MenuWidgetDescription>( | ||
connectMenu, | ||
props | ||
); | ||
} |