diff --git a/packages/core/src/shared/wizards/wizard.ts b/packages/core/src/shared/wizards/wizard.ts index f472e982813..26e32b623ec 100644 --- a/packages/core/src/shared/wizards/wizard.ts +++ b/packages/core/src/shared/wizards/wizard.ts @@ -94,7 +94,7 @@ export class Wizard>> { private assertReady() { // Check for `false` explicity so that the base-class constructor can access `this._form`. // We want to guard against confusion when implementing a subclass, not this base-class. - if (this._ready === false && this.init) { + if (this._ready === false) { throw Error('run() (or init()) must be called immediately after creating the Wizard') } } @@ -113,7 +113,7 @@ export class Wizard>> { return this._stepOffset[1] + this.stateController.totalSteps } - public get _form() { + protected get _form() { this.assertReady() return this.__form } @@ -136,7 +136,7 @@ export class Wizard>> { this._estimator = estimator } - public constructor(private readonly options: WizardOptions = {}) { + public constructor(protected readonly options: WizardOptions = {}) { this.stateController = new StateMachineController(options.initState as TState) this.__form = options.initForm ?? new WizardForm() this._exitStep = @@ -144,6 +144,14 @@ export class Wizard>> { // Subclass constructor logic should live in `init()`, if it exists. this._ready = !this.init + + if (typeof this.init === 'function') { + const _init = this.init.bind(this) + this.init = () => { + this._ready = true + return _init() + } + } } /** @@ -166,7 +174,6 @@ export class Wizard>> { if (!this._ready && this.init) { this._ready = true // Let init() use `this._form`. await this.init() - delete this.init } this.assignSteps() diff --git a/packages/core/src/test/shared/wizards/wizardTestUtils.ts b/packages/core/src/test/shared/wizards/wizardTestUtils.ts index 76c50f6ae94..8430278a4c6 100644 --- a/packages/core/src/test/shared/wizards/wizardTestUtils.ts +++ b/packages/core/src/test/shared/wizards/wizardTestUtils.ts @@ -65,9 +65,7 @@ function failIf(cond: boolean, message?: string): void { export async function createWizardTester>(wizard: Wizard | WizardForm): Promise> { if (wizard instanceof Wizard && wizard.init) { // Ensure that init() was called. Needed because createWizardTester() does not call run(). - ;(wizard as any)._ready = true await wizard.init() - delete wizard.init } const form = wizard instanceof Wizard ? wizard.boundForm : wizard