-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathDocsApiTable.razor
More file actions
145 lines (129 loc) · 5 KB
/
DocsApiTable.razor
File metadata and controls
145 lines (129 loc) · 5 KB
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
@namespace MudExtensions.Docs.Components
@using System.Text.RegularExpressions
@using System.Reflection
@inject HttpClient Http
<MudText Class="my-2" Color="Color.Secondary" Typo="Typo.h6">Parameters</MudText>
<MudTable Items="@(SafeProperties)" Elevation="0" Hover="true" Dense="true">
<HeaderContent>
<MudTh>Name</MudTh>
<MudTh>Type</MudTh>
<MudTh>Default</MudTh>
<MudTh>Description</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Name">@context.Name</MudTd>
<MudTd DataLabel="Type">@DocUtilities.GetFriendlyTypeName(context.PropertyType)</MudTd>
<MudTd DataLabel="Default">
@{
object? instance = TryCreateInstance(SafeComponentType);
string? value = instance != null ? context.GetValue(instance)?.ToString() : "null";
}
<MudText Typo="Typo.body2">@value</MudText>
</MudTd>
<MudTd DataLabel="Description">
@(_docReader?.GetSummary(context) ?? "—")
</MudTd>
</RowTemplate>
<NoRecordsContent>
No specific items.
</NoRecordsContent>
</MudTable>
<MudText Class="mt-8 mb-2" Color="Color.Secondary" Typo="Typo.h6">Events</MudText>
<MudTable Items="@(SafeEventProperties)" Elevation="0" Hover="true" Dense="true">
<HeaderContent>
<MudTh>Name</MudTh>
<MudTh>Type</MudTh>
<MudTh>Description</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Name">@context.Name</MudTd>
<MudTd DataLabel="Type">@DocUtilities.GetFriendlyTypeName(context.PropertyType)</MudTd>
<MudTd DataLabel="Description">@(_docReader?.GetSummary(context) ?? "—")</MudTd>
</RowTemplate>
<NoRecordsContent>
No specific items.
</NoRecordsContent>
</MudTable>
<MudText Class="mt-8 mb-2" Color="Color.Secondary" Typo="Typo.h6">Methods</MudText>
<MudTable Items="@(SafeMethods)" Elevation="0" Hover="true" Dense="true">
<HeaderContent>
<MudTh>Name</MudTh>
<MudTh>Return Type</MudTh>
<MudTh>Description</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Name">@context.Name</MudTd>
<MudTd DataLabel="Return Type">@DocUtilities.GetFriendlyTypeName(context.ReturnType)</MudTd>
<MudTd DataLabel="Description">@(_docReader?.GetSummary(context) ?? "—")</MudTd>
</RowTemplate>
<NoRecordsContent>
No specific items.
</NoRecordsContent>
</MudTable>
@if (Component?.RelatedComponents != null)
{
foreach (var comp in Component.RelatedComponents)
{
<ExampleCard Title="@($"Api - {comp.Name.Replace("`1", "")}")" ComponentName="@Type?.Name.Replace("Mud", "")" AliasName="api" ShowCodeSection="false">
<DocsApiTable Type="comp" />
</ExampleCard>
}
}
@code {
[Parameter] public Type? Type { get; set; }
[Parameter] public MudExtensionComponentInfo? Component { get; set; }
private SimpleXmlDocReader? _docReader;
private List<string> _excludedMethods = new()
{
"SetParametersAsync", "Equals", "GetHashCode", "GetType", "ToString"
};
private Type? SafeComponentType =>
(Type?.IsGenericTypeDefinition == true)
? Type?.MakeGenericType(Type.GetGenericArguments().Select(_ => typeof(string)).ToArray())
: Type;
private IEnumerable<PropertyInfo> SafeProperties =>
SafeComponentType?.GetProperties()
.Where(x => !x.PropertyType.Name.Contains("EventCallback") &&
x.Name != "FieldId" && x.Name != "UserAttributes")
.OrderBy(x => x.Name) ?? Enumerable.Empty<PropertyInfo>();
private IEnumerable<PropertyInfo> SafeEventProperties =>
SafeComponentType?.GetProperties()
.Where(x => x.PropertyType.Name.Contains("EventCallback"))
.OrderBy(x => x.Name) ?? Enumerable.Empty<PropertyInfo>();
private IEnumerable<MethodInfo> SafeMethods =>
SafeComponentType?.GetMethods()
.Where(x => !_excludedMethods.Contains(x.Name) &&
Regex.IsMatch(x.Name[0].ToString(), "[A-Z]"))
.OrderBy(x => x.Name) ?? Enumerable.Empty<MethodInfo>();
private object? TryCreateInstance(Type? type)
{
try
{
return (type?.ContainsGenericParameters == false)
? Activator.CreateInstance(type)
: null;
}
catch
{
return null;
}
}
protected override async Task OnInitializedAsync()
{
string? xmlContent = null;
if (OperatingSystem.IsBrowser())
{
xmlContent = await Http.GetStringAsync("CodeBeam.MudBlazor.Extensions.xml");
}
else
{
var xmlPath = Path.Combine(AppContext.BaseDirectory, "CodeBeam.MudBlazor.Extensions.xml");
if (File.Exists(xmlPath))
xmlContent = await File.ReadAllTextAsync(xmlPath);
}
if (!string.IsNullOrEmpty(xmlContent))
{
_docReader = new SimpleXmlDocReader(xmlContent);
}
}
}