From 064343cee990a2b131c0e93b5f6408d68c1693da Mon Sep 17 00:00:00 2001 From: Haroen Viaene Date: Thu, 27 Jan 2022 13:08:28 +0100 Subject: [PATCH] make other tests pass --- .../__tests__/connectDynamicWidgets-test.ts | 2 ++ .../dynamic-widgets/connectDynamicWidgets.ts | 17 ++++++++++------- src/types/widget.ts | 6 ++++++ src/widgets/index/index.ts | 6 +++--- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/connectors/dynamic-widgets/__tests__/connectDynamicWidgets-test.ts b/src/connectors/dynamic-widgets/__tests__/connectDynamicWidgets-test.ts index bd0481e357..7db246ba2b 100644 --- a/src/connectors/dynamic-widgets/__tests__/connectDynamicWidgets-test.ts +++ b/src/connectors/dynamic-widgets/__tests__/connectDynamicWidgets-test.ts @@ -691,6 +691,7 @@ describe('connectDynamicWidgets', () => { expect( dynamicWidgets.getRenderState(existingRenderState, createInitOptions()) ).toEqual({ + PREVENT_RENDER: true, dynamicWidgets: { attributesToRender: [], widgetParams, @@ -718,6 +719,7 @@ describe('connectDynamicWidgets', () => { createRenderOptions() ) ).toEqual({ + PREVENT_RENDER: true, dynamicWidgets: { attributesToRender: ['test1'], widgetParams, diff --git a/src/connectors/dynamic-widgets/connectDynamicWidgets.ts b/src/connectors/dynamic-widgets/connectDynamicWidgets.ts index 102950f2eb..c906c402d2 100644 --- a/src/connectors/dynamic-widgets/connectDynamicWidgets.ts +++ b/src/connectors/dynamic-widgets/connectDynamicWidgets.ts @@ -207,16 +207,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, }; }, diff --git a/src/types/widget.ts b/src/types/widget.ts index 034c6bd371..c4570c0d38 100644 --- a/src/types/widget.ts +++ b/src/types/widget.ts @@ -244,6 +244,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< diff --git a/src/widgets/index/index.ts b/src/widgets/index/index.ts index a10d0163a4..35657e96d9 100644 --- a/src/widgets/index/index.ts +++ b/src/widgets/index/index.ts @@ -650,8 +650,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` @@ -662,7 +662,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({ helper: helper!, parent: this,