/// <summary> /// Formats the value as a string. Derived classes can override this to determine the formatting used for <c>CurrentValueAsString</c>. /// </summary> /// <param name="value">The value to format.</param> /// <returns>A string representation of the value.</returns> protected override string?FormatValueAsString(TValue?value) { // Avoiding a cast to IFormattable to avoid boxing. switch (value) { case null: return(null); case int @int: return(BindConverter.FormatValue(@int, CultureInfo.InvariantCulture)); case long @long: return(BindConverter.FormatValue(@long, CultureInfo.InvariantCulture)); case short @short: return(BindConverter.FormatValue(@short, CultureInfo.InvariantCulture)); case float @float: return(BindConverter.FormatValue(@float, CultureInfo.InvariantCulture)); case double @double: return(BindConverter.FormatValue(@double, CultureInfo.InvariantCulture)); case decimal @decimal: return(BindConverter.FormatValue(@decimal, CultureInfo.InvariantCulture)); default: throw new InvalidOperationException($"Unsupported type {value.GetType()}"); } }
// Generate html when the component is rendered. protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenElement(0, "select"); builder.AddMultipleAttributes(1, AdditionalAttributes); string compoundCssClass = !string.IsNullOrWhiteSpace(AdditionalCssClasses) ? $"{AdditionalCssClasses} {CssClass}" : CssClass; builder.AddAttribute(2, "class", compoundCssClass); builder.AddAttribute(3, "value", BindConverter.FormatValue(CurrentValueAsString)); builder.AddAttribute(4, "onchange", EventCallback.Factory.CreateBinder <string>(this, value => CurrentValueAsString = value, CurrentValueAsString, null)); // Add an option element per enum value var enumType = GetEnumType(); BuildEnumValuesSorted(enumType); foreach (TEnum value in EnumValuesSortedNumerically) { builder.OpenElement(5, "option"); builder.AddAttribute(6, "value", value.ToString()); string enumValuePrefix = this.ShowIntValues ? $"{Convert.ToInt32(value)} : " : string.Empty; if (EmptyTextValue.HasValue && Convert.ToInt32(value) == EmptyTextValue.Value) { builder.AddContent(7, string.Empty); } else { builder.AddContent(7, enumValuePrefix + GetDisplayName(value)); } builder.CloseElement(); } builder.CloseElement(); // close the select element }
protected override void BuildRenderTree(RenderTreeBuilder builder) { var index = 0; builder.OpenElement(index, "div"); builder.AddAttribute(index++, "class", ContainerCss); builder.AddContent(index++, new RenderFragment(builder => { var index2 = 0; builder.OpenElement(index2++, "input"); builder.AddMultipleAttributes(index2++, AdditionalAttributes); builder.AddAttribute(index2++, "type", "checkbox"); builder.AddAttribute(index2++, "class", CssClass); builder.AddAttribute(index2++, "id", Id); builder.AddAttribute(index2++, "checked", BindConverter.FormatValue(CurrentValue)); builder.AddAttribute(index2++, "onchange", EventCallback.Factory.CreateBinder <bool>(this, __value => CurrentValue = __value, CurrentValue)); builder.CloseElement(); builder.OpenElement(index2++, "label"); builder.AddAttribute(index2++, "class", LabelCss); builder.AddAttribute(index2++, "for", Id); builder.AddContent(index2++, Label); builder.CloseElement(); })); builder.CloseElement(); }
/// <summary> /// Renders the component to the supplied <see cref="T:Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder" />. /// </summary> /// <param name="builder">A <see cref="T:Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder" /> that will receive the render output.</param> protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenElement(0, "div"); builder.AddAttribute(1, "class", "field"); builder.AddContent(5, field => { field.OpenElement(0, "div"); AddCommonAttributes(field); field.AddContent(10, child => { child.OpenElement(1, "label"); child.AddAttribute(2, "style", "cursor:pointer"); child.AddContent(10, label => { label.OpenElement(1, "input"); label.AddAttribute(2, "type", "radio"); label.AddAttribute(3, "value", BindConverter.FormatValue(Value)?.ToString()); label.AddAttribute(4, "name", CascadedRadioGroup.Name); //label.AddAttribute(3, "class", "hidden"); //label.AddAttribute(4, "tabindex", "-1"); label.AddAttribute(5, "checked", Checked); label.AddAttribute(6, "onchange", CascadedRadioGroup.ChangeEventCallback); label.CloseElement(); label.AddContent(10, Text); }); child.CloseElement(); }); field.CloseElement(); }); builder.CloseElement(); }
protected override void BuildRenderTree(RenderTreeBuilder builder) { if (builder != null) { builder.OpenElement(0, "select"); builder.AddMultipleAttributes(1, AdditionalAttributes); builder.AddAttribute(2, "class", $"input-public-key-type {CssClass}"); builder.AddAttribute(3, "value", BindConverter.FormatValue(CurrentValueAsString)); builder.AddAttribute(4, "onchange", EventCallback.Factory.CreateBinder <string>(this, __value => CurrentValueAsString = __value, CurrentValueAsString)); builder.OpenElement(5, "option"); builder.AddAttribute(6, "value", FormatValueAsString(KeyType.Ed25519)); builder.AddContent(7, "Ed25519"); builder.CloseElement(); builder.OpenElement(5, "option"); builder.AddAttribute(6, "value", FormatValueAsString(KeyType.ECDSA384)); builder.AddContent(7, "ECDSA 384"); builder.CloseElement(); builder.OpenElement(5, "option"); builder.AddAttribute(6, "value", FormatValueAsString(KeyType.RSA3072)); builder.AddContent(7, "RSA 3072"); builder.CloseElement(); builder.OpenElement(5, "option"); builder.AddAttribute(6, "value", FormatValueAsString(KeyType.Contract)); builder.AddContent(7, "CONTRACT"); builder.CloseElement(); builder.CloseElement(); } }
/// <summary> /// Formats the value as a string. Derived classes can override this to determine the formatting used for <c>CurrentValueAsString</c>. /// </summary> /// <param name="value">The value to format.</param> /// <returns>A string representation of the value.</returns> protected override string FormatValueAsString(TValue value) { switch (value) { case null: return(null); case int @int: return(BindConverter.FormatValue(@int, CultureInfo.CurrentCulture)); case long @long: return(BindConverter.FormatValue(@long, CultureInfo.CurrentCulture)); } string format = (DecimalsEffective > 0) ? "0." + String.Join("", Enumerable.Repeat('0', DecimalsEffective)) : "0"; switch (value) { //case short @short: // return BindConverter.FormatValue(@short, CultureInfo.CurrentCulture); case float @float: return(@float.ToString(format, CultureInfo.CurrentCulture)); case double @double: return(@double.ToString(format, CultureInfo.CurrentCulture)); case decimal @decimal: return(@decimal.ToString(format, CultureInfo.CurrentCulture)); } throw new InvalidOperationException($"Unsupported type {value.GetType()}."); }
/// <summary> /// コンポーネントのレンダリングを行います。 /// </summary> /// <param name="builder">ビルダ。</param> protected override void BuildRenderTree(RenderTreeBuilder builder) { if (this.Item.EditMode == EditModeOptions.Edit) { if (this.EditableState == EditStateOptions.None || (this.Item.EditState & this.EditableState) > 0) { builder.OpenElement(0, "input"); builder.AddMultipleAttributes(1, AdditionalAttributes); builder.AddAttribute(2, "type", "text"); builder.AddAttribute(3, "class", CssClass); builder.AddAttribute(4, "value", BindConverter.FormatValue(CurrentValue)); builder.AddAttribute(5, "onchange", EventCallback.Factory.CreateBinder <string>( this, __value => { this.CurrentValueAsString = __value; }, this.CurrentValueAsString )); builder.CloseElement(); } else { builder.AddContent(100, this.DisplayValue); } } else { builder.AddContent(200, this.DisplayValue); } base.BuildRenderTree(builder); }
/// <inheritdoc /> protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenElement(0, Html.TEXTAREA); builder.AddMultipleAttributes(1, Attributes); builder.AddAttribute(2, Html.CLASS, CssClass); // Overwrite class in Attributes builder.AddAttribute(3, Html.VALUE, BindConverter.FormatValue(CurrentValue)); builder.AddAttribute(4, Html.ONCHANGE, EventCallback.Factory.CreateBinder <string>(this, __value => CurrentValueAsString = __value, CurrentValueAsString)); // Disabled? if (Disabled ?? false) { builder.AddAttribute(5, Html.DISABLED, string.Empty); } // Help if (HasHelp) { builder.AddAttribute(6, Html.ARIA_DESCRIBEDBY, $"{Id}-help"); } builder.CloseElement(); // Help BuildRenderTreeHelp(builder); }
protected override void BuildRenderTree(RenderTreeBuilder builder) { var id = Guid.NewGuid(); builder.OpenElement(0, "input"); builder.AddAttribute(1, "type", InputType); builder.AddAttribute(2, "class", ClassBuilder); builder.AddAttribute(3, "value", BindConverter.FormatValue(CurrentValue)); builder.AddAttribute(4, "onclick", RadioOnClickEvent); builder.AddAttribute(7, "onblur", OnBlurEvent); builder.AddAttribute(8, "onfocus", OnFocusEvent); builder.AddAttribute(8, "checked", Checked()); builder.AddAttribute(8, "disabled", IsDisabled); if (IsToggle) { builder.AddAttribute(8, "id", id); } builder.AddMultipleAttributes(10, AdditionalAttributes); builder.AddElementReferenceCapture(11, elReference => Element = elReference); builder.CloseElement(); if (IsToggle) { builder.OpenElement(12, "label"); builder.AddAttribute(13, "for", id); builder.AddAttribute(14, "class", ToggleClassBuilder); builder.AddContent(14, @ChildContent); builder.CloseElement(); } }
public override void BuildInputRendererTree(IRendererTreeBuilder rendererTreeBuilder, IActualItemContext <object> actualItemContext, Action <string, object> onChangeAction, string columnName) { var value = actualItemContext.GetActualItemColumnValue(columnName); if (IsSupportedDateType(value)) { var dateTimeValue = ConvertToDateTime(value); var dateValueContainsTime = Math.Abs(dateTimeValue.TimeOfDay.TotalSeconds) > 0.00000000001; var dateFormat = dateValueContainsTime ? "yyyy-MM-dd'T'HH:mm:ss" : "yyyy-MM-dd"; rendererTreeBuilder .OpenElement(HtmlTagNames.Div, "edit-field-wrapper") .OpenElement(HtmlTagNames.Input, "edit-text-field") .AddAttribute(HtmlAttributes.Type, dateValueContainsTime ? "datetime-local" : "date") .AddAttribute(HtmlAttributes.Value, BindConverter.FormatValue(dateTimeValue, dateFormat)) .AddAttribute(HtmlJsEvents.OnChange, EventCallback.Factory.Create(this, (ChangeEventArgs e) => { onChangeAction?.Invoke(columnName, BindConverterExtensions.ConvertTo(e.Value, DateTime.MinValue)); })) .CloseElement() .CloseElement(); } else { Successor.BuildInputRendererTree(rendererTreeBuilder, actualItemContext, onChangeAction, columnName); } }
/// <inheritdoc /> protected override string FormatValueAsString(TValue?value) { return(value switch { DateTime dateTimeValue => BindConverter.FormatValue(dateTimeValue, DateFormat, CultureInfo.InvariantCulture), DateTimeOffset dateTimeOffsetValue => BindConverter.FormatValue(dateTimeOffsetValue, DateFormat, CultureInfo.InvariantCulture), _ => string.Empty,// Handles null for Nullable<DateTime>, etc. });
private void AddDoubleValueHandlerIfValueIsDouble(IRendererTreeBuilder rendererTreeBuilder, Action <string, object> onChangeAction, string localColumnName, object value) { if (value is double doubleValue) { rendererTreeBuilder.AddAttribute(HtmlAttributes.Value, BindConverter.FormatValue(doubleValue)); rendererTreeBuilder.AddAttribute(HtmlJSEvents.OnChange, EventCallback.Factory.Create(this, (ChangeEventArgs e) => onChangeAction?.Invoke(localColumnName, BindConverterExtensions.ConvertTo(e.Value, 0d)))); } }
/// <inheritdoc /> protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenElement(0, "input"); builder.AddMultipleAttributes(1, AdditionalAttributes); builder.AddAttribute(2, "class", CssClass); builder.AddAttribute(3, "value", BindConverter.FormatValue(CurrentValue)); builder.AddAttribute(4, "onchange", EventCallback.Factory.CreateBinder <string>(this, __value => CurrentValueAsString = __value, CurrentValueAsString)); builder.CloseElement(); }
protected override void BuildInput(RenderTreeBuilder builder, int sequence) { builder.OpenElement(0, "textarea"); builder.AddMultipleAttributes(1, AdditionalAttributes); builder.AddAttribute(2, "class", $"form-control {CssClass}"); builder.AddAttribute(3, "value", BindConverter.FormatValue(CurrentValue)); builder.AddAttribute(4, "onchange", EventCallback.Factory.CreateBinder <string>(this, __value => CurrentValueAsString = __value, CurrentValueAsString)); builder.CloseElement(); }
/// <inheritdoc /> protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenElement(0, $"{ThemeName}-checkbox"); builder.AddMultipleAttributes(1, AdditionalAttributes); builder.AddAttribute(2, "checked", BindConverter.FormatValue(CurrentValue)); builder.AddAttribute(3, "onchange", EventCallback.Factory.CreateBinder <bool>(this, __value => CurrentValue = __value, CurrentValue)); builder.AddContent(4, ChildContent); builder.CloseElement(); }
/// <summary> /// Builds the input checkbox. /// </summary> /// <param name="builder">The builder.</param> private void BuildInputCheckbox(RenderTreeBuilder builder) { builder.OpenElement(1, "input"); builder.AddAttribute(2, "type", "checkbox"); builder.AddAttribute(3, "id", FieldId); builder.AddAttribute(4, "checked", BindConverter.FormatValue(CurrentValue)); builder.AddAttribute(5, "readonly", ReadOnly); builder.AddAttribute(10, "onchange", EventCallback.Factory.CreateBinder <bool>(this, __value => CurrentValue = __value, CurrentValue)); builder.CloseElement(); }
/// <inheritdoc /> protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenElement(0, $"{ThemeName}-text-area"); builder.AddMultipleAttributes(1, AdditionalAttributes); builder.AddAttribute(3, "class", CssClass); builder.AddAttribute(4, "value", BindConverter.FormatValue(CurrentValue)); builder.AddAttribute(5, "oninput", EventCallback.Factory.CreateBinder <string?>(this, __value => CurrentValueAsString = __value, CurrentValueAsString)); builder.AddContent(6, ChildContent); builder.CloseElement(); }
/// <inheritdoc /> protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenElement(0, "textarea"); builder.AddMultipleAttributes(1, AdditionalAttributes); builder.AddAttributeIfNotNullOrEmpty(2, "class", CssClass); builder.AddAttribute(3, "value", BindConverter.FormatValue(CurrentValue)); builder.AddAttribute(4, "onchange", EventCallback.Factory.CreateBinder <string?>(this, __value => CurrentValueAsString = __value, CurrentValueAsString)); builder.AddElementReferenceCapture(5, __inputReference => Element = __inputReference); builder.CloseElement(); }
protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenElement("input"); builder.AddAttribute("hg-input-address"); builder.AddMultipleAttributes(AdditionalAttributes); builder.AddAttribute("class", CssClass); builder.AddAttribute("type", "text"); builder.AddAttribute("value", BindConverter.FormatValue(CurrentValueAsString)); builder.AddAttribute("onchange", EventCallback.Factory.CreateBinder <string?>(this, __value => CurrentValueAsString = __value, CurrentValueAsString)); builder.CloseElement(); }
protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenElement(0, "input"); builder.AddAttribute(1, "step", (string)_stepAttributeValueFieldInfo.GetValue(null) !); builder.AddMultipleAttributes(2, AdditionalAttributes); builder.AddAttribute(3, "type", "number"); builder.AddAttribute(4, "class", CssClass); builder.AddAttribute(5, "value", BindConverter.FormatValue(CurrentValueAsString)); builder.AddAttribute(6, "onchange", EventCallback.Factory.CreateBinder <string>(this, __value => CurrentValueAsString = __value, CurrentValueAsString !)); builder.CloseElement(); }
public void FormatValue_NullableEnum() { // Arrange var value = (SomeLetters?)null; // Act var actual = BindConverter.FormatValue(value); // Assert Assert.Null(actual); }
protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenElement(0, "input"); builder.AddMultipleAttributes(1, AdditionalAttributes); builder.AddAttribute(2, "type", "checkbox"); builder.AddAttribute(3, "class", CssClass); builder.AddAttribute(4, "checked", BindConverter.FormatValue(CurrentValue)); builder.AddAttribute(5, "onchange", EventCallback.Factory.CreateBinder <bool>(this, __value => CurrentValue = __value, CurrentValue)); builder.AddElementReferenceCapture(6, __inputReference => Element = __inputReference); builder.CloseElement(); }
public void FormatValue_Enum_OutOfRange() { // Arrange var value = SomeLetters.A + 3; var expected = value.ToString(); // Act var actual = BindConverter.FormatValue(value); // Assert Assert.Equal(expected, actual); }
public void FormatValue_DateTime_Format() { // Arrange var value = DateTime.Now; var expected = value.ToString("MM-yyyy", CultureInfo.InvariantCulture); // Act var actual = BindConverter.FormatValue(value, "MM-yyyy", CultureInfo.InvariantCulture); // Assert Assert.Equal(expected, actual); }
public void FormatValue_DateOnly() { // Arrange var value = DateOnly.FromDateTime(DateTime.Now); var expected = value.ToString(CultureInfo.CurrentCulture); // Act var actual = BindConverter.FormatValue(value); // Assert Assert.Equal(expected, actual); }
/// <inheritdoc /> protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenElement(0, "input"); builder.AddMultipleAttributes(1, UnknownParameters); builder.AddAttribute(2, "type", "checkbox"); //builder.AddAttribute(3, "class", Classname); builder.AddAttribute(3, "class", ""); builder.AddAttribute(4, "checked", BindConverter.FormatValue(currentValue)); builder.AddAttribute(5, "onchange", EventCallback.Factory.CreateBinder <bool>(this, __value => currentValue = __value, currentValue)); builder.AddAttribute(6, "id", Id); builder.CloseElement(); }
public void FormatValue_TimeOnly_Format() { // Arrange var value = TimeOnly.FromDateTime(DateTime.Now); var expected = value.ToString("HH:mm", CultureInfo.InvariantCulture); // Act var actual = BindConverter.FormatValue(value, "HH:mm", CultureInfo.InvariantCulture); // Assert Assert.Equal(expected, actual); }
public void FormatValue_Bool_Generic() { // Arrange var value = true; var expected = true; // Act var actual = BindConverter.FormatValue <bool>(value); // Assert Assert.Equal(expected, actual); }
public void FormatValue_NullableBool() { // Arrange var value = (bool?)true; var expected = true; // Act var actual = BindConverter.FormatValue(value); // Assert Assert.Equal(expected, actual); }
public void FormatValue_NullableBoolNull_Generic() { // Arrange var value = (bool?)null; var expected = (bool?)null; // Act var actual = BindConverter.FormatValue <bool?>(value); // Assert Assert.Equal(expected, actual); }