Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(widgets) cleanup widget constructors #9312

Merged
merged 3 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions modules/widgets/src/compass-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ export class CompassWidget implements Widget<CompassWidgetProps> {
element?: HTMLDivElement;

constructor(props: CompassWidgetProps) {
this.id = props.id || 'compass';
this.viewId = props.viewId || null;
this.placement = props.placement || 'top-left';
props.transitionDuration = props.transitionDuration || 200;
props.label = props.label || 'Compass';
props.style = props.style || {};
this.props = props;
this.id = props.id ?? this.id;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How much do we gain by copying these over to field rather than just accessing them from this.props? Seems like a bunch of boilerplate code for widgets that might not be necessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe one line of boilerplate would be saved since id is the only required member.

The projected widgets like Tooltip or Marker don't expose the placement member as a props since they're always in "fill".

I see the duplicate code. Maybe an abstract class could help?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the case for an abstract class is quite black and white, at least not until the amount of shared code grows further.
I agree that having all code visible and not relying on base class magic is also valuable.

this.viewId = props.viewId ?? this.viewId;
this.placement = props.placement ?? this.placement;

this.props = {
...props,
transitionDuration: props.transitionDuration ?? 200,
label: props.label ?? 'Reset Compass',
style: props.style ?? {}
};
}

setProps(props: Partial<CompassWidgetProps>) {
Expand Down Expand Up @@ -125,7 +128,7 @@ export class CompassWidget implements Widget<CompassWidgetProps> {
this.handleCompassReset(viewport);
}
}}
label={this.props.label}
title={this.props.label}
style={{transform: `rotateX(${rx}deg)`}}
>
<svg fill="none" width="100%" height="100%" viewBox="0 0 26 26">
Expand Down
15 changes: 9 additions & 6 deletions modules/widgets/src/fullscreen-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ export class FullscreenWidget implements Widget<FullscreenWidgetProps> {
fullscreen: boolean = false;

constructor(props: FullscreenWidgetProps) {
this.id = props.id || 'fullscreen';
this.placement = props.placement || 'top-left';
props.enterLabel = props.enterLabel || 'Enter Fullscreen';
props.exitLabel = props.exitLabel || 'Exit Fullscreen';
props.style = props.style || {};
this.props = props;
this.id = props.id ?? this.id;
this.placement = props.placement ?? this.placement;

this.props = {
...props,
enterLabel: props.enterLabel ?? 'Enter Fullscreen',
exitLabel: props.exitLabel ?? 'Exit Fullscreen',
style: props.style ?? {}
};
}

onAdd({deck}: {deck: Deck<any>}): HTMLDivElement {
Expand Down
21 changes: 12 additions & 9 deletions modules/widgets/src/zoom-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,18 @@ export class ZoomWidget implements Widget<ZoomWidgetProps> {
element?: HTMLDivElement;

constructor(props: ZoomWidgetProps) {
this.id = props.id || 'zoom';
this.viewId = props.viewId || null;
this.placement = props.placement || 'top-left';
props.orientation = props.orientation || 'vertical';
props.transitionDuration = props.transitionDuration || 200;
props.zoomInLabel = props.zoomInLabel || 'Zoom In';
props.zoomOutLabel = props.zoomOutLabel || 'Zoom Out';
props.style = props.style || {};
this.props = props;
this.id = props.id ?? this.id;
this.viewId = props.viewId ?? this.viewId;
this.placement = props.placement ?? this.placement;

this.props = {
...props,
orientation: props.orientation ?? 'vertical',
transitionDuration: props.transitionDuration ?? 200,
zoomInLabel: props.zoomInLabel ?? 'Zoom In',
zoomOutLabel: props.zoomOutLabel ?? 'Zoom Out',
style: props.style ?? {}
};
}

onAdd({deck}: {deck: Deck<any>}): HTMLDivElement {
Expand Down
Loading