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

[TwigComponent][LiveComponent] Live attributes forwarding with attribute nesting #1924

Closed
norkunas opened this issue Jun 18, 2024 · 7 comments

Comments

@norkunas
Copy link
Contributor

norkunas commented Jun 18, 2024

I have a live form and a other custom component which wraps dialog component and is opened by live action and is also live.
Dialog accepts nested attributes via modal:* properties and I need to forward live attributes to that modal:* part.

For example:

<twig:Dialog
  open
  fullWidth
  modal:aria-labelledby="my-dialog-title"
/>

How could I forward all data-live attributes? Would like to avoid this:

<twig:Dialog
  modal:id="{{ attributes.only('id').all()['id'] }}"
  modal:data-live-name-value="{{ attributes.only('data-live-name-value').all()['data-live-name-value'] }}"
  modal:data-live-url-value="{{ attributes.only('data-live-url-value').all()['data-live-url-value'] }}"
  modal:data-live-props-value="{{ attributes.only('data-live-props-value').all()['data-live-props-value'] }}"
  modal:data-live-csrf-value="{{ attributes.only('data-live-csrf-value').all()['data-live-csrf-value'] }}"
  modal:data-live-events-to-emit-value="{{ attributes.only('data-live-events-to-emit-value').all()['data-live-events-to-emit-value'] ?? false }}"
  modal:data-live-events-to-dispatch-value="{{ attributes.only('data-live-events-to-emit-value').all()['data-live-events-to-dispatch-value'] ?? false }}"
/>
@norkunas norkunas changed the title [TwigComponent][LiveComponent] Live attributes forwarding [TwigComponent][LiveComponent] Live attributes forwarding with attribute nesting Jun 18, 2024
@smnandre
Copy link
Collaborator

You could use a more direct syntax to avoid the situation you described

<twig:Dialog
  modal:id="{{ attributes.render('id') }}"
  modal:data-live-name-value="{{ attributes.render('data-live-name-value') }}"
  modal:data-live-url-value="{{ attributes.render('data-live-url-value') }}"
  modal:data-live-props-value="{{ attributes.render('data-live-props-value') }}"
  modal:data-live-csrf-value="{{ attributes.render('data-live-csrf-value') }}"
  ...

I did not understand your use case, and what you want to do... are you rendering the live attributes of your twig:Dialog component on the modal (non live) component ?

Little warning there, those attributes should not be considered public (they could change at any time).

@norkunas
Copy link
Contributor Author

I know that they could change anytime,thats why i want to forward all of them instead of defining manually. At first I missed that the events to emit/dispatch are dynamic,and not always present

@norkunas
Copy link
Contributor Author

I did not understand your use case, and what you want to do

Forward nested attributes, like I said in issue title not sure how that could be more clear :)

@norkunas
Copy link
Contributor Author

I thought that it would be possible to do something like:

{% set modalAttrs = { modal: { ...attributes } } %}

<twig:Dialog
  {{ ...modalAttrs }}
/>

but this didn't work

@smnandre
Copy link
Collaborator

I did not understand your use case, and what you want to do

Forward nested attributes, like I said in issue title not sure how that could be more clear :)

Haha... ok :) Let's pretend my sentence stopped there!

{% set modalAttrs = { modal: { ...attributes } } %}

<twig:Dialog
  {{ ...modalAttrs }}
/>

It could not work as you expect, as this nested notation is a very specific use case, and cannot be implied for everyone.

You could manipulate the array of attributes and prefix all key with "modal:" before spreading it i guess.

@norkunas
Copy link
Contributor Author

ah, that's right, keys are just strings..

You could manipulate the array of attributes and prefix all key with "modal:" before spreading it i guess.

too verbose to remap all the attributes :(

@norkunas norkunas closed this as not planned Won't fix, can't repro, duplicate, stale Jun 19, 2024
@smnandre
Copy link
Collaborator

# Not perfect in term of readability but does the job

{% set nested = attributes|reduce((c, v, k) => c|merge({('modal:'~k): v}), {}) %}
<twig:Dialog {{ ... nested }} />

# Or if the order has no importance..

{% set nested = attributes|reduce((c, v, k) => {('modal:'~k): v, ...c}, {}) %}
<twig:Dialog {{ ... nested }} />

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

No branches or pull requests

3 participants