Пример #1
0
        /// <inheritdoc/>
        protected override void BuildRenderInput(RenderTreeBuilder builder)
        {
            RefreshState();

            if (itemsToRender.Count > 0)
            {
                UglyHack uglyHack = new UglyHack();                 // see comment below

                foreach (var item in itemsToRender)
                {
                    TValue value = SelectorHelpers.GetValue <TItem, TValue>(ValueSelector, item);

                    builder.OpenComponent(1, typeof(HxInputCheckbox));

                    builder.AddAttribute(2, nameof(HxInputCheckbox.Label), SelectorHelpers.GetText(TextSelector, item));
                    builder.AddAttribute(3, nameof(HxInputCheckbox.Value), Value?.Contains(value) ?? false);
                    builder.AddAttribute(4, nameof(HxInputCheckbox.ValueChanged), EventCallback.Factory.Create <bool>(this, @checked => HandleValueChanged(@checked, item)));
                    builder.AddAttribute(5, nameof(HxInputCheckbox.Enabled), EnabledEffective);

                    // We need ValueExpression. Ehm, HxInputCheckbox needs ValueExpression. Because it is InputBase<T> which needs ValueExpression.
                    // We have nothing to give the HxInputCheckbox. So we make own class with property which we assign to the ValueExpression.
                    // Impacts? Unknown. Maybe none.
                    builder.AddAttribute(6, nameof(HxInputCheckbox.ValueExpression), (Expression <Func <bool> >)(() => uglyHack.HackProperty));

                    builder.AddAttribute(7, nameof(HxInputCheckbox.ShowValidationMessage), false);
                    builder.AddAttribute(8, nameof(HxInputCheckbox.Inline), this.Inline);

                    builder.AddMultipleAttributes(100, this.AdditionalAttributes);

                    builder.CloseComponent();
                }
            }
        }
Пример #2
0
 protected override void RenderChipValue(RenderTreeBuilder builder)
 {
     if ((chipValue is null) && (Value != null) && (DataImpl != null))
     {
         // fallback for initial rendering without chipValue
         // does not help when DataImpl is not set yet (loaded asynchronously)
         var item = DataImpl.FirstOrDefault(item => comparer.Equals(Value, SelectorHelpers.GetValue <TItem, TValue>(ValueSelectorImpl, item)));
         chipValue = SelectorHelpers.GetText(TextSelectorImpl, item);
     }
     builder.AddContent(0, chipValue);
 }
Пример #3
0
        /// <inheritdoc/>
        protected override bool TryParseValueFromString(string value, out TValue result, out string validationErrorMessage)
        {
            int index = int.Parse(value);

            result = (index == -1)
                                ? default(TValue)
                                : SelectorHelpers.GetValue <TItem, TValue>(ValueSelectorImpl, itemsToRender[index]);

            validationErrorMessage = null;
            return(true);
        }
Пример #4
0
    /// <inheritdoc />
    protected override string FormatValueAsString(List <TValue> value)
    {
        // Used for CurrentValueAsString (which is used for the input element and for the chip generator).
        // Thats why we do not use NullDataText here.

        if ((!value?.Any() ?? true) || (Data == null))
        {
            // don't care about chip generator, it does not call this method for null/empty value
            return(EmptyText);
        }

        // Take itemsToRender because they are sorted.
        List <TItem> selectedItems = itemsToRender.Where(item => value.Contains(SelectorHelpers.GetValue <TItem, TValue>(ValueSelector, item))).ToList();

        return(String.Join(", ", selectedItems.Select(TextSelector)));
    }
Пример #5
0
    private void HandleItemSelectionChanged(bool @checked, TItem item)
    {
        var    newValue = Value == null ? new List <TValue>() : new List <TValue>(Value);
        TValue value    = SelectorHelpers.GetValue <TItem, TValue>(ValueSelector, item);

        if (@checked)
        {
            newValue.Add(value);
        }
        else
        {
            newValue.Remove(value);
        }

        CurrentValue = newValue;         // setter includes ValueChanged + NotifyFieldChanged
    }
Пример #6
0
 public void SelectorHelpers_GetValue()
 {
     Assert.AreEqual(null, SelectorHelpers.GetValue <int?, string>(i => "X" + i, null)); // use default
     Assert.AreEqual("X1", SelectorHelpers.GetValue <int?, string>(i => "X" + i, 1));    // use value selector
     Assert.AreEqual(1, SelectorHelpers.GetValue <int?, int?>(null, 1));                 // use value selector
 }
Пример #7
0
        private void RefreshState()
        {
            if (DataImpl != null)
            {
                itemsToRender = DataImpl.ToList();

                // AutoSort
                if (AutoSortImpl && (itemsToRender.Count > 1))
                {
                    if (SortKeySelectorImpl != null)
                    {
                        itemsToRender = itemsToRender.OrderBy(this.SortKeySelectorImpl).ToList();
                    }
                    else if (TextSelectorImpl != null)
                    {
                        itemsToRender = itemsToRender.OrderBy(this.TextSelectorImpl).ToList();
                    }
                    else
                    {
                        itemsToRender = itemsToRender.OrderBy(i => i.ToString()).ToList();
                    }
                }

                // set next properties for rendering
                selectedItemIndex = itemsToRender.FindIndex(item => comparer.Equals(Value, SelectorHelpers.GetValue <TItem, TValue>(ValueSelectorImpl, item)));

                if ((Value != null) && (selectedItemIndex == -1))
                {
                    throw new InvalidOperationException($"Data does not contain item for current value '{Value}'.");
                }
            }
            else
            {
                itemsToRender     = null;
                selectedItemIndex = -1;
            }
        }
Пример #8
0
 private void HandleInputClick(int index)
 {
     CurrentValue = SelectorHelpers.GetValue <TItem, TValue>(ValueSelectorImpl, itemsToRender[index]);
 }