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 27, 2022
1 parent 88738cd commit 064343c
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 @@ -691,6 +691,7 @@ describe('connectDynamicWidgets', () => {
expect(
dynamicWidgets.getRenderState(existingRenderState, createInitOptions())
).toEqual({
PREVENT_RENDER: true,
dynamicWidgets: {
attributesToRender: [],
widgetParams,
Expand Down Expand Up @@ -718,6 +719,7 @@ describe('connectDynamicWidgets', () => {
createRenderOptions()
)
).toEqual({
PREVENT_RENDER: true,
dynamicWidgets: {
attributesToRender: ['test1'],
widgetParams,
Expand Down
17 changes: 10 additions & 7 deletions src/connectors/dynamic-widgets/connectDynamicWidgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
},
Expand Down
6 changes: 6 additions & 0 deletions src/types/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down
6 changes: 3 additions & 3 deletions src/widgets/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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,
Expand Down

0 comments on commit 064343c

Please sign in to comment.