TagHelperPack icon indicating copy to clipboard operation
TagHelperPack copied to clipboard

Adding [asp-class-if-*] TagHelper to conditionally add a class name based on a condition

Open bingzer opened this issue 1 year ago • 0 comments

Hello @DamianEdwards ,

I recently came across a situation where I needed to add a class based on a specific condition in my project. While searching, I noticed a similar feature request in Issue #31.

I've introduced the [asp-class-if-key] TagHelper with this syntax:

[asp-class-if-key]:

<div class="mt-0 p0" asp-class-if-bg-primary="(DateTime.UtcNow.Second % 2) == 0">
  This paragraph will have <code>bg-primary</code> class during <strong>even</strong> seconds.
</div>

which results in:

**** even second ****
<div class="mt-0 p-0 bg-primary">
  This paragraph will have <code>bg-primary</code> class during <strong>even</strong> seconds.
</div>

**** odd second ****
<div class="mt-0 p-0">
  This paragraph will have <code>bg-primary</code> class during <strong>even</strong> seconds.
</div>

Additionally, this PR supports various naming conventions, including camelCase, kebab-case, and underscore_case, as validated by the accompanying unit tests. I've also included unit tests and provided an example in the sample project. I believe that this feature could be valuable to other developers. Your review of this addition is greatly appreciated. Thank you for your consideration.

bingzer avatar Oct 17 '23 18:10 bingzer