Skip to content

Commit

Permalink
make other tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Haroenv committed Jan 23, 2023
1 parent c4d5418 commit 00d1713
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ describe('connectDynamicWidgets', () => {
expect(
dynamicWidgets.getRenderState(existingRenderState, createInitOptions())
).toEqual({
PREVENT_RENDER: true,
dynamicWidgets: {
attributesToRender: [],
widgetParams,
Expand Down Expand Up @@ -748,6 +749,7 @@ describe('connectDynamicWidgets', () => {
createRenderOptions()
)
).toEqual({
PREVENT_RENDER: true,
dynamicWidgets: {
attributesToRender: ['test1'],
widgetParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,19 @@ const connectDynamicWidgets: DynamicWidgetsConnector =
},
rendersOnPrevented: true,
getRenderState(renderState, renderOptions) {
const { PREVENT_RENDER = true } = renderState;
const dynamicWidgets = this.getWidgetRenderState(renderOptions);

// if we are in a "has results, but only just mounted widgets" state, add a flag
// in other cases the flag isn't set and we *do* render the other widgets
// TODO: improve this condition, reordering is allowed, we just want to make sure it's the same items
const willChangeWidgets =
renderState.dynamicWidgets?.attributesToRender.join('__') !==
dynamicWidgets?.attributesToRender.join('__');

return {
...renderState,
PREVENT_RENDER:
renderState.PREVENT_RENDER !== false &&
// if we are in a "has results, but only just mounted widgets" state, add a flag
// in other cases the flag isn't set and we *do* render the other widgets
// TODO: improve this condition, reordering is allowed, we just want to make sure it's the same items
renderState.dynamicWidgets?.attributesToRender.join('__') !==
dynamicWidgets?.attributesToRender.join('__'),
PREVENT_RENDER: PREVENT_RENDER !== false && willChangeWidgets,
dynamicWidgets,
};
},
Expand Down
6 changes: 6 additions & 0 deletions packages/instantsearch.js/src/types/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ type RequiredRenderStateLifeCycle<
>,
renderOptions: InitOptions | RenderOptions
) => IndexRenderState & TWidgetDescription['indexRenderState'];

/**
* If PREVENT_RENDER is set in the render state, still render this widget.
* This is only useful for cases where this widget is the one setting PREVENT_RENDER
*/
rendersOnPrevented?: boolean;
};

type RenderStateLifeCycle<
Expand Down
6 changes: 3 additions & 3 deletions packages/instantsearch.js/src/widgets/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,8 @@ const index = (widgetParams: IndexWidgetParams): IndexWidget => {
// a widget that causes the current state to be incomplete (dynamic widgets)
// can prevent following widgets from rendering this pass by setting the
// PREVENT_RENDER flag to "true"
const renderPrevented =
instantSearchInstance.renderState[this.getIndexId()].PREVENT_RENDER;
const { PREVENT_RENDER = false } =
instantSearchInstance.renderState?.[this.getIndexId()] ?? {};

localWidgets.forEach((widget) => {
// At this point, all the variables used below are set. Both `helper`
Expand All @@ -606,7 +606,7 @@ const index = (widgetParams: IndexWidgetParams): IndexWidget => {
// not have results yet.

// the widget can make itself rendering, even if the rest is prevented
if (widget.render && (!renderPrevented || widget.rendersOnPrevented)) {
if (widget.render && (!PREVENT_RENDER || widget.rendersOnPrevented)) {
widget.render(createRenderArgs(instantSearchInstance, this));
}
});
Expand Down

0 comments on commit 00d1713

Please sign in to comment.