WizardBuilder: sidebar categories stop opening/closing after "+ Page" (addPage() rebuilds twice)

Open Beginner friendly
#6,350 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript
Domain
frontend, tooling

Research direction

Start with src/WizardBuilder.js, especially addPage() and its saveComponent listener, then trace the fallback toggle wiring in src/WebformBuilder.js around attach(). Reproduce in the provided stock builder setup by adding a page and clicking a sidebar category twice. Done when adding or pasting a page leaves each category able to open and close on successive clicks.

Written by the indexing model from the issue text.

Description

Describe the bug

In the wizard builder, after clicking "+ Page" the sidebar categories (Basic, Advanced, Layout, Data, Premium) no longer open or close. A click on a closed category does nothing, and the open one can't be closed. Only reloading the builder brings them back. Copy + paste of a whole page has the same effect, because the paste goes through addPage() too.

It happens when Bootstrap's collapse JavaScript is not loaded (window.bootstrap.Collapse undefined), so the builder falls back to its own toggle handlers. That is the default with the stock formio.full.min.js.

Cause

WizardBuilder.addPage() rebuilds twice:

  1. It calls emitSaveComponentEvent(newPage, newPage, ...). WizardBuilder's own saveComponent listener finds newPage in this._form.components (it was just pushed there) and calls this.rebuild().
  2. Then addPage() itself ends with return this.rebuild();.

src/WizardBuilder.js on main, L58-L80 and L228-L247:

this.on('saveComponent', (component, originalComponent) => {
  ...
  if (this._form.components.includes(originalComponent)) {
    this._form.components[this._form.components.indexOf(originalComponent)] = component;
    this.rebuild();                       // <-- rebuild #1, triggered from addPage
  }
  ...
}, true);

addPage(page) {
  ...
  this._form.components.push(newPage);
  this.emitSaveComponentEvent(newPage, newPage, this._form, 'components', this._form.components.length - 1, true, newPage);
  this.emit('change', this._form);
  return this.rebuild();                  // <-- rebuild #2
}

WebformBuilder.attach() wires the fallback toggles inside super.attach(element).then(...) (L632, L657-L707). The two rebuilds overlap, both .then callbacks run after the second render, and both call loadRefs on the same final DOM. So every category button ends up with two click handlers, each calling hideShow(group, false, true). One click toggles the group open and straight back closed.

Probe on the repro page below (a click listener counter on [ref="sidebar-anchor"], Advanced category):

Action rebuild() calls click listeners on the button
builder.redraw() n/a 1
builder.addPage() 2 2

Version/Branch

Reproduced on @formio/[email protected] (current latest) and on 5.4.1. The code above is unchanged on main (90732a2).

To Reproduce

Self-contained page, stock formio.full.min.js, default builder options:

<link rel="stylesheet" href="formio.full.min.css">
<script src="formio.full.min.js"></script>
<div id="builder"></div>
<script>
  Formio.builder(document.getElementById('builder'), { display: 'wizard', components: [] })
    .then(function (b) { window.builder = b; });
</script>
  1. Click "Advanced" in the sidebar: it opens. Click it again: it closes. (Works.)
  2. Click "+ Page".
  3. Click "Advanced": it stays closed. Click "Basic": it stays open. Every category behaves the same way.
  4. Reload, then copy page 1 and paste it as a new page: same result.

Checked with Playwright on 5.7.0, clicking each category twice:

Basic Advanced, Layout, Data, Premium
Fresh builder closes, then opens open, then close
After "+ Page" stays open stay closed

Expected behavior

"+ Page" (and pasting a page) rebuilds the builder once, and every sidebar category still opens and closes on click.

Additional context

Suggested fix: make addPage() rebuild once. For example, let the saveComponent listener skip the rebuild for a page that addPage() itself just pushed, or drop the trailing this.rebuild() and return the listener's rebuild. Making the fallback toggle wiring idempotent (remove the previous sidebar handlers before adding new ones) would also protect against any other overlapping rebuilds.

Workaround for anyone hitting this: wrap the prototype so the rebuilds inside addPage() collapse into one (checked on 5.7.0 for both "+ Page" and page paste):

const proto = Formio.Builders.builders.wizard.prototype;
const original = proto.addPage;
proto.addPage = function (...args) {
  const hasOwn = Object.prototype.hasOwnProperty.call(this, 'rebuild');
  const ownRebuild = this.rebuild;
  this.rebuild = () => Promise.resolve();
  try {
    original.apply(this, args);
  } finally {
    if (hasOwn) {
      this.rebuild = ownRebuild;
    } else {
      delete this.rebuild;
    }
  }
  return this.rebuild();
};
Dominant language
JavaScript
Stars
2.1k
Forks
1.1k
Avg merge
3d 8h
Merged PRs (30d)
1

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from formio/formio.js

All issues in formio/formio.js

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.