Skip to content

Commit

Permalink
fix(widgets) Widgets should be reactive to prop changes (#9315)
Browse files Browse the repository at this point in the history
* fix(widgets) Widgets should be reactive to prop changes
  • Loading branch information
chrisgervang authored Dec 22, 2024
1 parent 05f8c95 commit 17ab465
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
5 changes: 4 additions & 1 deletion modules/widgets/src/compass-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export class CompassWidget implements Widget<CompassWidgetProps> {
}

setProps(props: Partial<CompassWidgetProps>) {
this.placement = props.placement ?? this.placement;
this.viewId = props.viewId ?? this.viewId;
const oldProps = this.props;
const el = this.element;
if (el) {
Expand All @@ -74,6 +76,7 @@ export class CompassWidget implements Widget<CompassWidgetProps> {
}

Object.assign(this.props, props);
this.update();
}

onViewportChange(viewport: Viewport) {
Expand Down Expand Up @@ -105,7 +108,7 @@ export class CompassWidget implements Widget<CompassWidgetProps> {
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);
Expand Down
8 changes: 5 additions & 3 deletions modules/widgets/src/fullscreen-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ export class FullscreenWidget implements Widget<FullscreenWidgetProps> {

private update() {
const {enterLabel, exitLabel} = this.props;
const el = this.element;
if (!el) {
const element = this.element;
if (!element) {
return;
}

Expand All @@ -91,10 +91,11 @@ export class FullscreenWidget implements Widget<FullscreenWidgetProps> {
className={this.fullscreen ? 'deck-widget-fullscreen-exit' : 'deck-widget-fullscreen-enter'}
/>
);
render(ui, el);
render(ui, element);
}

setProps(props: Partial<FullscreenWidgetProps>) {
this.placement = props.placement ?? this.placement;
const oldProps = this.props;
const el = this.element;
if (el) {
Expand All @@ -110,6 +111,7 @@ export class FullscreenWidget implements Widget<FullscreenWidgetProps> {
}

Object.assign(this.props, props);
this.update();
}

getContainer() {
Expand Down
46 changes: 27 additions & 19 deletions modules/widgets/src/zoom-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class ZoomWidget implements Widget<ZoomWidgetProps> {
id = 'zoom';
props: ZoomWidgetProps;
placement: WidgetPlacement = 'top-left';
orientation: 'vertical' | 'horizontal' = 'vertical';
viewId?: string | null = null;
viewports: {[id: string]: Viewport} = {};
deck?: Deck<any>;
Expand All @@ -60,7 +59,7 @@ export class ZoomWidget implements Widget<ZoomWidgetProps> {
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';
Expand All @@ -74,25 +73,9 @@ export class ZoomWidget implements Widget<ZoomWidgetProps> {
element.classList.add('deck-widget', 'deck-widget-zoom');
if (className) element.classList.add(className);
applyStyles(element, style);
const ui = (
<ButtonGroup orientation={this.orientation}>
<GroupedIconButton
onClick={() => this.handleZoomIn()}
label={this.props.zoomInLabel}
className="deck-widget-zoom-in"
/>
<GroupedIconButton
onClick={() => this.handleZoomOut()}
label={this.props.zoomOutLabel}
className="deck-widget-zoom-out"
/>
</ButtonGroup>
);
render(ui, element);

this.deck = deck;
this.element = element;

this.update();
return element;
}

Expand All @@ -102,6 +85,8 @@ export class ZoomWidget implements Widget<ZoomWidgetProps> {
}

setProps(props: Partial<ZoomWidgetProps>) {
this.placement = props.placement ?? this.placement;
this.viewId = props.viewId ?? this.viewId;
const oldProps = this.props;
const el = this.element;
if (el) {
Expand All @@ -117,6 +102,7 @@ export class ZoomWidget implements Widget<ZoomWidgetProps> {
}

Object.assign(this.props, props);
this.update();
}

onViewportChange(viewport: Viewport) {
Expand Down Expand Up @@ -146,4 +132,26 @@ export class ZoomWidget implements Widget<ZoomWidgetProps> {
this.handleZoom(viewport, viewport.zoom - 1);
}
}

private update() {
const element = this.element;
if (!element) {
return;
}
const ui = (
<ButtonGroup orientation={this.props.orientation}>
<GroupedIconButton
onClick={() => this.handleZoomIn()}
label={this.props.zoomInLabel}
className="deck-widget-zoom-in"
/>
<GroupedIconButton
onClick={() => this.handleZoomOut()}
label={this.props.zoomOutLabel}
className="deck-widget-zoom-out"
/>
</ButtonGroup>
);
render(ui, element);
}
}

0 comments on commit 17ab465

Please sign in to comment.