diff --git a/src/HydroComponent.cs b/src/HydroComponent.cs index 9b6191d..cb8c531 100644 --- a/src/HydroComponent.cs +++ b/src/HydroComponent.cs @@ -57,6 +57,7 @@ public abstract class HydroComponent : ViewComponent /// /// Provides component's key value /// + [JsonProperty] protected string Key { get; private set; } /// @@ -130,8 +131,8 @@ private void ConfigurePolls() /// /// Implementation of ViewComponent's InvokeAsync method /// - /// Parameters - /// Key + /// An object with component parameters + /// Local identifier to distinguish components of same type public async Task InvokeAsync(object parameters = null, string key = null) { ApplyParameters(parameters); @@ -424,7 +425,7 @@ private async Task RenderOnlineNestedComponent(IPersistentState persiste if (IsComponentIdRendered(componentId)) { - return GetComponentPlaceholderTemplate(componentId); + return GetComponentPlaceholderTemplate(componentId, Key); } if (!await AuthorizeAsync()) @@ -438,8 +439,8 @@ private async Task RenderOnlineNestedComponent(IPersistentState persiste return await GenerateComponentHtml(componentId, persistentState, includeScripts: true); } - private static string GetComponentPlaceholderTemplate(string componentId) => - $"
"; + private static string GetComponentPlaceholderTemplate(string componentId, string key) => + $"
"; private async Task RenderStaticComponent(IPersistentState persistentState) { @@ -487,9 +488,14 @@ private async Task GenerateComponentHtml(string componentId, IPersistent var rootElement = root.ChildNodes.First(n => n.NodeType == HtmlNodeType.Element); rootElement.SetAttributeValue("id", componentId); - rootElement.SetAttributeValue("key", componentId); rootElement.SetAttributeValue("hydro-name", GetType().Name); rootElement.SetAttributeValue("x-data", "hydro"); + + if (!string.IsNullOrWhiteSpace(Key)) + { + rootElement.SetAttributeValue("key", Key); + } + var hydroAttribute = rootElement.SetAttributeValue("hydro", null); hydroAttribute.QuoteType = AttributeValueQuote.WithoutValue; @@ -1073,4 +1079,4 @@ internal void AddClientScript(string script) => private static string Hash(string input) => $"W{Convert.ToHexString(MD5.HashData(Encoding.ASCII.GetBytes(input)))}"; -} \ No newline at end of file +} diff --git a/src/PropertyInjector.cs b/src/PropertyInjector.cs index 2fc4f05..f90be6d 100644 --- a/src/PropertyInjector.cs +++ b/src/PropertyInjector.cs @@ -30,8 +30,16 @@ private static IEnumerable GetPropertyInfos(Type type) return properties; } - var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.Public) - .Where(p => (p.DeclaringType != typeof(ViewComponent)) && p.GetSetMethod()?.IsPublic == true && !p.GetCustomAttributes().Any()) + var viewComponentType = typeof(ViewComponent); + var hydroComponentType = typeof(HydroComponent); + + var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) + .Where(p => (p.Name == "Key" && p.DeclaringType == hydroComponentType) + || (p.DeclaringType != viewComponentType + && p.GetGetMethod()?.IsPublic == true + && p.GetSetMethod()?.IsPublic == true + && !p.GetCustomAttributes().Any()) + ) .ToArray(); CachedPropertyInfos.TryAdd(type, propertyInfos); diff --git a/src/TagHelpers/HydroComponentTagHelper.cs b/src/TagHelpers/HydroComponentTagHelper.cs index 0d92f2f..3a7b7d8 100644 --- a/src/TagHelpers/HydroComponentTagHelper.cs +++ b/src/TagHelpers/HydroComponentTagHelper.cs @@ -94,4 +94,4 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu output.Content.SetHtmlContent(componentHtml); } -} +} \ No newline at end of file