Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(index): make IndexOptions['uiState'] and UiState react to generic #6288

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ export default (function connectGeoSearch<
return {
$$type,

init(initArgs) {
// Type is explicitly redefined, to avoid having the TWidgetParams type in the definition
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

init(initArgs: InitOptions<GeoSearchWidgetDescription<THit>>) {
const { instantSearchInstance } = initArgs;
const isFirstRendering = true;

Expand Down
4 changes: 3 additions & 1 deletion packages/instantsearch.js/src/connectors/hits/connectHits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
Unmounter,
Renderer,
IndexRenderState,
InitOptions,
} from '../../types';
import type { Banner, SearchResults } from 'algoliasearch-helper';

Expand Down Expand Up @@ -109,7 +110,8 @@ export default (function connectHits<TWidgetParams>(
return {
$$type: 'ais.hits',

init(initOptions) {
// Type is explicitly redefined, to avoid having the TWidgetParams type in the definition
init(initOptions: InitOptions<HitsWidgetDescription<THit>>) {
renderFn(
{
...this.getWidgetRenderState(initOptions),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
Unmounter,
UnknownWidgetParams,
IndexRenderState,
InitOptions,
} from '../../types';
import type {
Banner,
Expand Down Expand Up @@ -292,8 +293,8 @@ export default (function connectInfiniteHits<

return {
$$type: 'ais.infiniteHits',

init(initOptions) {
// Type is explicitly redefined, to avoid having the TWidgetParams type in the definition
init(initOptions: InitOptions<InfiniteHitsWidgetDescription<THit>>) {
renderFn(
{
...this.getWidgetRenderState(initOptions),
Expand Down
8 changes: 5 additions & 3 deletions packages/instantsearch.js/src/types/ui-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ type ConnectorUiStates = AutocompleteWidgetDescription['indexUiState'] &

type WidgetUiStates = PlacesWidgetDescription['indexUiState'];

export type IndexUiState = Partial<ConnectorUiStates & WidgetUiStates>;
export type IndexUiState<TExtraUiState = unknown> = Partial<
ConnectorUiStates & WidgetUiStates & TExtraUiState
>;

export type UiState = {
[indexId: string]: IndexUiState;
export type UiState<TExtraUiState = unknown> = {
[indexId: string]: IndexUiState<TExtraUiState>;
};
24 changes: 10 additions & 14 deletions packages/instantsearch.js/src/types/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ type SharedRenderOptions = {
) => string;
};

export type InitOptions = SharedRenderOptions & {
uiState: UiState;
export type InitOptions<
TWidgetDescription extends WidgetDescription = { $$type: string }
> = SharedRenderOptions & {
uiState: UiState<TWidgetDescription['indexUiState']>;
results?: undefined;
};

Expand Down Expand Up @@ -153,9 +155,7 @@ type SearchWidget<TWidgetDescription extends WidgetDescription> = {
getWidgetParameters?: (
state: SearchParameters,
widgetParametersOptions: {
uiState: Expand<
Partial<TWidgetDescription['indexUiState'] & IndexUiState>
>;
uiState: Expand<IndexUiState<TWidgetDescription['indexUiState']>>;
}
) => SearchParameters;
};
Expand All @@ -172,9 +172,7 @@ type RecommendWidget<
getWidgetParameters: (
state: RecommendParameters,
widgetParametersOptions: {
uiState: Expand<
Partial<TWidgetDescription['indexUiState'] & IndexUiState>
>;
uiState: Expand<IndexUiState<TWidgetDescription['indexUiState']>>;
}
) => RecommendParameters;
getRenderState: (
Expand Down Expand Up @@ -202,7 +200,7 @@ type RequiredWidgetLifeCycle<TWidgetDescription extends WidgetDescription> = {
/**
* Called once before the first search.
*/
init?: (options: InitOptions) => void;
init?: (options: InitOptions<TWidgetDescription>) => void;
/**
* Whether `render` should be called
*/
Expand Down Expand Up @@ -247,12 +245,12 @@ type RequiredUiStateLifeCycle<TWidgetDescription extends WidgetDescription> = {
* @param widgetStateOptions - Extra information to calculate uiState.
*/
getWidgetUiState: (
uiState: Expand<Partial<TWidgetDescription['indexUiState'] & IndexUiState>>,
uiState: Expand<IndexUiState<TWidgetDescription['indexUiState']>>,
widgetUiStateOptions: {
searchParameters: SearchParameters;
helper: Helper;
}
) => Partial<IndexUiState & TWidgetDescription['indexUiState']>;
) => IndexUiState<TWidgetDescription['indexUiState']>;

/**
* This function is required for a widget to be taken in account for routing.
Expand All @@ -276,9 +274,7 @@ type RequiredUiStateLifeCycle<TWidgetDescription extends WidgetDescription> = {
getWidgetSearchParameters: (
state: SearchParameters,
widgetSearchParametersOptions: {
uiState: Expand<
Partial<TWidgetDescription['indexUiState'] & IndexUiState>
>;
uiState: Expand<IndexUiState<TWidgetDescription['indexUiState']>>;
}
) => SearchParameters;
};
Expand Down