Skip to content

Commit c669f92

Browse files
committed
bug #950 [Live] Fixing bug with data-action="live#update" and inside clickable elements (weaverryan)
This PR was merged into the 2.x branch. Discussion ---------- [Live] Fixing bug with data-action="live#update" and inside clickable elements | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | None | License | MIT Small bug fix: ``` <a data-action="live#update" data-model="name" data-value="Dan" ><span>Change name to Dan</span></a> ``` In this case, the "click" would actually happen on the `span`, which is `event.target`. Switched to `event.currentTarget` to get the element that the listener is actually attached to (the `a`). Cheers! Commits ------- 9a5c60b [Live] Fixing bug with data-action="live#update" and inside clickable elements
2 parents 8344608 + 9a5c60b commit c669f92

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/LiveComponent/assets/dist/live_controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2697,9 +2697,9 @@ class LiveControllerDefault extends Controller {
26972697
}
26982698
update(event) {
26992699
if (event.type === 'input' || event.type === 'change') {
2700-
throw new Error(`Since LiveComponents 2.3, you no longer need data-action="live#update" on form elements. Found on element: ${getElementAsTagText(event.target)}`);
2700+
throw new Error(`Since LiveComponents 2.3, you no longer need data-action="live#update" on form elements. Found on element: ${getElementAsTagText(event.currentTarget)}`);
27012701
}
2702-
this.updateModelFromElementEvent(event.target, null);
2702+
this.updateModelFromElementEvent(event.currentTarget, null);
27032703
}
27042704
action(event) {
27052705
const rawAction = event.currentTarget.dataset.actionName;

src/LiveComponent/assets/src/live_controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@ export default class LiveControllerDefault extends Controller<HTMLElement> imple
138138
if (event.type === 'input' || event.type === 'change') {
139139
throw new Error(
140140
`Since LiveComponents 2.3, you no longer need data-action="live#update" on form elements. Found on element: ${getElementAsTagText(
141-
event.target
141+
event.currentTarget
142142
)}`
143143
);
144144
}
145145

146-
this.updateModelFromElementEvent(event.target, null);
146+
this.updateModelFromElementEvent(event.currentTarget, null);
147147
}
148148

149149
action(event: any) {

src/LiveComponent/assets/test/controller/model.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,17 @@ describe('LiveController data-model Tests', () => {
109109
it('renders correctly with data-value and live#update on a non-input', async () => {
110110
const test = await createTest({ name: 'Ryan' }, (data: any) => `
111111
<div ${initComponent(data)}>
112-
<a data-action="live#update" data-model="name" data-value="Jan">Change name to Jan</a>
112+
<a
113+
data-action="live#update"
114+
data-model="name"
115+
data-value="Jan"
116+
>Change name to Jan</a>
117+
118+
<a
119+
data-action="live#update"
120+
data-model="name"
121+
data-value="Dan"
122+
><span>Change name to Dan</span></a>
113123
114124
Name is: ${data.name}
115125
</div>
@@ -122,6 +132,14 @@ describe('LiveController data-model Tests', () => {
122132

123133
await waitFor(() => expect(test.element).toHaveTextContent('Name is: Jan'));
124134
expect(test.component.valueStore.getOriginalProps()).toEqual({name: 'Jan'});
135+
136+
test.expectsAjaxCall()
137+
.expectUpdatedData({ name: 'Dan' });
138+
139+
userEvent.click(getByText(test.element, 'Change name to Dan'));
140+
141+
await waitFor(() => expect(test.element).toHaveTextContent('Name is: Dan'));
142+
expect(test.component.valueStore.getOriginalProps()).toEqual({name: 'Dan'});
125143
});
126144

127145
it('falls back to using the name attribute when no data-model is present and <form data-model> is ancestor', async () => {

0 commit comments

Comments
 (0)