forked from algolia/instantsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ts): allow custom ui state and route state in routing (algolia#4816
) * feat(ts): allow custom ui state and route state in routing While I couldn't find a way to make InstantSearch itself generic (this gets passed to many places, which then loses generic), using the routing middleware directly is possible like this now ```ts import instantsearch from 'instantsearch.js/es' import { history } from 'instantsearch.js/es/lib/routers'; import { createRouterMiddleware } from 'instantsearch.js/es/middlewares'; import { StateMapping, UiState } from 'instantsearch.js/es/types'; type SwagIndexUiState = { swag: boolean }; type SwagUiState = { [indexName: string]: SwagIndexUiState }; const stateMapping: StateMapping<UiState & SwagUiState, SwagUiState> = { stateToRoute(uiState) { return Object.keys(uiState).reduce( (state, indexId) => ({ ...state, [indexId]: { swag: uiState[indexId].swag }, }), {} ); }, routeToState(routeState = {}) { return Object.keys(routeState).reduce( (state, indexId) => ({ ...state, [indexId]: routeState[indexId], }), {} ); }, }; const search = instantsearch(); search.use( createRouterMiddleware<UiState & SwagUiState, SwagUiState>({ router: history(), stateMapping, }) ); search.addWidgets([instantsearch.widgets.hits({ container })]); ``` * actually genericificate InstantSearch * already inferred so no need * Apply suggestions from code review * address feedback * better comment
- Loading branch information
Showing
14 changed files
with
199 additions
and
149 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
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
Oops, something went wrong.