internal MFormValueChangedArgs(IMField pField, IMPropertyInfo pPropertyInfo, object pNewValue, T pModel) { NewValue = pNewValue; Model = pModel; Field = pField; Property = pPropertyInfo.Name; PropertyInfo = pPropertyInfo; }
public async Task OnInputValueChanged(IMField pField, IMPropertyInfo pPropertyInfo, object pNewValue) { ChangedValues.Add(pPropertyInfo); if (OnValueChanged.HasDelegate) { await OnValueChanged.InvokeAsync(new MFormValueChangedArgs <T>(pField, pPropertyInfo, pNewValue, Model)); } }
public void RegisterField(IMField pField, bool pSkipRendering = false) { if (pField == null) { return; } FieldList.Add(pField); if (pSkipRendering) { return; } StateHasChanged(); }
private void AddInput(RenderTreeBuilder builder2, IMField field, IMPropertyInfo propertyInfo, Guid inpId) { if (field is IMPropertyField pf) { if (field is IMComplexField) { var appendMethod = typeof(RenderHelper).GetMethod(nameof(RenderHelper.AppendComplexType)).MakeGenericMethod(typeof(T), pf.PropertyType); appendMethod.Invoke(null, new object[] { builder2, propertyInfo, Model, inpId, this, field, MFormGridContext }); return; } bool isInFilterRow = AdditionalAttributes != null && AdditionalAttributes.ContainsKey("data-is-filterrow"); var method = typeof(RenderHelper).GetMethod(nameof(RenderHelper.AppendInput)).MakeGenericMethod(propertyInfo.PropertyType); method.Invoke(null, new object[] { builder2, propertyInfo, Model, inpId, this, isInFilterRow, field }); } }
public async Task OnInputValueChanged(IMField pField, IMPropertyInfo pPropertyInfo, object pNewValue) { if (!ChangedValues.Contains(pPropertyInfo)) { ChangedValues.Add(pPropertyInfo); } if (OnValueChanged.HasDelegate) { await OnValueChanged.InvokeAsync(new MFormValueChangedArgs <T>(pField, pPropertyInfo, pNewValue, Model)); } if (mEditContext != null && pField is IMPropertyField propertyField) { mEditContext.NotifyFieldChanged(mEditContext.Field(propertyField.Property)); } }
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 void UnregisterField(IMField pField) { FieldList.Remove(pField); StateHasChanged(); }
public void RegisterField(IMField pField) { FieldList.Add(pField); StateHasChanged(); }
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; } }
private static async Task InvokeValueChanged(IMForm pParent, IMPropertyInfo pPropertyInfo, IMField pField, object pModel, object pNewValue) { lock (pModel) { if (pPropertyInfo.GetCustomAttribute <DateAttribute>() != null) { var dateTime = pNewValue as DateTime?; if (dateTime != null && dateTime.Value.Kind == DateTimeKind.Unspecified) { pNewValue = DateTime.SpecifyKind(dateTime.Value, DateTimeKind.Utc); } } pPropertyInfo.SetValue(pModel, pNewValue); } await pParent.OnInputValueChanged(pField, pPropertyInfo, pNewValue); }