The Wayback Machine - https://web.archive.org/web/20220218121135/https://github.com/dotnet/aspnetcore/issues/40300
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

Blazor: Improve the way data-* can be passed to a component #40300

Open
1 task done
MithrilMan opened this issue Feb 18, 2022 · 0 comments
Open
1 task done

Blazor: Improve the way data-* can be passed to a component #40300

MithrilMan opened this issue Feb 18, 2022 · 0 comments

Comments

@MithrilMan
Copy link

@MithrilMan MithrilMan commented Feb 18, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

A component is not guaranteed to generate an html element but in most scenario it happens.
Actually there is a way to allow a component to allow to receive undefined explicit parameters, by using CaptureUnmatchedValues property of ParameterAttribute.

This mechanism is sub-optimal because you can't for instance filter which attributes you'll get, it's a "catch'em all"

For advanced scenario, using a data-* attribute is very useful, the problem is that we have either to explicit define all the different attributes we want to get with explicit properties like

[Parameter] public string DataTarget { get; set; }

<div data-target="@DataTarget">
</div>

or we just rely on the CaptureUnmatchedValues where we can just splat the dictionary into the div @attributes

Since data-* attributes are very useful to extend a component behavior by making use of the element dataset (e.g. to alter the DOM in javascript or let another component to attach event handlers to the DOM), the component creator doesn't know which data- attribute the component user needs and so the component user is forced to wrap the component in an html element and apply the data attribute there (being it a limiting factor).

would be helpful to have a way to allow a component to receive any data-* attribute and not everything.

Describe the solution you'd like

I can think about 2 solutions:
being able to automatically register data-attributes in ComponentBase, that would have a new exposed property
public Dictionary<string, object> DataAttributes { get; set; }

or allow to further configure the ParameterAttribute to explicit set the subset of attributes we are interested in catching (maybe using regex expression or tailoring this explicitly for data-* .

[Parameter( CaptureDataAttributes = true) ] public Dictionary<string, object>DataAttributes { get; set; }

The binding example could then have a way to bind all data attributes by using a syntax like
@attributes:data, @data, @data-attributes or any other syntax that may have sense

<div @attributes:data="@DataAttributes">
</div>

Thanks

Additional context

No response

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

Successfully merging a pull request may close this issue.

None yet
1 participant