Skip to content

Commit

Permalink
Merge pull request #64 from hydrostack/28-add-key-attribute-only-on-d…
Browse files Browse the repository at this point in the history
…emand

Include key only if present. Include key in the serialized model.
  • Loading branch information
kjeske authored Aug 28, 2024
2 parents 4bf54c6 + efe804e commit 578de11
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
20 changes: 13 additions & 7 deletions src/HydroComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public abstract class HydroComponent : ViewComponent
/// <summary>
/// Provides component's key value
/// </summary>
[JsonProperty]
protected string Key { get; private set; }

/// <summary>
Expand Down Expand Up @@ -130,8 +131,8 @@ private void ConfigurePolls()
/// <summary>
/// Implementation of ViewComponent's InvokeAsync method
/// </summary>
/// <param name="parameters">Parameters</param>
/// <param name="key">Key</param>
/// <param name="parameters">An object with component parameters</param>
/// <param name="key">Local identifier to distinguish components of same type</param>
public async Task<IHtmlContent> InvokeAsync(object parameters = null, string key = null)
{
ApplyParameters(parameters);
Expand Down Expand Up @@ -424,7 +425,7 @@ private async Task<string> RenderOnlineNestedComponent(IPersistentState persiste

if (IsComponentIdRendered(componentId))
{
return GetComponentPlaceholderTemplate(componentId);
return GetComponentPlaceholderTemplate(componentId, Key);
}

if (!await AuthorizeAsync())
Expand All @@ -438,8 +439,8 @@ private async Task<string> RenderOnlineNestedComponent(IPersistentState persiste
return await GenerateComponentHtml(componentId, persistentState, includeScripts: true);
}

private static string GetComponentPlaceholderTemplate(string componentId) =>
$"<div id=\"{componentId}\" key=\"{componentId}\" hydro hydro-placeholder></div>";
private static string GetComponentPlaceholderTemplate(string componentId, string key) =>
$"<div id=\"{componentId}\" {(!string.IsNullOrWhiteSpace(key) ? $"key=\"{key}\"" : "")} hydro hydro-placeholder></div>";

private async Task<string> RenderStaticComponent(IPersistentState persistentState)
{
Expand Down Expand Up @@ -487,9 +488,14 @@ private async Task<string> 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;

Expand Down Expand Up @@ -1073,4 +1079,4 @@ internal void AddClientScript(string script) =>

private static string Hash(string input) =>
$"W{Convert.ToHexString(MD5.HashData(Encoding.ASCII.GetBytes(input)))}";
}
}
12 changes: 10 additions & 2 deletions src/PropertyInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ private static IEnumerable<PropertyInfo> 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<TransientAttribute>().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<TransientAttribute>().Any())
)
.ToArray();

CachedPropertyInfos.TryAdd(type, propertyInfos);
Expand Down
2 changes: 1 addition & 1 deletion src/TagHelpers/HydroComponentTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu

output.Content.SetHtmlContent(componentHtml);
}
}
}

0 comments on commit 578de11

Please sign in to comment.