|
| 1 | +using System; |
| 2 | +using System.Text.RegularExpressions; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Microsoft.AspNetCore.Components; |
| 5 | +using Microsoft.AspNetCore.Components.Web; |
| 6 | +using MudBlazor; |
| 7 | +using MudBlazor.Utilities; |
| 8 | +using MudExtensions.Extensions; |
| 9 | + |
| 10 | +namespace MudExtensions |
| 11 | +{ |
| 12 | + public partial class MudCodeInput<T> : MudFormComponent<T, string> |
| 13 | + { |
| 14 | + public MudCodeInput() : base(new DefaultConverter<T>()) { } |
| 15 | + |
| 16 | + protected string Classname => |
| 17 | + new CssBuilder($"d-flex gap-{Spacing}") |
| 18 | + .AddClass(Class) |
| 19 | + .Build(); |
| 20 | + |
| 21 | + protected string InputClassname => |
| 22 | + new CssBuilder("justify-text-center") |
| 23 | + .AddClass("mud-code", Variant != Variant.Text) |
| 24 | + .AddClass(ClassInput) |
| 25 | + .Build(); |
| 26 | + |
| 27 | + private List<MudTextField<T>> _elementReferences = new(); |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// The CSS classes for each input, seperated by space. |
| 31 | + /// </summary> |
| 32 | + [Parameter] |
| 33 | + [Category(CategoryTypes.FormComponent.Behavior)] |
| 34 | + public string ClassInput { get; set; } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Type of the input element. It should be a valid HTML5 input type. |
| 38 | + /// </summary> |
| 39 | + [Parameter] |
| 40 | + [Category(CategoryTypes.FormComponent.Behavior)] |
| 41 | + public InputType InputType { get; set; } = InputType.Text; |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// The value of the input. |
| 45 | + /// </summary> |
| 46 | + private T _theValue; |
| 47 | + [Parameter] |
| 48 | + [Category(CategoryTypes.FormComponent.Behavior)] |
| 49 | + public T Value |
| 50 | + { |
| 51 | + get => _theValue; |
| 52 | + set |
| 53 | + { |
| 54 | + if (Converter.Set(_theValue) == Converter.Set(value)) |
| 55 | + { |
| 56 | + return; |
| 57 | + } |
| 58 | + _theValue = value; |
| 59 | + SetValueFromOutside(_theValue).AndForgetExt(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// The event fires when value changed. |
| 65 | + /// </summary> |
| 66 | + [Parameter] |
| 67 | + [Category(CategoryTypes.FormComponent.Behavior)] |
| 68 | + public EventCallback<T> ValueChanged { get; set; } |
| 69 | + |
| 70 | + private int _count; |
| 71 | + /// <summary> |
| 72 | + /// The number of text fields. |
| 73 | + /// </summary> |
| 74 | + [Parameter] |
| 75 | + [Category(CategoryTypes.FormComponent.Behavior)] |
| 76 | + public int Count |
| 77 | + { |
| 78 | + get => _count; |
| 79 | + set |
| 80 | + { |
| 81 | + if (value == _count || value < 0) |
| 82 | + { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + if (12 < value) |
| 87 | + { |
| 88 | + _count = 12; |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + _count = value; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Determines the spacing between each input. |
| 99 | + /// </summary> |
| 100 | + [Parameter] |
| 101 | + [Category(CategoryTypes.FormComponent.Behavior)] |
| 102 | + public int Spacing { get; set; } = 2; |
| 103 | + |
| 104 | + [Parameter] |
| 105 | + [Category(CategoryTypes.FormComponent.Behavior)] |
| 106 | + public bool Disabled { get; set; } |
| 107 | + |
| 108 | + [Parameter] |
| 109 | + [Category(CategoryTypes.FormComponent.Behavior)] |
| 110 | + public bool ReadOnly { get; set; } |
| 111 | + |
| 112 | + [Parameter] |
| 113 | + [Category(CategoryTypes.FormComponent.Behavior)] |
| 114 | + public Variant Variant { get; set; } = Variant.Text; |
| 115 | + |
| 116 | + [Parameter] |
| 117 | + [Category(CategoryTypes.FormComponent.Behavior)] |
| 118 | + public Margin Margin { get; set; } |
| 119 | + |
| 120 | + protected async Task HandleKeyDown(KeyboardEventArgs arg) |
| 121 | + { |
| 122 | + if (Disabled || ReadOnly) |
| 123 | + { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + if (RuntimeLocation.IsClientSide) |
| 128 | + { |
| 129 | + await Task.Delay(10); |
| 130 | + } |
| 131 | + |
| 132 | + if (arg.Key == "Backspace" || arg.Key == "ArrowLeft") |
| 133 | + { |
| 134 | + await FocusPrevious(); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + if (arg.Key.Length == 1 || arg.Key == "ArrowRight") |
| 139 | + { |
| 140 | + await FocusNext(); |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + private int _lastFocusedIndex = 0; |
| 146 | + protected void CheckFocus(int count) |
| 147 | + { |
| 148 | + _lastFocusedIndex = count; |
| 149 | + } |
| 150 | + |
| 151 | + public async Task FocusNext() |
| 152 | + { |
| 153 | + if (_lastFocusedIndex >= Count - 1) |
| 154 | + { |
| 155 | + await _elementReferences[_lastFocusedIndex].BlurAsync(); |
| 156 | + await _elementReferences[_lastFocusedIndex].FocusAsync(); |
| 157 | + return; |
| 158 | + } |
| 159 | + await _elementReferences[_lastFocusedIndex + 1].FocusAsync(); |
| 160 | + } |
| 161 | + |
| 162 | + public async Task FocusPrevious() |
| 163 | + { |
| 164 | + if (_lastFocusedIndex == 0) |
| 165 | + { |
| 166 | + await _elementReferences[_lastFocusedIndex].BlurAsync(); |
| 167 | + await _elementReferences[_lastFocusedIndex].FocusAsync(); |
| 168 | + return; |
| 169 | + } |
| 170 | + await _elementReferences[_lastFocusedIndex - 1].FocusAsync(); |
| 171 | + } |
| 172 | + |
| 173 | + protected override void OnInitialized() |
| 174 | + { |
| 175 | + base.OnInitialized(); |
| 176 | + SyncReferences(); |
| 177 | + } |
| 178 | + |
| 179 | + private void SyncReferences() |
| 180 | + { |
| 181 | + _elementReferences.Clear(); |
| 182 | + for (int i = 0; i < 12; i++) |
| 183 | + { |
| 184 | + _elementReferences.Add(new MudTextField<T>()); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + public async Task SetValue() |
| 189 | + { |
| 190 | + string result = ""; |
| 191 | + for (int i = 0; i < Count; i++) |
| 192 | + { |
| 193 | + var val = _elementReferences[i].Value?.ToString(); |
| 194 | + if (val == null) |
| 195 | + { |
| 196 | + continue; |
| 197 | + } |
| 198 | + |
| 199 | + result += val; |
| 200 | + } |
| 201 | + |
| 202 | + Value = Converter.Get(result); |
| 203 | + await ValueChanged.InvokeAsync(Value); |
| 204 | + } |
| 205 | + |
| 206 | + public async Task SetValueFromOutside(T value) |
| 207 | + { |
| 208 | + string val = Converter.Set(value); |
| 209 | + if (Count < val.Length) |
| 210 | + { |
| 211 | + val = val.Substring(0, Count); |
| 212 | + } |
| 213 | + Value = Converter.Get(val); |
| 214 | + for (int i = 0; i < Count; i++) |
| 215 | + { |
| 216 | + if (i < val.Length) |
| 217 | + { |
| 218 | + await _elementReferences[i].SetText(val[i].ToString()); |
| 219 | + } |
| 220 | + else |
| 221 | + { |
| 222 | + await _elementReferences[i].SetText(null); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + await ValueChanged.InvokeAsync(Value); |
| 227 | + StateHasChanged(); |
| 228 | + } |
| 229 | + |
| 230 | + } |
| 231 | +} |
0 commit comments