From 265dcdfb5e5889f0eada16e5c1f877cca0f0a9de Mon Sep 17 00:00:00 2001 From: CrazyTim Date: Sat, 15 Jan 2022 10:34:52 +1100 Subject: [PATCH] Fix: `onCurrentIndexChange` is raised when wheel is initialised --- src/wheel.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/wheel.js b/src/wheel.js index 8495a6b..184bca3 100644 --- a/src/wheel.js +++ b/src/wheel.js @@ -33,6 +33,8 @@ export class Wheel { * See README.md for property descriptions. */ init(props = {}) { + this._isInitialising = true; + this.borderColor = props.borderColor; this.borderWidth = props.borderWidth; this.debug = props.debug; @@ -167,7 +169,6 @@ export class Wheel { } - this.refreshCurrentIndex(angles); this.drawItemLines(ctx, angles); this.drawBorder(ctx); @@ -227,6 +228,8 @@ export class Wheel { this.applyDrag(delta); + this._isInitialising = false; + // Wait until next frame. this.frameRequestId = window.requestAnimationFrame(this.draw.bind(this)); @@ -577,10 +580,14 @@ export class Wheel { this._currentIndex = i; - this.onCurrentIndexChange?.({ - event: 'CurrentIndexChange', - currentIndex: this._currentIndex, - }); + if (!this._isInitialising) { + this.onCurrentIndexChange?.({ + event: 'currentIndexChange', + currentIndex: this._currentIndex, + }); + } + + break; } } @@ -846,6 +853,7 @@ export class Wheel { this._items = Defaults.wheel.items; } this.processItems(); + this.refreshCurrentIndex(this.getItemAngles(this.rotation)); } /** @@ -905,6 +913,7 @@ export class Wheel { } else { this._rotation = Defaults.wheel.rotation; } + this.refreshCurrentIndex(this.getItemAngles(this.rotation)); } /**