码迷,mamicode.com
首页 > 其他好文 > 详细

开发一个通用Input Blazor组件

时间:2021-02-09 11:57:38      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ref   protect   intval   focus   oar   ntc   erp   lse   handler   

通常情况下,input 输入框的绑定方式如下:

<label class="col-form-label form-control-sm">@Value</label>

代码文件中:

[Parameter]

public string Value { get; set; }

一般通常用法:

<div class="form-group form-row mb-1">

<label class="col-sm-2 col-form-label">@GetLang("订单编号:")</label>

<div class="col-sm-10 input-group input-group-sm">

<RichErpLabel Value="@order.djno" />

</div>

 

</div>

 

但是,这样的方式只能绑定string型变量,能否绑定任意类型的变量呢?如日期型,正数型,小数型,像这样使用:

<input FieldName="djno" Item="order" FormatString="yyyy.MM.dd" />

 

答案是可以的,于是我们开发一个通用类型的RichErpTextBox组件:

RichErpTextBox.razor:

<input @ref="textInput" type="@Type" class="form-control form-control-sm" value="@Value"

@onchange="@(async (e) => { await OnChangeHandler(e); })"

@onkeyup="onkeyup"

readonly="@IsReadonly"

maxlength="@MaxLength" />

代码文件RichErpTextBox.razor.cs:

public partial class RichErpTextBox : RichErpComponentBase

{

[Parameter] public string FieldName { get; set; }

[Parameter] public Func<object, string> ValueExpress { get; set; }

[Parameter] public string FormatString { get; set; }

#region public object Item

[Parameter]

public object Item

{

get

{

return _Item;

}

set

{

if (value == null) return;

//if (_Item == value) return;

_Item = value;

if (ValueExpress != null)

{

var ls_value = ValueExpress.Invoke(Item);

if (Value != ls_value)

{

Value = ls_value;

}

}

if (FieldName.RichErpIsNotNull())

{

var temp = RichErpReflector.GetValue(Item, FieldName);

if (temp is string)

{

var ls_temp = temp as string;

if (Value != ls_temp)

{

Value = ls_temp;

}

}

else

{

var ls_value = temp.RichErpToString(FormatString);

if (Value != ls_value)

{

Value = ls_value;

}

}

}

}

}

object _Item;

#endregion

#region public string Value

[Parameter]

public string Value

{

get

{

return _Value;

}

set

{

_Value = value;

}

}

string _Value;

#endregion

#region public int? IntValue

[Parameter]

public int? IntValue

{

get

{

return _IntValue;

}

set

{

_IntValue = value;

if (_IntValue == null)

{

_Value = "";

return;

}

_Value = _IntValue.Value.ToString();

}

}

int? _IntValue;

#endregion

#region public decimal? DecValue

[Parameter]

public decimal? DecValue

{

get

{

return _DecValue;

}

set

{

_DecValue = value;

if (_DecValue == null)

{

_Value = "";

return;

}

_Value = _DecValue.RichErpToString(FormatString);

}

}

decimal? _DecValue;

#endregion

[Parameter]

public int MaxLength { get; set; } = int.MaxValue;

[Parameter]

public bool IsReadonly { get; set; }

 

[Parameter] public EventCallback<string> ValueChanged { get; set; }

[Parameter] public EventCallback<decimal?> DecValueChanged { get; set; }

[Parameter] public EventCallback<int?> IntValueChanged { get; set; }

[Parameter] public EventCallback<KeyboardEventArgs> OnKeyup { get; set; }

private ElementReference textInput;

#region async protected virtual Task OnChangeHandler(ChangeEventArgs e)

async protected virtual Task OnChangeHandler(ChangeEventArgs e)

{

try

{

var ls_value = e.Value as string;

if (Value == ls_value) return;

var ls_oldValue = Value;

Value = ls_value;

if (Item != null && FieldName.RichErpIsNotNull())

{

try

{

RichErpReflector.SetValue(Item, FieldName, Value);

}

catch

{

Value = ls_oldValue;

return;

}

}

 

await ValueChanged.InvokeAsync(Value);

}

catch (Exception ex)

{

ShowMessage(ex);

}

}

#endregion

async Task onkeyup(KeyboardEventArgs e)

{

if (OnKeyup.HasDelegate) await OnKeyup.InvokeAsync(e);

}

async public Task Focus()

{

await textInput.FocusAsync();

}

}

最终使用方法:

可以这样:<RichErpTextBox FieldName="propertyName" Item="vm" />

可以这样:<RichErpTextBox Value="@propertyName" IsReadonly="true" />

可以这样:<RichErpTextBox @bind-Value="@propertyName"/>

可以这样:<RichErpTextBox DecValue="teshuExt.jdgl_all_teshu.bswd" />

可以这样:<RichErpTextBox @bind-DecValue="teshuExt.jdgl_all_teshu.bswd" />

是不是很灵活,而且功能非常强大!

 

开发一个通用Input Blazor组件

标签:ref   protect   intval   focus   oar   ntc   erp   lse   handler   

原文地址:https://www.cnblogs.com/richerpxue/p/14387787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!