Skip to content

Restrict parent and child component hierarchy in blazor #12302

@Stamo-Gochev

Description

@Stamo-Gochev

Tag helpers can restrict (to a certain extent) which tags can be their child tag helpers and vice versa using RestrictChildren:

[HtmlTargetElement("parent-taghelper")]
[RestrictChildren("child-taghelper")]
public partial class ParentTagHelper : TagHelper { ... }

and also with ParentTag:

[HtmlTargetElement("child-taghelper", ParentTag="parent-taghelper" )]
public partial class ChildTagHelper : TagHelper { ... }

What will be the way to do that in blazor? Having the ability to restrict parent/child hierarchy will be very important for the IntelliSense as the users of components will not be able to define components in incorrect places.

Describe the solution you'd like

How tag helpers are implemented to solve this issue (using strings) can be improved What I think will be better is to pass the name of the class that can be a parent of child in a way similar to:

[Child(MyNamespace.Components.ChildComponent)]
// multiple children
[Child(MyNamespace.Components.AnotherChildComponent)]
public class ParentComponent: ComponentBase

and the other way around:

[Parent(MyNamespace.Components.ParentComponent)]
// multiple parents
[Parent(MyNamespace.Components.AnotherParentComponent)]
public class ChildComponent : ComponentBase

The exact names of the attributes can be different. I am not sure whether they should be multiple attributes for a list of parents/children or a single one (e.g. [Children(ChildComponent, AnotherChildComponent)]), but the general idea is to use concrete class names instead of strings.

You can also suggest other ways to solve this issue apart from using attributes. What are your thoughts? @SteveSandersonMS @NTaylorMullen

Additional context

I want to point out an analogy with a problem that we faced when building tag helpers that we want to avoid when building blazor components.

The problem that we faced when creating tag helpers using the parent/child restrictions was that when a tag helper with the same name was used in more than one parents, the IntelliSense doesn't display it correctly. Here is an example:

We have the pointer tag helper in both the RadialGauge and LinearGauge tag helpers. As the name "pointer" is not unique, there is a problem like that:

taghelper

Note that although we are in the radial gauge tag helper, the IntelliSense picks up things from the linear gauge.

We discussed with @NTaylorMullen back in the days that the fix for that is to use unique names for the tag helpers, but this clutters the names as it requires a unique prefix before each name on each level (or at least in places of collisions). For longer nesting of hierarchies, which happens in more complex tag helpers, this is getting very long.

Restricting parent/child hierarchy with class names instead of strings should fix this problem (if this approach is chosen by blazor) as class names are unique according to a namespace.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Components Big RockThis issue tracks a big effort which can span multiple issuesPillar: Complete Blazor WebPriority:2Work that is important, but not critical for the releaseaffected-fewThis issue impacts only small number of customersarea-blazorIncludes: Blazor, Razor Componentscomponent ecosystemIndicates an issue which also has impact on 3rd party component ecosystemenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-razor.languagefeature-renderingFeatures dealing with how blazor renders componentsseverity-majorThis label is used by an internal tool

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions