6
6
7
7
namespace Microsoft . AspNetCore . Components . Forms . Mapping ;
8
8
9
+ // Provides values for [SupplyParameterFromForm] parameters on components.
10
+ // It is used in two ways:
11
+ // - By default, an instance is registered in DI, supplying values outside any FormMappingScope
12
+ // - If there is a FormMappingScope, internally it creates an instance of this to implement ICascadingValueSupplier for it
9
13
internal class SupplyParameterFromFormValueProvider : ICascadingValueSupplier
10
14
{
11
- private readonly FormMappingContext _mappingContext ;
12
15
private readonly IFormValueMapper ? _formValueMapper ;
16
+ private readonly FormMappingContext _mappingContext ;
13
17
14
18
public FormMappingContext MappingContext => _mappingContext ;
15
19
16
- public SupplyParameterFromFormValueProvider ( IFormValueMapper ? formValueMapper , NavigationManager navigation , FormMappingContext ? parentContext , string thisName )
20
+ public SupplyParameterFromFormValueProvider ( IFormValueMapper ? formValueMapper , string mappingScopeName )
17
21
{
18
- ArgumentNullException . ThrowIfNull ( navigation ) ;
19
-
20
22
_formValueMapper = formValueMapper ;
21
- Name = thisName ;
22
-
23
- // MappingContextId: action parameter used to define the handler
24
- // Name: form name and context used to map
25
- // Cases:
26
- // 1) No name ("")
27
- // Name = "";
28
- // MappingContextId = "";
29
- // <form name="" action="" />
30
- // 2) Name provided
31
- // Name = "my-handler";
32
- // MappingContextId = <<base-relative-uri>>((<<existing-query>>&)|?)handler=my-handler
33
- // <form name="my-handler" action="relative/path?existing=value&handler=my-handler
34
- // 3) Parent has a name "parent-name"
35
- // Name = "parent-name.my-handler";
36
- // MappingContextId = <<base-relative-uri>>((<<existing-query>>&)|?)handler=my-handler
37
- var name = FormMappingContext . Combine ( parentContext , thisName ) ;
38
- var mappingId = string . IsNullOrEmpty ( name ) ? "" : GenerateMappingContextId ( name ) ;
39
- _mappingContext = new FormMappingContext ( name , mappingId ) ;
40
-
41
- string GenerateMappingContextId ( string name )
42
- {
43
- var mappingId = navigation . ToBaseRelativePath ( navigation . GetUriWithQueryParameter ( "handler" , name ) ) ;
44
- var hashIndex = mappingId . IndexOf ( '#' ) ;
45
- return hashIndex == - 1 ? mappingId : new string ( mappingId . AsSpan ( 0 , hashIndex ) ) ;
46
- }
23
+ _mappingContext = new FormMappingContext ( mappingScopeName ) ;
24
+
25
+ MappingScopeName = mappingScopeName ;
47
26
}
48
27
49
- public string Name { get ; }
28
+ public string MappingScopeName { get ; }
50
29
51
30
bool ICascadingValueSupplier . IsFixed => true ;
52
31
@@ -59,10 +38,10 @@ public bool CanSupplyValue(in CascadingParameterInfo parameterInfo)
59
38
}
60
39
61
40
// We also supply values for [SupplyValueFromForm]
62
- if ( _formValueMapper is not null && parameterInfo . Attribute is SupplyParameterFromFormAttribute )
41
+ if ( _formValueMapper is not null && parameterInfo . Attribute is SupplyParameterFromFormAttribute supplyParameterFromFormAttribute )
63
42
{
64
- var ( formName , valueType ) = GetFormNameAndValueType ( _mappingContext , parameterInfo ) ;
65
- return _formValueMapper . CanMap ( valueType , formName ) ;
43
+ var combinedFormName = _mappingContext . GetCombinedFormName ( supplyParameterFromFormAttribute . Handler ) ;
44
+ return _formValueMapper . CanMap ( parameterInfo . PropertyType , combinedFormName ) ;
66
45
}
67
46
68
47
return false ;
@@ -77,9 +56,9 @@ public bool CanSupplyValue(in CascadingParameterInfo parameterInfo)
77
56
}
78
57
79
58
// We also supply values for [SupplyValueFromForm]
80
- if ( _formValueMapper is { } valueMapper && parameterInfo . Attribute is SupplyParameterFromFormAttribute )
59
+ if ( _formValueMapper is { } valueMapper && parameterInfo . Attribute is SupplyParameterFromFormAttribute supplyParameterFromFormAttribute )
81
60
{
82
- return GetFormPostValue ( valueMapper , _mappingContext , parameterInfo ) ;
61
+ return GetFormPostValue ( valueMapper , _mappingContext , parameterInfo , supplyParameterFromFormAttribute ) ;
83
62
}
84
63
85
64
throw new InvalidOperationException ( $ "Received an unexpected attribute type { parameterInfo . Attribute . GetType ( ) } ") ;
@@ -91,18 +70,18 @@ void ICascadingValueSupplier.Subscribe(ComponentState subscriber, in CascadingPa
91
70
void ICascadingValueSupplier . Unsubscribe ( ComponentState subscriber , in CascadingParameterInfo parameterInfo )
92
71
=> throw new NotSupportedException ( ) ; // IsFixed = true, so the framework won't call this
93
72
94
- internal static object ? GetFormPostValue ( IFormValueMapper formValueMapper , FormMappingContext ? mappingContext , in CascadingParameterInfo parameterInfo )
73
+ internal static object ? GetFormPostValue ( IFormValueMapper formValueMapper , FormMappingContext ? mappingContext , in CascadingParameterInfo parameterInfo , SupplyParameterFromFormAttribute supplyParameterFromFormAttribute )
95
74
{
96
75
Debug . Assert ( mappingContext != null ) ;
97
- var ( formName , valueType ) = GetFormNameAndValueType ( mappingContext , parameterInfo ) ;
76
+ var combinedFormName = mappingContext . GetCombinedFormName ( supplyParameterFromFormAttribute . Handler ) ;
98
77
99
78
var parameterName = parameterInfo . Attribute . Name ?? parameterInfo . PropertyName ;
100
79
var handler = ( ( SupplyParameterFromFormAttribute ) parameterInfo . Attribute ) . Handler ;
101
80
Action < string , FormattableString , string ? > errorHandler = string . IsNullOrEmpty ( handler ) ?
102
81
mappingContext . AddError :
103
- ( name , message , value ) => mappingContext . AddError ( formName , parameterName , message , value ) ;
82
+ ( name , message , value ) => mappingContext . AddError ( combinedFormName , parameterName , message , value ) ;
104
83
105
- var context = new FormValueMappingContext ( formName ! , valueType , parameterName )
84
+ var context = new FormValueMappingContext ( combinedFormName , parameterInfo . PropertyType , parameterName )
106
85
{
107
86
OnError = errorHandler ,
108
87
MapErrorToContainer = mappingContext . AttachParentValue
@@ -112,15 +91,4 @@ void ICascadingValueSupplier.Unsubscribe(ComponentState subscriber, in Cascading
112
91
113
92
return context . Result ;
114
93
}
115
-
116
- private static ( string FormName , Type ValueType ) GetFormNameAndValueType ( FormMappingContext ? mappingContext , in CascadingParameterInfo parameterInfo )
117
- {
118
- var valueType = parameterInfo . PropertyType ;
119
- var valueName = ( ( SupplyParameterFromFormAttribute ) parameterInfo . Attribute ) . Handler ;
120
- var formName = string . IsNullOrEmpty ( valueName ) ?
121
- ( mappingContext ? . Name ) :
122
- FormMappingContext . Combine ( mappingContext , valueName ) ;
123
-
124
- return ( formName ! , valueType ) ;
125
- }
126
94
}
0 commit comments