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

Set the lit-html event context to this and add a @eventOptions decorator #244

Merged
merged 4 commits into from
Oct 5, 2018

Conversation

justinfagnani
Copy link
Contributor

Fixes #236 and #237

This lets users add methods as declarative event handlers without having to bind the method or use an arrow function. The decorator allows options to be set on the method for capture, passing and once.

class MyElement extends LitElement {
  render() {
    return html`<button @click=${this.onClick}>Click Me</button>`;
  }

  @eventOptions({capture: true})
  onClick(e) {
    console.log('clicked');
  }
}

This PR relies on lit/lit#531 and tests will fail till that is merged and released, though tests pass locally.

@justinfagnani
Copy link
Contributor Author

cc @katejeffreys and @arthurevans for docs.

@justinfagnani justinfagnani changed the title Events Set the lit-html event context to this and add a @eventOptions decorator Oct 5, 2018
Copy link
Member

@kevinpschaaf kevinpschaaf left a comment

Choose a reason for hiding this comment

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

Code LGTM, but:

  • Consider README updates above
  • Fix tslint error, confirm CI
  • Add to CHANGELOG

@@ -64,7 +64,7 @@ and renders declaratively using `lit-html`.
* expression: ``` html`<div>${disabled ? 'Off' : 'On'}</div>` ```
* property: ``` html`<x-foo .bar="${bar}"></x-foo>` ```
* attribute: ``` html`<div class="${color} special"></div>` ```
* event handler: ``` html`<button @click="${(e) => this._clickHandler(e)}"></button>` ```
* event handler: ``` html`<button @click="${this._clickHandler}"></button>` ```
Copy link
Member

Choose a reason for hiding this comment

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

Worth mentioning (here or elsewhere) that listener is bound to element (since that's not standard lit-html)?

Also, worth showing a non-decorator pattern for options?

Copy link

Choose a reason for hiding this comment

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

Please show/add as many explanations and examples as possible on things like this! :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The README is about to be replaced with real docs.... It already didn't say how to set up event handlers correctly, so I think this at least isn't any worse.

@thepassle
Copy link

thepassle commented Oct 5, 2018

Seconded (Edit: This was in reply to adams post in regard to examples/explanations)

@justinfagnani
Copy link
Contributor Author

@kevinpschaaf PTAL.

I didn't update the README yet because honestly I'm not sure where to put it, the README's far too long already, and it's about to be replaced.

@justinfagnani
Copy link
Contributor Author

Oh, you already approved @kevinpschaaf :)

@justinfagnani justinfagnani merged commit ffb3306 into master Oct 5, 2018
@kevinpschaaf
Copy link
Member

Hah, oh well. I was going to suggest to modify the "declarative rendering" section along these lines:

  • event handler: html`<button @click="${this._clickHandler}"></button>`, where the value is either
    • a function (to be called with the element as context), or
    • an object with a handleEvent property (called with the element as context), and
    • in either case, any additional addEventListener options such as capture: true may be included on the function or object to control the event listener

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants