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): implement useClearRefinements (#3256)
* feat(hooks): implement useClearRefinements FX-737 #3252 * Update examples/hooks/components/ClearRefinements.tsx Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com> Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com>
- Loading branch information
1 parent
a24eaeb
commit 930b83d
Showing
7 changed files
with
160 additions
and
13 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
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,28 @@ | ||
import React from 'react'; | ||
import { | ||
useClearRefinements, | ||
UseClearRefinementsProps, | ||
} from 'react-instantsearch-hooks'; | ||
import { cx } from '../cx'; | ||
|
||
export type ClearRefinementsProps = React.ComponentProps<'div'> & | ||
UseClearRefinementsProps; | ||
|
||
export function ClearRefinements(props: ClearRefinementsProps) { | ||
const { canRefine, refine } = useClearRefinements(props); | ||
|
||
return ( | ||
<div className={cx('ais-ClearRefinements', props.className)}> | ||
<button | ||
className={cx( | ||
'ais-ClearRefinements-button', | ||
!canRefine && 'ais-ClearRefinements-button--disabled' | ||
)} | ||
disabled={!canRefine} | ||
onClick={() => refine()} | ||
> | ||
Clear Refinements | ||
</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
89 changes: 89 additions & 0 deletions
89
packages/react-instantsearch-hooks/src/__tests__/useClearRefinements.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,89 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import React from 'react'; | ||
|
||
import { createInstantSearchTestWrapper } from '../../../../test/utils'; | ||
import { useClearRefinements } from '../useClearRefinements'; | ||
import { useRefinementList } from '../useRefinementList'; | ||
|
||
import type { UseRefinementListProps } from '..'; | ||
|
||
describe('useClearRefinements', () => { | ||
test('returns the connector render state', async () => { | ||
const wrapper = createInstantSearchTestWrapper(); | ||
const { result, waitForNextUpdate } = renderHook( | ||
() => useClearRefinements(), | ||
{ | ||
wrapper, | ||
} | ||
); | ||
|
||
// Initial render state from manual `getWidgetRenderState` | ||
expect(result.current).toEqual({ | ||
hasRefinements: false, | ||
canRefine: false, | ||
refine: expect.any(Function), | ||
createURL: expect.any(Function), | ||
}); | ||
|
||
await waitForNextUpdate(); | ||
|
||
// InstantSearch.js state from the `render` lifecycle step | ||
expect(result.current).toEqual({ | ||
hasRefinements: false, | ||
canRefine: false, | ||
refine: expect.any(Function), | ||
createURL: expect.any(Function), | ||
}); | ||
}); | ||
|
||
test('returns items on render', async () => { | ||
const wrapper = createInstantSearchTestWrapper({ | ||
initialUiState: { | ||
indexName: { | ||
refinementList: { | ||
brand: ['Apple'], | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
const { result, waitForNextUpdate } = renderHook( | ||
() => useClearRefinements(), | ||
{ | ||
wrapper: ({ children }) => | ||
wrapper({ | ||
children: ( | ||
<> | ||
<RefinementList attribute="brand" /> | ||
{children} | ||
</> | ||
), | ||
}), | ||
} | ||
); | ||
|
||
// Initial render state from manual `getWidgetRenderState` | ||
expect(result.current).toEqual({ | ||
hasRefinements: false, | ||
canRefine: false, | ||
refine: expect.any(Function), | ||
createURL: expect.any(Function), | ||
}); | ||
|
||
await waitForNextUpdate(); | ||
|
||
// InstantSearch.js state from the `render` lifecycle step | ||
expect(result.current).toEqual({ | ||
hasRefinements: true, | ||
canRefine: true, | ||
refine: expect.any(Function), | ||
createURL: expect.any(Function), | ||
}); | ||
}); | ||
}); | ||
|
||
function RefinementList(props: UseRefinementListProps) { | ||
useRefinementList(props); | ||
|
||
return null; | ||
} |
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
17 changes: 17 additions & 0 deletions
17
packages/react-instantsearch-hooks/src/useClearRefinements.ts
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 connectClearRefinements from 'instantsearch.js/es/connectors/clear-refinements/connectClearRefinements'; | ||
|
||
import { useConnector } from './useConnector'; | ||
|
||
import type { | ||
ClearRefinementsConnectorParams, | ||
ClearRefinementsWidgetDescription, | ||
} from 'instantsearch.js/es/connectors/clear-refinements/connectClearRefinements'; | ||
|
||
export type UseClearRefinementsProps = ClearRefinementsConnectorParams; | ||
|
||
export function useClearRefinements(props?: UseClearRefinementsProps) { | ||
return useConnector< | ||
ClearRefinementsConnectorParams, | ||
ClearRefinementsWidgetDescription | ||
>(connectClearRefinements, props); | ||
} |