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

refactor: move event handlers to constructors #3314

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Changes from 1 commit
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
40 changes: 5 additions & 35 deletions tools/generate-component/boilerplate/component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { CSSResultGroup, PropertyValues, TemplateResult } from 'lit';
import { html, LitElement, nothing } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';

import { EventEmitter } from '../core/eventing.js';
import type { CSSResultGroup, TemplateResult } from 'lit';
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';

import style from './__noPrefixName__.scss?lit&inline';

Expand All @@ -16,40 +14,12 @@ import style from './__noPrefixName__.scss?lit&inline';
export class __nameUpperCase__ extends LitElement {
public static override styles: CSSResultGroup = style;
kyubisation marked this conversation as resolved.
Show resolved Hide resolved
public static readonly events: Record<string, string> = {
myEventName: 'myEventName',
// Add event names or remove
} as const;

/** myProp documentation */
@property({ attribute: 'my-prop', reflect: true }) public myProp: string = '';

/** _myState documentation */
@state() private _myState = false;

private _myEvent: EventEmitter<any> = new EventEmitter(
this,
__nameUpperCase__.events.myEventName,
);

private _onClickFn(): void {
this._myEvent.emit();
}

protected override willUpdate(changedProperties: PropertyValues<this>): void {
super.willUpdate(changedProperties);

if (changedProperties.has('myProp')) {
// do stuff
}
}

public override disconnectedCallback(): void {
super.disconnectedCallback();
// do stuff
}

protected override render(): TemplateResult {
return html`
<div class="__name__">${this._myState ? html`<slot></slot>` : nothing} ${this.myProp}</div>
<div class="__name__"><slot></slot></div>
`;
}
}
Expand Down
Loading