forked from bitfoundation/bitplatform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitComponentBase.cs
207 lines (156 loc) · 6.3 KB
/
BitComponentBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
namespace Bit.BlazorUI;
public abstract partial class BitComponentBase : ComponentBase, IAsyncDisposable
{
private BitDir? _dir;
private string _uniqueId = BitShortId.NewId();
protected bool IsRendered;
protected bool IsDisposed;
internal string _Id => Id ?? _uniqueId;
/// <summary>
/// The readonly unique id of the root element. it will be assigned to a new Guid at component instance construction.
/// </summary>
public string UniqueId => _uniqueId;
/// <summary>
/// The ElementReference of the root element.
/// </summary>
public ElementReference RootElement { get; internal set; }
[CascadingParameter] protected BitDir? CascadingDir { get; set; }
/// <summary>
/// The aria-label of the control for the benefit of screen readers.
/// </summary>
[Parameter] public string? AriaLabel { get; set; }
/// <summary>
/// Custom CSS class for the root element of the component.
/// </summary>
[Parameter] public string? Class { get; set; }
/// <summary>
/// Determines the component direction.
/// </summary>
[Parameter]
public BitDir? Dir
{
get => _dir ?? CascadingDir;
set => _dir = value;
}
/// <summary>
/// Capture and render additional attributes in addition to the component's parameters.
/// </summary>
[Parameter] public Dictionary<string, object> HtmlAttributes { get; set; } = [];
/// <summary>
/// Custom id attribute for the root element. if null the UniqueId will be used instead.
/// </summary>
[Parameter] public string? Id { get; set; }
/// <summary>
/// Whether or not the component is enabled.
/// </summary>
[Parameter] public bool IsEnabled { get; set; } = true;
/// <summary>
/// Custom CSS style for the root element of the component.
/// </summary>
[Parameter] public string? Style { get; set; }
/// <summary>
/// Whether the component is visible, hidden or collapsed.
/// </summary>
[Parameter] public BitVisibility Visibility { get; set; }
internal protected Dictionary<string, object?>? ParametersCache { get; set; }
public override Task SetParametersAsync(ParameterView parameters)
{
HtmlAttributes.Clear();
var parametersDictionary = ParametersCache ?? throw new InvalidOperationException();
foreach (var parameter in parametersDictionary!)
{
switch (parameter.Key)
{
case nameof(CascadingDir):
var cascadingDir = (BitDir?)parameter.Value;
if (CascadingDir != cascadingDir) ClassBuilder.Reset();
CascadingDir = cascadingDir;
parametersDictionary.Remove(parameter.Key);
break;
case nameof(AriaLabel):
AriaLabel = (string?)parameter.Value;
parametersDictionary.Remove(parameter.Key);
break;
case nameof(Class):
var @class = (string?)parameter.Value;
if (Class != @class) ClassBuilder.Reset();
Class = @class;
parametersDictionary.Remove(parameter.Key);
break;
case nameof(Dir):
var dir = (BitDir?)parameter.Value;
if (Dir != dir) ClassBuilder.Reset();
Dir = dir;
parametersDictionary.Remove(parameter.Key);
break;
case nameof(Id):
Id = (string?)parameter.Value;
parametersDictionary.Remove(parameter.Key);
break;
case nameof(IsEnabled):
var isEnabled = (bool)parameter.Value;
if (IsEnabled != isEnabled) ClassBuilder.Reset();
IsEnabled = isEnabled;
parametersDictionary.Remove(parameter.Key);
break;
case nameof(Style):
var style = (string?)parameter.Value;
if (Style != style) StyleBuilder.Reset();
Style = style;
parametersDictionary.Remove(parameter.Key);
break;
case nameof(Visibility):
var visibility = (BitVisibility)parameter.Value;
if (Visibility != visibility) StyleBuilder.Reset();
Visibility = visibility;
parametersDictionary.Remove(parameter.Key);
break;
default:
HtmlAttributes.Add(parameter.Key, parameter.Value);
break;
}
}
ParametersCache = null;
return base.SetParametersAsync(ParameterView.Empty);
}
protected override void OnInitialized()
{
RegisterCssStyles();
StyleBuilder
.Register(() => Style)
.Register(() => Visibility switch
{
BitVisibility.Hidden => "visibility:hidden",
BitVisibility.Collapsed => "display:none",
_ => string.Empty
});
ClassBuilder
.Register(() => RootElementClass)
.Register(() => IsEnabled ? string.Empty : "bit-dis")
.Register(() => Dir == BitDir.Rtl ? "bit-rtl" : string.Empty);
RegisterCssClasses();
ClassBuilder.Register(() => Class);
base.OnInitialized();
}
protected override void OnAfterRender(bool firstRender)
{
IsRendered = true;
base.OnAfterRender(firstRender);
}
protected abstract string RootElementClass { get; }
protected ElementClassBuilder ClassBuilder { get; private set; } = new ElementClassBuilder();
protected ElementStyleBuilder StyleBuilder { get; private set; } = new ElementStyleBuilder();
protected virtual void RegisterCssStyles() { }
protected virtual void RegisterCssClasses() { }
protected virtual void OnVisibilityChanged(BitVisibility visibility) { }
public async ValueTask DisposeAsync()
{
await DisposeAsync(true);
GC.SuppressFinalize(this);
}
protected virtual ValueTask DisposeAsync(bool disposing)
{
IsDisposed = true;
return ValueTask.CompletedTask;
}
}