ant-design-blazor/components/core/Base/AntDomComponentBase.cs
Vladislav Richter 059491d749 refactor: replace IdGeneratorHelper with DI service that can be replaced by user (#664)
* Replaced IdGeneratorHelper with DI service that can be replaced by user

* removed license comments and unnecessary usings

* check Id null before assigning autogenerated id

* fix: unit test

Co-authored-by: ElderJames <shunjiey@hotmail.com>
2020-10-10 15:31:32 +08:00

82 lines
1.9 KiB
C#

using Microsoft.AspNetCore.Components;
namespace AntDesign
{
public abstract class AntDomComponentBase : AntComponentBase
{
[Inject]
private IComponentIdGenerator ComponentIdGenerator { get; set; }
[Parameter]
public string Id { get; set; }
//[Parameter(CaptureUnmatchedValues = true)]
//public Dictionary<string, object> Attributes { get; set; } = new Dictionary<string, object>();
private ElementReference _ref;
/// <summary>
/// Returned ElementRef reference for DOM element.
/// </summary>
public virtual ElementReference Ref
{
get => _ref;
set
{
_ref = value;
RefBack?.Set(value);
}
}
protected ClassMapper ClassMapper { get; } = new ClassMapper();
protected AntDomComponentBase()
{
ClassMapper
.Get(() => this.Class);
}
protected override void OnInitialized()
{
Id ??= ComponentIdGenerator.Generate(this);
base.OnInitialized();
}
/// <summary>
/// Specifies one or more class names for an DOM element.
/// </summary>
[Parameter]
public string Class
{
get => _class;
set
{
_class = value;
ClassMapper.OriginalClass = value;
}
}
/// <summary>
/// Specifies an inline style for an DOM element.
/// </summary>
[Parameter]
public string Style
{
get => _style;
set
{
_style = value;
this.StateHasChanged();
}
}
protected virtual string GenerateStyle()
{
return Style;
}
private string _class;
private string _style;
}
}