From 17ab465b0f167ebb2d338a5a795a0555f2230848 Mon Sep 17 00:00:00 2001 From: Chris Gervang Date: Sun, 22 Dec 2024 11:52:47 -0800 Subject: [PATCH] fix(widgets) Widgets should be reactive to prop changes (#9315) * fix(widgets) Widgets should be reactive to prop changes --- modules/widgets/src/compass-widget.tsx | 5 ++- modules/widgets/src/fullscreen-widget.tsx | 8 ++-- modules/widgets/src/zoom-widget.tsx | 46 +++++++++++++---------- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/modules/widgets/src/compass-widget.tsx b/modules/widgets/src/compass-widget.tsx index caeee46d4ef..8ba1feace90 100644 --- a/modules/widgets/src/compass-widget.tsx +++ b/modules/widgets/src/compass-widget.tsx @@ -59,6 +59,8 @@ export class CompassWidget implements Widget { } setProps(props: Partial) { + this.placement = props.placement ?? this.placement; + this.viewId = props.viewId ?? this.viewId; const oldProps = this.props; const el = this.element; if (el) { @@ -74,6 +76,7 @@ export class CompassWidget implements Widget { } Object.assign(this.props, props); + this.update(); } onViewportChange(viewport: Viewport) { @@ -105,7 +108,7 @@ export class CompassWidget implements Widget { return [0, 0]; } - update() { + private update() { const viewId = this.viewId || Object.values(this.viewports)[0]?.id || 'default-view'; const viewport = this.viewports[viewId]; const [rz, rx] = this.getRotation(viewport); diff --git a/modules/widgets/src/fullscreen-widget.tsx b/modules/widgets/src/fullscreen-widget.tsx index 3c149f0c1a3..59f7a78245f 100644 --- a/modules/widgets/src/fullscreen-widget.tsx +++ b/modules/widgets/src/fullscreen-widget.tsx @@ -79,8 +79,8 @@ export class FullscreenWidget implements Widget { private update() { const {enterLabel, exitLabel} = this.props; - const el = this.element; - if (!el) { + const element = this.element; + if (!element) { return; } @@ -91,10 +91,11 @@ export class FullscreenWidget implements Widget { className={this.fullscreen ? 'deck-widget-fullscreen-exit' : 'deck-widget-fullscreen-enter'} /> ); - render(ui, el); + render(ui, element); } setProps(props: Partial) { + this.placement = props.placement ?? this.placement; const oldProps = this.props; const el = this.element; if (el) { @@ -110,6 +111,7 @@ export class FullscreenWidget implements Widget { } Object.assign(this.props, props); + this.update(); } getContainer() { diff --git a/modules/widgets/src/zoom-widget.tsx b/modules/widgets/src/zoom-widget.tsx index 28f022db749..0301fa78d26 100644 --- a/modules/widgets/src/zoom-widget.tsx +++ b/modules/widgets/src/zoom-widget.tsx @@ -50,7 +50,6 @@ export class ZoomWidget implements Widget { id = 'zoom'; props: ZoomWidgetProps; placement: WidgetPlacement = 'top-left'; - orientation: 'vertical' | 'horizontal' = 'vertical'; viewId?: string | null = null; viewports: {[id: string]: Viewport} = {}; deck?: Deck; @@ -60,7 +59,7 @@ export class ZoomWidget implements Widget { this.id = props.id || 'zoom'; this.viewId = props.viewId || null; this.placement = props.placement || 'top-left'; - this.orientation = props.orientation || 'vertical'; + props.orientation = props.orientation || 'vertical'; props.transitionDuration = props.transitionDuration || 200; props.zoomInLabel = props.zoomInLabel || 'Zoom In'; props.zoomOutLabel = props.zoomOutLabel || 'Zoom Out'; @@ -74,25 +73,9 @@ export class ZoomWidget implements Widget { element.classList.add('deck-widget', 'deck-widget-zoom'); if (className) element.classList.add(className); applyStyles(element, style); - const ui = ( - - this.handleZoomIn()} - label={this.props.zoomInLabel} - className="deck-widget-zoom-in" - /> - this.handleZoomOut()} - label={this.props.zoomOutLabel} - className="deck-widget-zoom-out" - /> - - ); - render(ui, element); - this.deck = deck; this.element = element; - + this.update(); return element; } @@ -102,6 +85,8 @@ export class ZoomWidget implements Widget { } setProps(props: Partial) { + this.placement = props.placement ?? this.placement; + this.viewId = props.viewId ?? this.viewId; const oldProps = this.props; const el = this.element; if (el) { @@ -117,6 +102,7 @@ export class ZoomWidget implements Widget { } Object.assign(this.props, props); + this.update(); } onViewportChange(viewport: Viewport) { @@ -146,4 +132,26 @@ export class ZoomWidget implements Widget { this.handleZoom(viewport, viewport.zoom - 1); } } + + private update() { + const element = this.element; + if (!element) { + return; + } + const ui = ( + + this.handleZoomIn()} + label={this.props.zoomInLabel} + className="deck-widget-zoom-in" + /> + this.handleZoomOut()} + label={this.props.zoomOutLabel} + className="deck-widget-zoom-out" + /> + + ); + render(ui, element); + } }