/// <summary>
        /// Create a <see cref="VxFormElementLoader{TValue}"/> that will create a <see cref="FormElement"/>
        /// based on the dynamic <see cref="ExpandoObject"/>. This allows for dynamic usage of the form-generator.
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="model"></param>
        /// <param name="key"></param>
        /// <param name="builder"></param>
        private void CreateFormElementReferenceExpando <TValue>(ExpandoObject model, string key,
                                                                RenderTreeBuilder builder, Layout.VxFormElementDefinition formColumnDefinition)
        {
            // cast the model to a dictionary so it's accessable
            var accessor = ((IDictionary <string, object>)model);

            object value = default(TValue);

            accessor.TryGetValue(key, out value);

            var valueChanged = Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck(
                EventCallback.Factory.Create <TValue>(
                    this, EventCallback.Factory.
                    CreateInferred(this, __value => accessor[key] = __value,
                                   (TValue)accessor[key])));

            var formElementReference = new FormElementReference <TValue>()
            {
                Value                = (TValue)value,
                ValueChanged         = valueChanged,
                FormColumnDefinition = formColumnDefinition
            };

            var elementType = typeof(VxFormElementLoader <TValue>);

            builder.OpenComponent(0, elementType);
            builder.AddAttribute(1, nameof(VxFormElementLoader <TValue> .ValueReference), formElementReference);
            builder.CloseComponent();
        }
        private void CreateFormElementReferencePoco <TValue>(object model, PropertyInfo propertyInfo,
                                                             RenderTreeBuilder builder, Layout.VxFormElementDefinition formColumnDefinition)
        {
            var valueChanged = Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck(
                EventCallback.Factory.Create <TValue>(
                    this, EventCallback.Factory.
                    CreateInferred(this, __value => propertyInfo.SetValue(model, __value),

                                   (TValue)propertyInfo.GetValue(model))));
            // Create an expression to set the ValueExpression-attribute.
            var constant = Expression.Constant(model, model.GetType());
            var exp      = Expression.Property(constant, propertyInfo.Name);
            var lamb     = Expression.Lambda <Func <TValue> >(exp);

            var formElementReference = new FormElementReference <TValue>()
            {
                Value                = (TValue)propertyInfo.GetValue(model),
                ValueChanged         = valueChanged,
                ValueExpression      = lamb,
                FormColumnDefinition = formColumnDefinition
            };

            var elementType = typeof(VxFormElementLoader <TValue>);

            builder.OpenComponent(0, elementType);
            builder.AddAttribute(1, nameof(VxFormElementLoader <TValue> .ValueReference), formElementReference);
            builder.CloseComponent();
        }
Beispiel #3
0
        protected static void RenderChildren(
            RenderTreeBuilder builder,
            int index,
            object dataContext,
            string fieldIdentifier,
            Type typeOfChildToRender)
        {
            builder.AddAttribute(
                index++,
                nameof(ChildContent),
                new RenderFragment(_builder =>
            {
                var values = FormElementReference <ValueReferences> .GetValue(
                    dataContext,
                    fieldIdentifier);
                foreach (var val in values)
                {
                    var _index = 0;

                    _builder.OpenComponent(
                        _index++,
                        typeOfChildToRender);

                    _builder.AddAttribute(
                        _index++,
                        nameof(FgInputCheckbox.Value),
                        val.Value);

                    _builder.AddAttribute(
                        _index++,
                        nameof(ValueChanged),
                        Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck(
                            EventCallback.Factory.Create <bool>(
                                dataContext,
                                EventCallback.Factory.CreateInferred(
                                    val.Value,
                                    __value => val.Value = __value, val.Value))));

                    var constant = Expression.Constant(
                        val,
                        val.GetType());
                    var exp = Expression.Property(
                        constant,
                        nameof(ValueReference <string, bool> .Value));
                    var lamb = Expression.Lambda <Func <bool> >(exp);
                    _builder.AddAttribute(
                        _index++,
                        nameof(InputBase <bool> .ValueExpression),
                        lamb);
                    _builder.AddAttribute(
                        _index++,
                        nameof(FgInputCheckbox.Label),
                        val.Key);

                    _builder.CloseComponent();
                }
            }));
        }
        internal static void RenderChildren(RenderTreeBuilder builder,
                                            int index,
                                            object dataContext,
                                            string fieldIdentifier,
                                            Type typeOfChildToRender)
        {
            builder.AddAttribute(index++, nameof(ChildContent),
                                 new RenderFragment(_builder =>
            {
                // when type is a enum present them as an <option> element
                // by leveraging the component InputSelectOption
                var values = FormElementReference <ValueReferences> .GetValue(dataContext, fieldIdentifier);
                foreach (var val in values)
                {
                    var _index = 0;

                    //  Open the InputSelectOption component
                    _builder.OpenComponent(_index++, typeOfChildToRender);

                    // Set the value of the enum as a value and key parameter
                    _builder.AddAttribute(_index++, nameof(VxInputCheckbox <TValue> .Value), val.Value);

                    // Create the handler for ValueChanged. This wil update the model instance with the input
                    _builder.AddAttribute(_index++, nameof(ValueChanged),
                                          Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck(
                                              EventCallback.Factory.Create <bool>(
                                                  dataContext, EventCallback.Factory.
                                                  CreateInferred(val.Value, __value => val.Value = __value, val.Value))));

                    // Create an expression to set the ValueExpression-attribute.
                    var constant = Expression.Constant(val, val.GetType());
                    var exp      = Expression.Property(constant, nameof(ValueReference <string, bool> .Value));
                    var lamb     = Expression.Lambda <Func <bool> >(exp);
                    _builder.AddAttribute(_index++, nameof(InputBase <bool> .ValueExpression), lamb);

                    _builder.AddAttribute(_index++, nameof(VxInputCheckbox <TValue> .Label), val.Key);

                    // Close the component
                    _builder.CloseComponent();
                }
            }));
        }