11using Microsoft . AspNetCore . Components ;
22using Microsoft . AspNetCore . Components . Web ;
33using MudBlazor ;
4+ using MudBlazor . State ;
45using MudBlazor . Utilities ;
56
67namespace MudExtensions
78{
89 /// <summary>
9- ///
10+ /// Inputs which each input box can contain only one character.
1011 /// </summary>
1112 /// <typeparam name="T"></typeparam>
1213 public partial class MudCodeInput < T > : MudFormComponent < T , string >
1314 {
1415 /// <summary>
15- ///
16+ /// MudCodeInput constructor.
1617 /// </summary>
17- public MudCodeInput ( ) : base ( new DefaultConverter < T > ( ) ) { }
18+ public MudCodeInput ( ) : base ( new DefaultConverter < T > ( ) )
19+ {
20+ using var registerScope = CreateRegisterScope ( ) ;
21+ _theValue = registerScope . RegisterParameter < T ? > ( nameof ( Value ) )
22+ . WithParameter ( ( ) => Value )
23+ . WithEventCallback ( ( ) => ValueChanged )
24+ . WithChangeHandler ( OnValueChanged ) ;
25+ _count = registerScope . RegisterParameter < int > ( nameof ( Count ) )
26+ . WithParameter ( ( ) => Count )
27+ . WithChangeHandler ( OnCountChanged ) ;
28+ }
29+
30+ private readonly ParameterState < T ? > _theValue ;
31+ private readonly ParameterState < int > _count ;
32+
33+ private async Task OnValueChanged ( )
34+ {
35+ await SetValueFromOutside ( Value ) ;
36+ }
37+
38+ private void OnCountChanged ( )
39+ {
40+ if ( Count < 0 )
41+ {
42+ Count = 0 ;
43+ }
44+
45+ if ( 12 < Count )
46+ {
47+ Count = 12 ;
48+ }
49+ }
1850
1951 /// <summary>
2052 /// Protected classes.
@@ -30,7 +62,7 @@ public MudCodeInput() : base(new DefaultConverter<T>()) { }
3062 protected string ? InputClassname =>
3163 new CssBuilder ( "justify-text-center" )
3264 . AddClass ( "mud-code" , Variant != Variant . Text )
33- . AddClass ( ClassInput )
65+ . AddClass ( InputClass )
3466 . Build ( ) ;
3567
3668 private List < MudTextField < T > > _elementReferences = new ( ) ;
@@ -40,35 +72,21 @@ public MudCodeInput() : base(new DefaultConverter<T>()) { }
4072 /// </summary>
4173 [ Parameter ]
4274 [ Category ( CategoryTypes . FormComponent . Behavior ) ]
43- public string ? ClassInput { get ; set ; }
75+ public string ? InputClass { get ; set ; }
4476
4577 /// <summary>
4678 /// Type of the input element. It should be a valid HTML5 input type.
4779 /// </summary>
4880 [ Parameter ]
4981 [ Category ( CategoryTypes . FormComponent . Behavior ) ]
5082 public InputType InputType { get ; set ; } = InputType . Text ;
51-
52- private T ? _theValue ;
5383
5484 /// <summary>
5585 /// The value of the input.
5686 /// </summary>
5787 [ Parameter ]
5888 [ Category ( CategoryTypes . FormComponent . Behavior ) ]
59- public T ? Value
60- {
61- get => _theValue ;
62- set
63- {
64- if ( Converter . Set ( _theValue ) == Converter . Set ( value ) )
65- {
66- return ;
67- }
68- _theValue = value ;
69- SetValueFromOutside ( _theValue ) . CatchAndLog ( ) ;
70- }
71- }
89+ public T ? Value { get ; set ; }
7290
7391 /// <summary>
7492 /// The event fires when value changed.
@@ -77,32 +95,12 @@ public T? Value
7795 [ Category ( CategoryTypes . FormComponent . Behavior ) ]
7896 public EventCallback < T ? > ValueChanged { get ; set ; }
7997
80- private int _count ;
8198 /// <summary>
8299 /// The number of text fields.
83100 /// </summary>
84101 [ Parameter ]
85102 [ Category ( CategoryTypes . FormComponent . Behavior ) ]
86- public int Count
87- {
88- get => _count ;
89- set
90- {
91- if ( value == _count || value < 0 )
92- {
93- return ;
94- }
95-
96- if ( 12 < value )
97- {
98- _count = 12 ;
99- }
100- else
101- {
102- _count = value ;
103- }
104- }
105- }
103+ public int Count { get ; set ; }
106104
107105 /// <summary>
108106 /// Determines the spacing between each input.
@@ -245,12 +243,11 @@ public async Task SetValue()
245243 result += val ;
246244 }
247245
248- Value = Converter . Get ( result ) ;
249- await ValueChanged . InvokeAsync ( Value ) ;
246+ await _theValue . SetValueAsync ( Converter . Get ( result ) ) ;
250247 }
251248
252249 /// <summary>
253- ///
250+ /// Call this method to set value programmatically.
254251 /// </summary>
255252 /// <param name="value"></param>
256253 /// <returns></returns>
@@ -261,7 +258,7 @@ public async Task SetValueFromOutside(T? value)
261258 {
262259 val = val . Substring ( 0 , Count ) ;
263260 }
264- Value = Converter . Get ( val ) ;
261+ await _theValue . SetValueAsync ( Converter . Get ( val ) ) ;
265262 for ( int i = 0 ; i < Count ; i ++ )
266263 {
267264 if ( i < val ? . Length )
@@ -273,9 +270,6 @@ public async Task SetValueFromOutside(T? value)
273270 await _elementReferences [ i ] . SetText ( null ) ;
274271 }
275272 }
276-
277- await ValueChanged . InvokeAsync ( Value ) ;
278- StateHasChanged ( ) ;
279273 }
280274
281275 }
0 commit comments