Skip to content

Commit

Permalink
Add Application.registerDefaultEventNames and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
grncdr committed Jun 23, 2023
1 parent 7fd847d commit 4ecb43d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
23 changes: 22 additions & 1 deletion docs/reference/actions.md
Expand Up @@ -52,7 +52,7 @@ Stimulus lets you shorten the action descriptors for some common element/event p
<button data-action="gallery#next">…</button>
```

The full set of these shorthand pairs is as follows:
The built-in set of these shorthand pairs is as follows:

Element | Default Event
----------------- | -------------
Expand All @@ -65,6 +65,27 @@ input type=submit | click
select | change
textarea | input

This built-in set can be extended with the `Application.registerDefaultEventName` method, for example to associate default event names with custom elements.

<meta data-controller="callout" data-callout-text-value="gallery#next">

```js
import { Application } from "@hotwired/stimulus"

const app = new Application()
app.registerDefaultEventNames({ "custom-button": "click" })
app.start()
```

The above allows you to omit the event name on custom buttons:

```html
<custom-button data-action="gallery#next">…</custom-button>
```

Custom default event names must be registered before calling `start()`, or the application will not be able to add the correct event listeners.

```html

## KeyboardEvent Filter

Expand Down
4 changes: 4 additions & 0 deletions src/core/application.ts
Expand Up @@ -53,6 +53,10 @@ export class Application implements ErrorHandler {
this.actionDescriptorFilters[name] = filter
}

registerDefaultEventNames(extensions: Schema['defaultEventNames']) {
Object.assign(this.schema.defaultEventNames, extensions)
}

load(...definitions: Definition[]): void
load(definitions: Definition[]): void
load(head: Definition | Definition[], ...rest: Definition[]) {
Expand Down

0 comments on commit 4ecb43d

Please sign in to comment.