|
2 | 2 |
|
3 | 3 | <!-- end auto-generated rule header --> |
4 | 4 |
|
5 | | -Disallows passing Ember event handler names directly as component arguments. |
| 5 | +It is possible to pass e.g. `@click` to an Ember component to override the default `click` event handler. For tagless components this will trigger an assertion though and can't be used as legitimate API, and for Glimmer components it will not work out of the box, like in Ember components, either. |
6 | 6 |
|
7 | | -Passing Ember DOM event names as component arguments (e.g., `@click`, `@submit`, `@keyDown`) is discouraged. Use the `{{on}}` modifier on the element instead for explicit event binding. |
8 | | - |
9 | | -## Rule Details |
10 | | - |
11 | | -This rule detects component arguments whose name (without the `@` prefix) matches an Ember DOM event name. Only PascalCase component invocations are checked (built-in `Input` and `Textarea` are excluded). |
12 | | - |
13 | | -The checked event names are: |
14 | | - |
15 | | -`touchStart`, `touchMove`, `touchEnd`, `touchCancel`, `keyDown`, `keyUp`, `keyPress`, `mouseDown`, `mouseUp`, `contextMenu`, `click`, `doubleClick`, `mouseMove`, `mouseEnter`, `mouseLeave`, `focusIn`, `focusOut`, `submit`, `change`, `input`, `dragStart`, `drag`, `dragEnter`, `dragLeave`, `dragOver`, `dragEnd`, `drop` |
| 7 | +This rule scans potential component invocations for these patterns and flags them as issues. |
16 | 8 |
|
17 | 9 | ## Examples |
18 | 10 |
|
19 | | -Examples of **incorrect** code for this rule: |
| 11 | +This rule **forbids** the following: |
20 | 12 |
|
21 | | -```gjs |
22 | | -<template> |
23 | | - <MyComponent @click={{this.handleClick}} /> |
24 | | -</template> |
| 13 | +```hbs |
| 14 | +<Foo @click={{this.handleClick}} /> |
25 | 15 | ``` |
26 | 16 |
|
27 | | -```gjs |
28 | | -<template> |
29 | | - <MyComponent @submit={{this.handleSubmit}} /> |
30 | | -</template> |
| 17 | +```hbs |
| 18 | +<Foo @keyPress={{this.handleClick}} /> |
31 | 19 | ``` |
32 | 20 |
|
33 | | -```gjs |
34 | | -<template> |
35 | | - <CustomButton @mouseEnter={{this.handleHover}} /> |
36 | | -</template> |
| 21 | +```hbs |
| 22 | +{{foo click=this.handleClick}} |
37 | 23 | ``` |
38 | 24 |
|
39 | | -Examples of **correct** code for this rule: |
| 25 | +This rule **allows** the following: |
40 | 26 |
|
41 | | -```gjs |
42 | | -<template> |
43 | | - <MyComponent @action={{this.handleAction}} /> |
44 | | -</template> |
| 27 | +```hbs |
| 28 | +<Foo @onClick={{this.handleClick}} /> |
45 | 29 | ``` |
46 | 30 |
|
47 | | -```gjs |
48 | | -<template> |
49 | | - <button {{on "click" this.handleClick}}>Click</button> |
50 | | -</template> |
| 31 | +```hbs |
| 32 | +<Foo @myCustomClickHandler={{this.handleClick}} /> |
51 | 33 | ``` |
52 | 34 |
|
53 | | -```gjs |
54 | | -<template> |
55 | | - <MyComponent @value={{this.value}} @onChange={{this.updateValue}} /> |
56 | | -</template> |
| 35 | +```hbs |
| 36 | +<Foo @onKeyPress={{this.handleClick}} /> |
57 | 37 | ``` |
58 | 38 |
|
59 | | -## Options |
| 39 | +```hbs |
| 40 | +{{foo onClick=this.handleClick}} |
| 41 | +``` |
60 | 42 |
|
61 | | -| Name | Type | Default | Description | |
62 | | -| -------- | -------- | ------- | -------------------------------------------------------------------------------------------------- | |
63 | | -| `ignore` | `object` | `{}` | Per-component exceptions. Keys are component names, values are arrays of argument names to ignore. | |
| 43 | +## Configuration |
64 | 44 |
|
65 | | -```js |
66 | | -module.exports = { |
67 | | - rules: { |
68 | | - 'ember/template-no-passed-in-event-handlers': [ |
69 | | - 'error', |
70 | | - { |
71 | | - ignore: { |
72 | | - MyComponent: ['@click', '@submit'], |
73 | | - }, |
74 | | - }, |
75 | | - ], |
76 | | - }, |
77 | | -}; |
78 | | -``` |
| 45 | +- boolean - `true` to enable / `false` to disable |
| 46 | + - object -- An object with the following keys: |
| 47 | + - `ignore` -- An object with the following keys/values: |
| 48 | + - key: string -- The name of the element or mustache statement to ignore event handlers for |
| 49 | + - value: array -- Event handler names. Event handler names should exclude the @ when specifying those intended for named arguments. This rule will ensure both non-named arguments and named arguments are both ignored appropriately. |
79 | 50 |
|
80 | | -## Migration |
| 51 | + eg. |
81 | 52 |
|
82 | | -Replace: |
| 53 | + Given the following configuration: |
83 | 54 |
|
84 | | -```gjs |
85 | | -<MyComponent @click={{this.handleClick}} /> |
86 | | -``` |
| 55 | + ```json |
| 56 | + { |
| 57 | + "ignore": { |
| 58 | + "MyButton": ["click"] |
| 59 | + } |
| 60 | + } |
| 61 | + ``` |
87 | 62 |
|
88 | | -With: |
| 63 | +## Migration |
89 | 64 |
|
90 | | -```gjs |
91 | | -<MyComponent> |
92 | | - <button {{on "click" this.handleClick}}>Click</button> |
93 | | -</MyComponent> |
94 | | -``` |
| 65 | +- create explicit component APIs for these events (e.g. `@click` -> `@onClick`) |
95 | 66 |
|
96 | 67 | ## References |
97 | 68 |
|
98 | | -- [eslint-plugin-ember template-no-passed-in-event-handlers](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/template-no-passed-in-event-handlers.md) |
| 69 | +- <https://api.emberjs.com/ember/release/classes/Component#event-handler-methods> |
0 commit comments