feat(module: form): GenerateFormItem support recognize ReadOnlyAttribute to set component's disabled (#4191)

* GenerateFormItem自动生成表单时,识别TModel属性中的ReadOnlyAttribute标识,并使其生效

* fix indent

---------

Co-authored-by: James Yeung <shunjiey@hotmail.com>
This commit is contained in:
算神 2024-09-14 16:09:27 +08:00 committed by GitHub
parent 65ae78f904
commit 57ad7125fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 12 deletions

View File

@ -295,6 +295,8 @@ public partial class GenerateFormItem<TModel> : ComponentBase
var funcType = typeof(Func<>);
var constructedFuncType = funcType.MakeGenericType(property.PropertyType);
var isReadOnly = property.GetCustomAttribute<ReadOnlyAttribute>()?.IsReadOnly ?? false;
return builder =>
{
builder.OpenComponent(0, constructedType);
@ -303,6 +305,7 @@ public partial class GenerateFormItem<TModel> : ComponentBase
new Action<object>(o => property.SetValue(model, o)));
builder.AddAttribute(2, "ValueChanged", eventCallback);
builder.AddAttribute(3, "ValueExpression", Expression.Lambda(constructedFuncType, exp));
if (isReadOnly) builder.AddAttribute(4, "Disabled", isReadOnly);
builder.CloseComponent();
};
}

View File

@ -7,11 +7,11 @@
OnFinishFailed="OnFinishFailed"
LabelColSpan="8"
WrapperColSpan="16">
<GenerateFormItem TModel="Model" Definitions="Definitions" ValidateRules="ValidateRules" NotGenerate="NotGenerate"/>
<GenerateFormItem TModel="Model" Definitions="Definitions" ValidateRules="ValidateRules" NotGenerate="NotGenerate" />
<FormItem Label="ListTestOutOfGenerate">
<Select @bind-Values="@context.I" Style="width: 120px;" TItemValue="string" TItem="string">
<SelectOptions>
<SelectOption Value="@("lucy")" Label="Lucy"/>
<SelectOption Value="@("lucy")" Label="Lucy" />
</SelectOptions>
</Select>
</FormItem>
@ -52,7 +52,9 @@
[DisplayName("DefinitionsTest")] public string J { get; set; }
// [ValidateComplexType]
[DisplayName("ReadOnlyTest"), ReadOnly(true)]
public string K { get; set; }
public ModelChild ModelChild { get; set; }
}
@ -84,7 +86,7 @@
[Required] public IEnumerable<string> II { get; set; }
public string JJ { get; set; }
// [ValidateComplexType]
public ModelChild2 ModelChild2 { get; set; }
}
@ -127,18 +129,18 @@
if (structurePath == "J")
{
return @<Select @bind-Value="model.J" Style="width: 120px;" TItemValue="string" TItem="string">
<SelectOptions>
<SelectOption Value="@("lucy")" Label="Lucy"/>
</SelectOptions>
</Select>;
<SelectOptions>
<SelectOption Value="@("lucy")" Label="Lucy" />
</SelectOptions>
</Select>;
}
if (structurePath == "ModelChild.JJ")
{
return @<Select @bind-Value="model.ModelChild.JJ" Style="width: 120px;" TItemValue="string" TItem="string">
<SelectOptions>
<SelectOption Value="@("lucy")" Label="Lucy"/>
</SelectOptions>
</Select>;
<SelectOptions>
<SelectOption Value="@("lucy")" Label="Lucy" />
</SelectOptions>
</Select>;
}
return null;
}