forked from R0landas/intl-tel-input-blazor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MudIntlTelInput.razor.cs
215 lines (174 loc) · 6.56 KB
/
MudIntlTelInput.razor.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
208
209
210
211
212
213
214
215
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;
using MudBlazor;
using MudBlazor.Utilities;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
namespace IntlTelInputBlazor
{
public partial class MudIntlTelInput<T> : MudDebouncedInput<T>
{
protected string Classname =>
new CssBuilder("mud-input-input-control")
.AddClass(Class)
.AddClass("mud-input-iti")
.Build();
public MudInput<string> InputReference { get; private set; }
public MudIntlTelInput() : base()
{
//Validation = new Func<T, bool>(ValidateInput);
Converter = new()
{
SetFunc = value => value as string,
GetFunc = text => (T)(object)(IntlTel)text,
};
}
protected bool ValidateInput(T value)
{
return typeof(T) == typeof(IntlTel) && ((IntlTel)(object)value)?.IsValid != false;
}
/// <summary>
/// Type of the input element. It should be a valid HTML5 input type.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.Behavior)]
public InputType InputType { get; set; } = InputType.Telephone;
[CascadingParameter(Name = "SubscribeToParentForm")]
internal bool SubscribeToParentFormEx { get; set; } = true;
private string GetCounterText() => Counter == null ? string.Empty : (Counter == 0 ? (string.IsNullOrEmpty(Text) ? "0" : $"{Text.Length}") : ((string.IsNullOrEmpty(Text) ? "0" : $"{Text.Length}") + $" / {Counter}"));
/// <summary>
/// Show clear button.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.Behavior)]
public bool Clearable { get; set; } = false;
/// <summary>
/// Button click event for clear button. Called after text and value has been cleared.
/// </summary>
[Parameter] public EventCallback<MouseEventArgs> OnClearButtonClick { get; set; }
public override ValueTask FocusAsync()
{
return InputReference.FocusAsync();
}
public override ValueTask BlurAsync()
{
return InputReference.BlurAsync();
}
public override ValueTask SelectAsync()
{
return InputReference.SelectAsync();
}
public override ValueTask SelectRangeAsync(int pos1, int pos2)
{
return InputReference.SelectRangeAsync(pos1, pos2);
}
protected override async Task ResetValueAsync()
{
await InputReference.ResetAsync();
await base.ResetValueAsync();
}
/// <summary>
/// Clear the text field, set Value to default(T) and Text to null
/// </summary>
/// <returns></returns>
public Task Clear()
{
return InputReference.SetText(null);
}
/// <summary>
/// Sets the input text from outside programmatically
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public async Task SetText(string text)
{
if (InputReference != null)
await InputReference.SetText(text);
return;
}
protected override Task SetTextAsync(string text, bool updateValue = true)
{
return Task.CompletedTask;
}
[Parameter]
public bool AllowDropDown { get; set; } = true;
[Parameter]
public bool AutoHideDialCode { get; set; } = true;
[Parameter]
public string AutoPlaceholder { get; set; } = "polite";
[Parameter]
public string CustomContainer { get; set; }
[Parameter]
public IEnumerable<string> ExcludeCountries { get; set; } = Enumerable.Empty<string>();
[Parameter]
public bool FormatOnDisplay { get; set; } = true;
[Parameter]
public string InitialCountry { get; set; }
[Parameter]
public Dictionary<string, string> LocalizedCountries { get; set; }
[Parameter]
public bool NationalMode { get; set; } = true;
[Parameter]
public IEnumerable<string> OnlyCountries { get; set; } = Enumerable.Empty<string>();
[Parameter]
public string PlaceholderNumberType { get; set; } = "MOBILE";
[Parameter]
public IEnumerable<string> PreferredCountries { get; set; } = new[] { RegionInfo.CurrentRegion.TwoLetterISORegionName };
[Parameter]
public bool SeparateDialCode { get; set; }
[Parameter]
public string UtilsScript { get; set; } = "./_content/IntlTelInputBlazor/js/utils.js";
private int _inputIndex;
private DotNetObjectReference<MudIntlTelInput<IntlTel>> dotNetHelper;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
dotNetHelper = DotNetObjectReference.Create(this) as DotNetObjectReference<MudIntlTelInput<IntlTel>>;
_inputIndex = await _intlTelInputJsInterop.Init2(InputReference.ElementReference, dotNetHelper, new
{
AllowDropDown,
AutoHideDialCode,
AutoPlaceholder,
CustomContainer,
ExcludeCountries,
FormatOnDisplay,
InitialCountry,
LocalizedCountries,
NationalMode,
OnlyCountries,
PlaceholderNumberType,
PreferredCountries,
SeparateDialCode,
UtilsScript
});
if (Value is not null)
{
await _intlTelInputJsInterop.SetNumber(_inputIndex, Value.ToString());
}
}
}
[JSInvokable]
public async Task Update()
{
var value = (T)(object)await _intlTelInputJsInterop.GetData(_inputIndex);
if (value is not null)
{
await _intlTelInputJsInterop.SetNumber(_inputIndex, value.ToString());
}
await base.SetValueAsync(value, true);
}
private async Task OnInput(ChangeEventArgs e)
{
await Update();
}
protected override void Dispose(bool disposing)
{
dotNetHelper?.Dispose();
base.Dispose(disposing);
}
}
}