Skip to content

[Live] Fixing bug with data-action="live#update" and inside clickable elements #950

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

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/LiveComponent/assets/dist/live_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2697,9 +2697,9 @@ class LiveControllerDefault extends Controller {
}
update(event) {
if (event.type === 'input' || event.type === 'change') {
throw new Error(`Since LiveComponents 2.3, you no longer need data-action="live#update" on form elements. Found on element: ${getElementAsTagText(event.target)}`);
throw new Error(`Since LiveComponents 2.3, you no longer need data-action="live#update" on form elements. Found on element: ${getElementAsTagText(event.currentTarget)}`);
}
this.updateModelFromElementEvent(event.target, null);
this.updateModelFromElementEvent(event.currentTarget, null);
}
action(event) {
const rawAction = event.currentTarget.dataset.actionName;
Expand Down
4 changes: 2 additions & 2 deletions src/LiveComponent/assets/src/live_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ export default class LiveControllerDefault extends Controller<HTMLElement> imple
if (event.type === 'input' || event.type === 'change') {
throw new Error(
`Since LiveComponents 2.3, you no longer need data-action="live#update" on form elements. Found on element: ${getElementAsTagText(
event.target
event.currentTarget
)}`
);
}

this.updateModelFromElementEvent(event.target, null);
this.updateModelFromElementEvent(event.currentTarget, null);
}

action(event: any) {
Expand Down
20 changes: 19 additions & 1 deletion src/LiveComponent/assets/test/controller/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,17 @@ describe('LiveController data-model Tests', () => {
it('renders correctly with data-value and live#update on a non-input', async () => {
const test = await createTest({ name: 'Ryan' }, (data: any) => `
<div ${initComponent(data)}>
<a data-action="live#update" data-model="name" data-value="Jan">Change name to Jan</a>
<a
data-action="live#update"
data-model="name"
data-value="Jan"
>Change name to Jan</a>

<a
data-action="live#update"
data-model="name"
data-value="Dan"
><span>Change name to Dan</span></a>

Name is: ${data.name}
</div>
Expand All @@ -122,6 +132,14 @@ describe('LiveController data-model Tests', () => {

await waitFor(() => expect(test.element).toHaveTextContent('Name is: Jan'));
expect(test.component.valueStore.getOriginalProps()).toEqual({name: 'Jan'});

test.expectsAjaxCall()
.expectUpdatedData({ name: 'Dan' });

userEvent.click(getByText(test.element, 'Change name to Dan'));

await waitFor(() => expect(test.element).toHaveTextContent('Name is: Dan'));
expect(test.component.valueStore.getOriginalProps()).toEqual({name: 'Dan'});
});

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