forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicInputBase.cs
More file actions
84 lines (75 loc) · 2.63 KB
/
DynamicInputBase.cs
File metadata and controls
84 lines (75 loc) · 2.63 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
using System.Collections.Generic;
using Microsoft.AspNetCore.Components;
namespace ServiceStack.Blazor.Components;
public class DynamicInputBase : TextInputBase
{
[Parameter, EditorRequired]
public Dictionary<string, object> Model { get; set; } = new();
protected string Value
{
get => Model.TryGetValue(Input!.Id, out var value) ? value?.ToString() ?? "" : "";
set => Model[Input!.Id] = value ?? "";
}
[Parameter, EditorRequired]
public InputInfo? Input { get; set; }
[Parameter]
public override string? Id
{
get => Input?.Id;
set => Input!.Id = value;
}
protected string UseId => Input!.Id!;
protected override string UseType => Input!.Type ?? base.UseType;
protected override string UsePlaceholder => Input!.Placeholder ?? base.UsePlaceholder;
protected override string UseLabel => Input!.Label ?? base.UseLabel;
protected override string UseHelp => Input!.Help ?? base.UseHelp;
protected List<KeyValuePair<string, string>> KvpValues() => TextUtils.ToKeyValuePairs(Model);
protected Dictionary<string, object>? AllAttributes
{
get
{
var input = Input!;
var to = new Dictionary<string, object>()
{
["id"] = input.Id,
["name"] = input.Name,
["type"] = input.Type,
["placeholder"] = input.Placeholder,
["pattern"] = input.Pattern,
["readonly"] = input.ReadOnly ?? false,
["required"] = input.Required ?? false,
["min"] = input.Min,
["max"] = input.Max,
["step"] = input.Step ?? 0,
["minlength"] = input.MinLength ?? 0,
["maxlength"] = input.MaxLength ?? 0,
};
if (input.Meta != null)
{
foreach (var entry in input.Meta)
{
to[entry.Key] = entry.Value;
}
}
foreach (var key in to.Keys.ToArray())
{
var val = to[key];
if ((val is null)
|| (val is bool b && !b)
|| (val is int i && i == 0)
|| (val is string s && (s.StartsWith("on") || SanitizeAttribute(s))))
{
to.Remove(key);
}
}
if (AdditionalAttributes != null)
{
foreach (var entry in AdditionalAttributes)
{
to[entry.Key] = entry.Value;
}
}
return to;
}
}
}