Created
October 3, 2022 15:43
-
-
Save CuddleBunny/888d6c6bd8df8e02cf13546efe628042 to your computer and use it in GitHub Desktop.
Razor Component for the Feature tag helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@inject IFeatureManager featureManager | |
@if (Enabled) | |
{ | |
@ChildContent | |
} | |
@code { | |
// Reference: https://github.com/microsoft/FeatureManagement-Dotnet/blob/main/src/Microsoft.FeatureManagement.AspNetCore/TagHelpers/FeatureTagHelper.cs | |
/// <summary> | |
/// A feature name, or comma separated list of feature names, for which the content should be rendered. By default, all specified features must be enabled to render the content, but this requirement can be controlled by the <see cref="Requirement"/> property. | |
/// </summary> | |
[Parameter] | |
public string Name { get; set; } | |
// TODO: Implement to match FeatureTagHelper? https://github.com/microsoft/FeatureManagement-Dotnet/blob/main/src/Microsoft.FeatureManagement.AspNetCore/TagHelpers/FeatureTagHelper.cs | |
/// <summary> | |
/// Controls whether 'All' or 'Any' feature in a list of features should be enabled to render the content within the feature tag. | |
/// </summary> | |
//[Parameter] | |
//public RequirementType Requirement { get; set; } = RequirementType.All; | |
/// <summary> | |
/// Negates the evaluation for whether or not a feature tag should display content. This is used to display alternate content when a feature or set of features are disabled. | |
/// </summary> | |
[Parameter] | |
public bool Negate { get; set; } | |
[Parameter] | |
public RenderFragment ChildContent { get; set; } | |
public bool Enabled { get; set; } = false; | |
public override async Task SetParametersAsync(ParameterView parameters) | |
{ | |
await base.SetParametersAsync(parameters); | |
await UpdateEnabledStatus(); | |
} | |
protected override async Task OnParametersSetAsync() | |
{ | |
await base.OnParametersSetAsync(); | |
await UpdateEnabledStatus(); | |
} | |
private async Task UpdateEnabledStatus() | |
{ | |
var enabled = false; | |
if (!string.IsNullOrEmpty(Name)) | |
{ | |
enabled = await featureManager.IsEnabledAsync(Name); | |
} | |
if (Negate) | |
enabled = !enabled; | |
Enabled = enabled; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment