Skip to content
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

Pass arbitrary dataset values to data-turbo-confirm via data-turbo-method links? #811

Open
danieldpence opened this issue Dec 6, 2022 · 4 comments · May be fixed by #856
Open

Pass arbitrary dataset values to data-turbo-confirm via data-turbo-method links? #811

danieldpence opened this issue Dec 6, 2022 · 4 comments · May be fixed by #856

Comments

@danieldpence
Copy link

Following @excid3's awesome work in #525 enabling custom confirmation dialogs via data-turbo-confirm, I've run into a scenario where it doesn't seem like it's currently possible to pass additional information to the Turbo.setConfirmMethod function when data-turbo-confirm is placed on data-turbo-method links.

Consider:
I have a delete link:

<a data-turbo-method="delete" data-turbo-confirm="Are you sure?" data-title="My custom confirm title" data-type="negative" href="/delete/path">
Delete
</a>

Clicking that link causes a hidden form element to get created in order to trigger the form submission, here: https://github.com/hotwired/turbo/blob/main/src/observers/form_link_click_observer.ts#L33-L60

The problem is, only a subset of the dataset is passed along with the form element to the confirmMethod call.

In order to work around this, I'm manually querying the DOM for the original link via something like this:

window.Turbo.setConfirmMethod((message, element, submitter) => {
  let originalElement = submitter

  if (!submitter) {
    let matchingElements = document.querySelectorAll(
      `a[data-turbo-confirm="${message}"][data-turbo-method="${element.getAttribute('method')}"]`
    )
    originalElement = [...matchingElements].find((el) => element.action.includes(el.href))
  }

  const {
    type,
    title,
    turboConfirm: description,
    cancel,
    submit,
    turboConfirmDetail: eventDetail,
  } = originalElement.dataset

  ...
}

I'd love to be able to just pull those extra data attributes off of the element (the form element) instead. Would you be open to accepting a PR that allows that to be possible?

@dhh
Copy link
Member

dhh commented Dec 25, 2022

The element that has the data-confirm attribute should get passed along 👍

excid3 added a commit to excid3/turbo that referenced this issue Jan 30, 2023
@excid3 excid3 linked a pull request Jan 30, 2023 that will close this issue
@excid3
Copy link
Contributor

excid3 commented Jan 30, 2023

Just submitted #856 to fix this.

In the meantime, you should use button_to instead of a link_to if you need to access attributes in your custom confirm method.

excid3 added a commit to excid3/turbo that referenced this issue Jan 30, 2023
@gucki
Copy link

gucki commented Jul 26, 2023

@excid3 I can confirm this fixes this bug, but it doesn't play nice with ex. button groups of bootstrap :-(

shiftyp pushed a commit to shiftyp/ts-turbo that referenced this issue Sep 9, 2023
shiftyp pushed a commit to shiftyp/ts-turbo that referenced this issue Sep 10, 2023
shiftyp pushed a commit to shiftyp/ts-turbo that referenced this issue Sep 10, 2023
shiftyp pushed a commit to shiftyp/ts-turbo that referenced this issue Sep 10, 2023
@sansari
Copy link

sansari commented Oct 26, 2023

Hi, I ended up needing to implement a similar workaround—curious if there are still plans to merge this? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

5 participants