DEV: Introduce a helper for handling events (#25433)

Instead of

```hbs
{{on "input" (action this.foo value="target.value")}}
{{on "input" (action (mut this.bar) value="target.value")}}
```

you can use:

```hbs
{{on "input" (with-event-value this.foo)}}
{{on "input" (with-event-value (fn (mut this.bar)))}}
```

or in gjs:

```gjs
import { fn } from "@ember/helper";
import { on } from "@ember/modifier";
import withEventValue from "discourse/helpers/with-event-value";
…
{{on "input" (withEventValue (fn (mut this.bar)))}}
```
This commit is contained in:
Jarek Radosz 2024-02-28 14:00:53 +01:00 committed by GitHub
parent 0e17ff8d09
commit 36a9b5d0fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 42 additions and 62 deletions

View File

@ -1,5 +1,5 @@
<input
{{on "input" (action (mut this.value) value="target.value")}}
{{on "input" (with-event-value (fn (mut this.value)))}}
type="number"
value={{this.value}}
min={{if this.setting.min this.setting.min null}}

View File

@ -6,7 +6,7 @@
class="filter-reports-input"
placeholder={{i18n "admin.dashboard.filter_reports"}}
autofocus={{true}}
{{on "input" (action "filterReports" value="target.value")}}
{{on "input" (with-event-value this.filterReports)}}
/>
</div>

View File

@ -5,10 +5,7 @@
{{i18n "category.position"}}
</label>
<input
{{on
"input"
(action (mut this.category.position) value="target.value")
}}
{{on "input" (with-event-value (fn (mut this.category.position)))}}
value={{this.category.position}}
type="number"
min="0"
@ -42,7 +39,7 @@
<input
{{on
"input"
(action (mut this.category.num_featured_topics) value="target.value")
(with-event-value (fn (mut this.category.num_featured_topics)))
}}
value={{this.category.num_featured_topics}}
type="number"
@ -197,9 +194,8 @@
<input
{{on
"input"
(action
(mut this.category.category_setting.num_auto_bump_daily)
value="target.value"
(with-event-value
(fn (mut this.category.category_setting.num_auto_bump_daily))
)
}}
value={{this.category.category_setting.num_auto_bump_daily}}
@ -216,9 +212,8 @@
<input
{{on
"input"
(action
(mut this.category.category_setting.auto_bump_cooldown_days)
value="target.value"
(with-event-value
(fn (mut this.category.category_setting.auto_bump_cooldown_days))
)
}}
value={{this.category.category_setting.auto_bump_cooldown_days}}

View File

@ -9,7 +9,7 @@
name="name"
placeholder={{i18n "admin.emoji.name"}}
@value={{readonly this.name}}
{{on "input" (action (mut this.name) value="target.value")}}
{{on "input" (with-event-value (fn (mut this.name)))}}
/>
</div>
</div>

View File

@ -33,7 +33,7 @@
class="time-input"
@value={{this._time}}
disabled={{this.timeInputDisabled}}
{{on "input" (action "onChangeTime" value="target.value")}}
{{on "input" (with-event-value this.onChangeTime)}}
/>
</div>
{{/if}}

View File

@ -22,10 +22,7 @@
<td>
<div class="reorder-categories-actions">
<input
{{on
"change"
(action (fn this.change entry) value="target.value")
}}
{{on "change" (with-event-value (fn this.change entry))}}
value={{entry.position}}
type="number"
min="0"

View File

@ -21,7 +21,7 @@
id="section-name"
{{on
"input"
(action (mut this.transformedModel.title) value="target.value")
(with-event-value (fn (mut this.transformedModel.title)))
}}
/>

View File

@ -10,7 +10,7 @@ export default TextField.extend({
init() {
this._super(...arguments);
deprecated(
`NumberField component is deprecated. Use native <input> elements instead.\ne.g. <input {{on "input" (action (mut this.value) value="target.value")}} type="number" value={{this.value}} />`,
`NumberField component is deprecated. Use native <input> elements instead.\ne.g. <input {{on "input" (with-event-value (fn (mut this.value)))}} type="number" value={{this.value}} />`,
{
id: "discourse.number-field",
since: "3.2.0.beta5",

View File

@ -246,10 +246,7 @@
id="search-min-post-count"
placeholder={{i18n "search.advanced.post.min.placeholder"}}
aria-label={{i18n "search.advanced.post.min.aria_label"}}
{{on
"input"
(action "onChangeSearchTermMinPostCount" value="target.value")
}}
{{on "input" (with-event-value this.onChangeSearchTermMinPostCount)}}
/>
{{d-icon "arrows-alt-h"}}
<Input
@ -259,10 +256,7 @@
id="search-max-post-count"
placeholder={{i18n "search.advanced.post.max.placeholder"}}
aria-label={{i18n "search.advanced.post.max.aria_label"}}
{{on
"input"
(action "onChangeSearchTermMaxPostCount" value="target.value")
}}
{{on "input" (with-event-value this.onChangeSearchTermMaxPostCount)}}
/>
</div>
</div>
@ -278,10 +272,7 @@
id="search-min-views"
placeholder={{i18n "search.advanced.min_views.placeholder"}}
aria-label={{i18n "search.advanced.min_views.aria_label"}}
{{on
"input"
(action "onChangeSearchTermMinViews" value="target.value")
}}
{{on "input" (with-event-value this.onChangeSearchTermMinViews)}}
/>
{{d-icon "arrows-alt-h"}}
<Input
@ -291,10 +282,7 @@
id="search-max-views"
placeholder={{i18n "search.advanced.max_views.placeholder"}}
aria-label={{i18n "search.advanced.max_views.aria_label"}}
{{on
"input"
(action "onChangeSearchTermMaxViews" value="target.value")
}}
{{on "input" (with-event-value this.onChangeSearchTermMaxViews)}}
/>
</div>
</div>

View File

@ -47,7 +47,7 @@
@value={{@link.name}}
class={{@link.nameCssClass}}
ariaLabel={{i18n "sidebar.sections.custom.links.name.label"}}
{{on "input" (action (mut @link.name) value="target.value")}}
{{on "input" (with-event-value (fn (mut @link.name)))}}
/>
{{#if @link.invalidNameMessage}}
<div class="name warning">
@ -62,7 +62,7 @@
@value={{@link.value}}
class={{@link.valueCssClass}}
ariaLabel={{i18n "sidebar.sections.custom.links.value.label"}}
{{on "input" (action (mut @link.value) value="target.value")}}
{{on "input" (with-event-value (fn (mut @link.value)))}}
/>
{{#if @link.invalidValueMessage}}
<div class="value warning">

View File

@ -7,7 +7,7 @@
@id="edit-name"
@value={{readonly this.tagInfo.name}}
@maxlength={{this.siteSettings.max_tag_length}}
@input={{action (mut this.newTagName) value="target.value"}}
@input={{with-event-value (fn (mut this.newTagName))}}
@autofocus="true"
/>
@ -16,10 +16,7 @@
@value={{readonly this.tagInfo.descriptionWithNewLines}}
placeholder={{i18n "tagging.description"}}
maxlength={{1000}}
{{on
"input"
(action (mut this.newTagDescription) value="target.value")
}}
{{on "input" (with-event-value (fn (mut this.newTagDescription)))}}
autofocus="true"
/>

View File

@ -0,0 +1,7 @@
import { get } from "@ember/object";
export default function withEventValue(mutFn) {
return function (event) {
return mutFn(get(event, "target.value"));
};
}

View File

@ -22,7 +22,7 @@
@value={{readonly this.filter}}
placeholder={{i18n "groups.index.all"}}
class="groups-header-filters-name no-blur"
{{on "input" (action "onFilterChanged" value="target.value")}}
{{on "input" (with-event-value this.onFilterChanged)}}
@type="search"
aria-description={{i18n "groups.index.search_results"}}
/>

View File

@ -41,10 +41,7 @@
@value={{readonly this.nameInput}}
placeholder={{i18n "directory.filter_name"}}
class="filter-name no-blur"
{{on
"input"
(action "onUsernameFilterChanged" value="target.value")
}}
{{on "input" (with-event-value this.onUsernameFilterChanged)}}
/>
{{#if this.showGroupFilter}}
<ComboBox

View File

@ -20,9 +20,14 @@ module("Integration | Component | char-counter", function (hooks) {
test("updating value updates counter", async function (assert) {
this.max = 50;
await render(
hbs`<CharCounter @value={{this.charCounterContent}} @max={{this.max}}><textarea {{on "input" (action (mut this.charCounterContent) value="target.value")}}></textarea></CharCounter>`
);
await render(hbs`
<CharCounter
@value={{this.charCounterContent}}
@max={{this.max}}
>
<textarea {{on "input" (with-event-value (fn (mut this.charCounterContent)))}}></textarea>
</CharCounter>
`);
assert
.dom(this.element)

View File

@ -25,7 +25,7 @@
)
}}
@value={{this.chatEmojiPickerManager.picker.initialFilter}}
@filterAction={{action this.didInputFilter value="target.value"}}
@filterAction={{with-event-value this.didInputFilter}}
@icons={{hash left="search"}}
@containerClass="chat-emoji-picker__filter"
autofocus={{true}}

View File

@ -17,7 +17,7 @@
class="chat-modal-create-channel__input"
@type="text"
@value={{this.name}}
{{on "input" (action this.onNameChange value="target.value")}}
{{on "input" (with-event-value this.onNameChange)}}
/>
</div>

View File

@ -14,10 +14,7 @@
@max={{this.descriptionMaxLength}}
>
<textarea
{{on
"input"
(action this.onChangeChatChannelDescription value="target.value")
}}
{{on "input" (with-event-value this.onChangeChatChannelDescription)}}
class="chat-modal-edit-channel-description__description-input"
placeholder={{i18n
"chat.channel_edit_description_modal.input_placeholder"

View File

@ -1,10 +1,7 @@
<StyleguideExample @title="<CharCounter>">
<CharCounter @max="50" @value={{@dummy.charCounterContent}}>
<textarea
{{on
"input"
(action (mut @dummy.charCounterContent) value="target.value")
}}
{{on "input" (with-event-value (fn (mut @dummy.charCounterContent)))}}
class="styleguide--char-counter"
></textarea>
</CharCounter>