public static void AppendInput <T>(RenderTreeBuilder pBuilder, IMPropertyInfo pPropertyInfo, object pModel, Guid pId, IMForm pParent, bool pIsInFilterRow, IMField pField) { try { if (!IsTypeSupported(typeof(T)) || IsPropertyHolderNull(pPropertyInfo, pModel)) { ShowNotSupportedType(pBuilder, pPropertyInfo, pModel, pId, pParent); return; } T value = (T)(pPropertyInfo.GetValue(pModel) ?? default(T)); Type tType = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T); bool isReadOnly = pPropertyInfo.IsReadOnly || pPropertyInfo.GetCustomAttribute(typeof(ReadOnlyAttribute)) != null; if (mNumberTypes.Contains(tType)) { pBuilder.OpenComponent <InputNumber <T> >(0); } else if (tType == typeof(DateTime) || tType == typeof(DateTimeOffset)) { if (pPropertyInfo.GetCustomAttribute(typeof(TimeAttribute)) != null) { pBuilder.OpenComponent <InputTime <T> >(0); } else if (pPropertyInfo.GetCustomAttribute(typeof(DateTimeAttribute)) != null) { pBuilder.OpenComponent <InputDateTime <T> >(0); } else { pBuilder.OpenComponent <InputDate <T> >(0); } } else if (typeof(T) == typeof(bool)) { pBuilder.OpenComponent <MInputCheckbox>(0); } else if (typeof(T) == typeof(bool?)) { pBuilder.OpenComponent <MSelect <T> >(0); if (pIsInFilterRow) { pBuilder.AddAttribute(10, "NullValueDescription", "\u200b"); } } else if (tType == typeof(Guid)) { pBuilder.OpenComponent <InputGuid <T> >(0); } else if (tType.IsEnum) { pBuilder.OpenComponent <MSelect <T> >(0); if (pIsInFilterRow) { pBuilder.AddAttribute(10, "NullValueDescription", "\u200b"); } } else { if (pPropertyInfo.GetCustomAttribute(typeof(TextAreaAttribute)) != null) { pBuilder.OpenComponent <InputTextArea>(0); } else { pBuilder.OpenComponent <InputText>(0); } } if (pPropertyInfo.GetCustomAttribute(typeof(PasswordAttribute)) != null) { pBuilder.AddAttribute(33, "type", "password"); } if (pField.AdditionalAttributes != null) { pBuilder.AddMultipleAttributes(17, pField.AdditionalAttributes .Where(a => a.Key != Extensions.MFORM_IN_TABLE_ROW_TD_STYLE_ATTRIBUTE) .Where(a => a.Key != nameof(IMGridColumn)) .ToDictionary(a => a.Key, a => a.Value)); } pBuilder.AddAttribute(1, "id", pId); pBuilder.AddAttribute(2, "Value", value); pBuilder.AddAttribute(23, "ValueChanged", RuntimeHelpers.CreateInferredEventCallback <T>(pParent, async __value => { pPropertyInfo.SetValue(pModel, __value); await pParent.OnInputValueChanged(pField, pPropertyInfo, __value); }, value)); pBuilder.AddAttribute(23, "onkeyup", EventCallback.Factory.Create <KeyboardEventArgs>(pParent, (a) => { pParent.OnInputKeyUp(a); })); var valueExpression = GetValueExpression <T>(pPropertyInfo, pModel); pBuilder.AddAttribute(4, "ValueExpression", valueExpression); string cssClass = "m-form-control"; if (isReadOnly) { pBuilder.AddAttribute(33, "disabled", string.Empty); pBuilder.AddAttribute(33, "IsDisabled", true); } pBuilder.AddAttribute(10, "class", cssClass); if (typeof(T) == typeof(bool?)) { IEnumerable <bool?> options = new bool?[] { true, false }; pBuilder.AddAttribute(10, "Options", options); } pBuilder.CloseComponent(); if (pParent.EnableValidation) { pBuilder.OpenComponent <ValidationMessage <T> >(60); pBuilder.AddAttribute(61, "For", valueExpression); pBuilder.CloseComponent(); } } catch (Exception e) { Console.WriteLine(e.ToString()); throw; } }
public static void AppendInput <T>(RenderTreeBuilder pBuilder, IMPropertyInfo pPropertyInfo, object pModel, Guid pId, IMForm pParent, bool pIsInFilterRow, IMField pField, bool pUpdateOnInput) { try { if (!IsTypeSupported(typeof(T)) || IsPropertyHolderNull(pPropertyInfo, pModel)) { ShowNotSupportedType(pBuilder, pPropertyInfo, pModel, pId); return; } T value = default(T); var val = pPropertyInfo.GetValue(pModel); if (val != null) { value = (T)ReflectionHelper.ChangeType(val, typeof(T)); } Type tType = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T); bool isReadOnly = pPropertyInfo.IsReadOnly || pPropertyInfo.GetCustomAttribute <ReadOnlyAttribute>() != null; var restrictValues = pPropertyInfo.GetCustomAttribute <RestrictValuesAttribute>(); if (typeof(T) == typeof(bool?) || tType.IsEnum || restrictValues != null) { pBuilder.OpenComponent <MSelect <T> >(0); if (pIsInFilterRow) { pBuilder.AddAttribute(10, "NullValueDescription", "\u200b"); } } else if (mNumberTypes.Contains(tType)) { if (pUpdateOnInput) { pBuilder.OpenComponent <InputNumberOnInput <T> >(0); } else { pBuilder.OpenComponent <InputNumber <T> >(0); } } else if (tType == typeof(DateTime) || tType == typeof(DateTimeOffset)) { if (pPropertyInfo.GetCustomAttribute <TimeAttribute>() != null) { pBuilder.OpenComponent <InputTime <T> >(0); } else if (pPropertyInfo.GetCustomAttribute <DateTimeAttribute>() != null) { pBuilder.OpenComponent <InputDateTime <T> >(0); } else { pBuilder.OpenComponent <InputDate <T> >(0); } } else if (typeof(T) == typeof(bool)) { pBuilder.OpenComponent <MInputCheckbox>(0); } else if (tType == typeof(Guid)) { pBuilder.OpenComponent <InputGuid <T> >(0); } else if (tType == typeof(Type)) { pBuilder.OpenComponent <InputType>(0); } else { if (pPropertyInfo.GetCustomAttribute <TextAreaAttribute>() != null) { pBuilder.OpenComponent <InputTextArea>(0); } else { pBuilder.OpenComponent <InputText>(0); } } if (pPropertyInfo.GetCustomAttribute <PasswordAttribute>() != null) { pBuilder.AddAttribute(33, "type", "password"); } if (pField.AdditionalAttributes != null) { pBuilder.AddMultipleAttributes(17, pField.AdditionalAttributes .Where(a => a.Key != Extensions.MFORM_IN_TABLE_ROW_TD_STYLE_ATTRIBUTE) .Where(a => a.Key != nameof(IMGridColumn)) .ToDictionary(a => a.Key, a => a.Value)); } // pBuilder.SetUpdatesAttributeName(pPropertyInfo.Name); pBuilder.AddAttribute(1, "id", pId); pBuilder.AddAttribute(2, "Value", value); pBuilder.AddAttribute(129, "form", pParent.Id); pBuilder.AddAttribute(23, "ValueChanged", RuntimeHelpers.CreateInferredEventCallback <T>(pParent, async __value => { await InvokeValueChanged(pParent, pPropertyInfo, pField, pModel, __value); }, value)); if (pUpdateOnInput && !mNumberTypes.Contains(tType)) { pBuilder.AddAttribute(23, "oninput", EventCallback.Factory.Create <ChangeEventArgs>(pParent, async a => { object value = a.Value; value = ReflectionHelper.ChangeType(value, typeof(T)); //maybe the TryParseValueFromString method of the input is better? await InvokeValueChanged(pParent, pPropertyInfo, pField, pModel, value); })); } pBuilder.AddAttribute(23, "onkeyup", EventCallback.Factory.Create <KeyboardEventArgs>(pParent, (a) => { pParent.OnInputKeyUp(a, pPropertyInfo); })); var valueExpression = GetValueExpression <T>(pPropertyInfo, pModel); pBuilder.AddAttribute(4, "ValueExpression", valueExpression); string cssClass = "m-form-control"; if (isReadOnly) { pBuilder.AddAttribute(33, "disabled", string.Empty); pBuilder.AddAttribute(33, "IsDisabled", true); } pBuilder.AddAttribute(10, "class", cssClass); // pBuilder.SetUpdatesAttributeName(pPropertyInfo.Name); <- new code generator will add this, but I don't know why if (restrictValues != null) { foreach (var allowedValue in restrictValues.AllowedValues) { if (typeof(T) != typeof(string) && !typeof(T).IsAssignableFrom(allowedValue.GetType())) { throw new Exception($"Allowed value {allowedValue} does not implement property type {typeof(T).AssemblyQualifiedName}"); } } IEnumerable <T> options; if (typeof(T) == typeof(string)) { options = restrictValues.AllowedValues.Select(v => (T)(object)v.ToString()).ToArray(); } else { options = restrictValues.AllowedValues.Cast <T>().ToArray(); } pBuilder.AddAttribute(10, "Options", options); } else if (typeof(T) == typeof(bool?)) { IEnumerable <bool?> options = new bool?[] { true, false }; pBuilder.AddAttribute(10, "Options", options); } pBuilder.CloseComponent(); if (pParent.EnableValidation) { pBuilder.OpenComponent <ValidationMessage <T> >(60); pBuilder.AddAttribute(61, "For", valueExpression); pBuilder.CloseComponent(); } } catch (Exception e) { Console.WriteLine(e.ToString()); throw; } }