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

Question about the passed Stimulus controller values #755

Open
titanve opened this issue Mar 15, 2024 · 7 comments
Open

Question about the passed Stimulus controller values #755

titanve opened this issue Mar 15, 2024 · 7 comments

Comments

@titanve
Copy link

titanve commented Mar 15, 2024

This is a quick question: Why should we only pass values in the element where we placed the data-controller?
Why we cannot place them in the child elements under the element where the data-controller is placed?

image

Thanks!

@marcoroth
Copy link
Member

Hey @titanve, the primary reason that speaks against this is that you could end up with more than value for the same controller and Stimulus Value, since you are allowed to place the same attribute on more than just the controller-element itself.

@marcoroth
Copy link
Member

With that being said, I think it would be neat to have a similar feature to the Stimulus Targets where you can define the same Stimulus Value more than once and get access to all the values in an array with the plural accessor.

So you could do something like:

<div data-controller="post">
  <article id="post_1" data-post-target="post" data-post-id-value="1"></article>
  <article id="post_2" data-post-target="post" data-post-id-value="2"></article>
  <article id="post_3" data-post-target="post" data-post-id-value="3"></article>
  <article id="post_4" data-post-target="post" data-post-id-value="4"></article>
</div>
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["post"]
  static values = {
    id: Number
  }

  connect() {
    this.idValue // => either `null` (since the data-attribute on the controller-element is not defined) or the first one it sees (`1`)
    this.idValues // => [1, 2, 3, 4]
  }
}

@titanve
Copy link
Author

titanve commented Mar 18, 2024

Hi @marcoroth

Is it possible to implement your suggestion? I would like to.

We would need to search down in the DOM tree, from where the data-controller="post" is placed, for any occurrence of data-post-*-value="some_value" and populate the array or arrays depending if we have more than one key value.

Thanks.

@david942j
Copy link

+1 to have the support of values

@sebastialonso
Copy link

sebastialonso commented Nov 28, 2024

@marcoroth

you could end up with more than value for the same controller and Stimulus Value

I think that's the use case OP might be referring to. At least I'm in a similar scenario and haven't been able to find a solution.

<div data-controller="filter">
   <h1>Choose filter</h1>
   <button data-filter-slug-value="GREEN" data-action="click -> map#filter">GREEN</button>
   <button data-filter-slug-value="RED" data-action="click -> map#filter">RED</button>
   <button data-filter-slug-value="BLUE" data-action="click -> map#filter">BLUE</button>
</div>

Maybe I'm approaching this the wrong way, but I'd like the filter method to get the "correct" value of slug whenever I click a button, which are generated programatically.
If I'm understanding this correctly, I'm always getting this.slugValue as empty becase data-*-value is not defined in the controller element?

@adrienpoly
Copy link
Member

@sebastialonso what you want to use here is the Action Parameters

  • Values are global to the controller
  • Action params are scoped to the action element

@sebastialonso
Copy link

I ended up going exactly with that, thanks @adrienpoly!

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

No branches or pull requests

5 participants