# ComponentBuilder
**Repository Path**: AchievedOwner/ComponentBuilder
## Basic Information
- **Project Name**: ComponentBuilder
- **Description**: A framework can easily help you to create blazor component from code behind.
- **Primary Language**: C#
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 0
- **Created**: 2021-12-23
- **Last Updated**: 2024-01-27
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# ComponentBuilder
轻松创建具有自动化功能的Blazor组件库,并同时支持 razor文件和 RenderTreeBuilder 两种方式。
[English](README.md) | [快速上手](./docs/readme.zh-CN.md) | [在线文档](https://playermaker.gitbook.io/componentbuilder/chinese/jian-jie)



## :sparkles: 特性
* 属性优先,从参数中优雅地定义 CSS
* 轻松通过属性与组件关联
* 可自定义CSS和组件的属性编码逻辑
* 同时支持 razor文件 或 `RenderTreeBuilder` 来创建组件
* 支持具有类似参数的组件的“预定义”
* 动态且简单地和 JS 进行交互
* 拦截器设计,为组件的生命周期赋予新的功能
* 渲染器管道模式,以识别动态渲染组件
* 丰富地 'RenderTreeBuilder' 扩展方法,代码片段轻松编写
* 用 Fluent API 轻松创建元素和组件
## :rainbow: 组件定义
* 在 `Button.razor`
```html
@inherits BlazorComponentBase
@code{
[CssClass("btn")]
public Button()
{
}
[Parameter][CssClass("active")]public bool Active { get; set; }
[Parameter][CssClass("btn-")]public Color? Color { get; set; }
[Parameter]public RenderFragment? ChildContent { get; set; }
[Parameter][HtmlData("tooltip")]public string? Tooltip { get; set; }
[Parameter][HtmlEvent("onclick")]public EventCallback OnClick { get; set; }
[Parameter][HtmlAttribute]public string? Title { get; set; }
public enum Color
{
Primary,
Secondary,
[CssClass("info")]Information,
}
}
```
* 在 `Button.cs` 类
```csharp
[HtmlTag("button")]
[CssClass("btn")]
public class Button : BlazorComponentBase, IHasChildContent, IHasOnClick
{
[Parameter][CssClass("active")]public bool Active { get; set; }
[Parameter][CssClass("btn-")]public Color? Color { get; set; }
[Parameter]public RenderFragment? ChildContent { get; set; }
[Parameter][HtmlData("tooltip")]public string? Tooltip { get; set; }
[Parameter][HtmlEvent("onclick")]public EventCallback OnClick { get; set;
[Parameter][HtmlAttribute]public string? Title { get; set; }
}
public enum Color
{
Primary,
Secondary,
[CssClass("info")]Information,
}
```
* 使用和对比
```html
```
## :key: C# 和 Javascript 交互
* 导入模块
```js
//in app.js
export function display(){
// ...your code
}
```
```csharp
[Inject]IJSRuntime JS { get; set; }
var js = await JS.Value.ImportAsync("./app.js");
js.display(); // same as function name
```
* 动态运行 JS 代码
```csharp
JS.Value.EvaluateAsync(window => {
window.console.log("log")
});
JS.Value.EvaludateAsync(@"
console.log(\"log\");
")
```
* 快速使用 JS 回调 C# 代码
```csharp
JS.Value.InvokeVoidAsync("myFunction", CallbackFactory.Create(arg=> {
//get arg from js
}));
JS.Value.InvokeVoidAsync("calculate", CallbackFactory.Create((arg1,arg2)=> {
//get value of arg1,arg2 from js
}))
```
```js
function myFunction(dotNetRef){
dotNetRef.invokeMethodAsync("Invoke", "arg");
}
function calculate(dotNetRef){
dotNetRef.invokeMethodAsync("Invoke", 1, 2);
}
```
## :information_source: 个性化 CSS/Style/Attributes
* Logical CSS
```csharp
protected override void BuildCssClass(ICssClassBuilder builder)
{
if(builder.Contains("annotation-enter"))
{
builder.Remove("annotation-exist");
}
else
{
builder.Append("annotation-enter").Append("annotation-exist");
}
}
```
* Logical Attributes
```csharp
protected override void BuildAttributes(IDictionary attributes)
{
attributes["onclick"] = HtmlHelper.Event.Create(this, ()=>{ ... });
if(attrbutes.ContainKey("data-toggle"))
{
attributes["data-toggle"] = "collapse";
}
}
```
## :palm_tree: Fluent API
```csharp
builder.Div()
.Class("my-class")
.Class("active", IsActive)
.Class("text-block", !string.IsNullOrEmpty(Name))
.Style($"font-size:{Size}px", Size.HasValue)
.Content("hello world")
.Close();
builder.Component