ant-design-blazor/components/input-number/InputNumber.razor.cs

134 lines
3.4 KiB
C#
Raw Normal View History

2020-03-26 10:45:35 +08:00
using Microsoft.AspNetCore.Components;
using System;
2020-04-24 18:32:50 +08:00
using System.Globalization;
2020-03-26 10:45:35 +08:00
using System.Linq;
namespace AntDesign
2020-03-26 10:45:35 +08:00
{
public partial class InputNumber : AntInputComponentBase<double>
2020-03-26 10:45:35 +08:00
{
private string _format;
protected const string PrefixCls = "ant-input-number";
[Parameter]
public Func<double, string> Formatter { get; set; }
2020-03-26 10:45:35 +08:00
[Parameter]
public Func<string, double> Parser { get; set; }
2020-03-26 10:45:35 +08:00
private double _step = 1;
2020-04-24 18:32:50 +08:00
2020-03-26 10:45:35 +08:00
[Parameter]
public double Step
2020-03-26 10:45:35 +08:00
{
get
{
return _step;
}
set
{
_step = value;
if (string.IsNullOrEmpty(_format))
{
2020-04-24 18:32:50 +08:00
_format = string.Join('.', _step.ToString(CultureInfo.InvariantCulture).Split('.').Select(n => new string('0', n.Length)));
2020-03-26 10:45:35 +08:00
}
}
}
[Parameter]
public double? DefaultValue { get; set; }
2020-03-26 10:45:35 +08:00
[Parameter]
public double Max { get; set; } = double.PositiveInfinity;
2020-03-26 10:45:35 +08:00
[Parameter]
public double Min { get; set; } = double.NegativeInfinity;
2020-03-26 10:45:35 +08:00
[Parameter]
public string Size { get; set; } = InputSize.Default;
[Parameter]
public bool Disabled { get; set; }
2020-03-26 10:45:35 +08:00
protected override void OnInitialized()
{
base.OnInitialized();
if (DefaultValue.HasValue)
2020-03-26 10:45:35 +08:00
{
Value = DefaultValue.Value;
2020-03-26 10:45:35 +08:00
}
}
private void SetClass()
{
ClassMapper.Clear()
.Add(PrefixCls)
.If($"{PrefixCls}-lg", () => Size == InputSize.Large)
.If($"{PrefixCls}-sm", () => Size == InputSize.Small)
.If($"{PrefixCls}-disabled", () => this.Disabled);
2020-03-26 10:45:35 +08:00
}
protected override void OnParametersSet()
{
base.OnParametersSet();
SetClass();
}
private void Increase()
{
OnInput(new ChangeEventArgs() { Value = Value + Step });
2020-03-26 10:45:35 +08:00
}
private void Decrease()
{
OnInput(new ChangeEventArgs() { Value = Value - Step });
2020-03-26 10:45:35 +08:00
}
private void OnInput(ChangeEventArgs args)
{
// TODO: handle non-number input, parser
double num;
if (Parser != null)
2020-03-26 10:45:35 +08:00
{
num = Parser(args.Value.ToString());
2020-03-26 10:45:35 +08:00
}
else
{
2020-04-24 18:32:50 +08:00
_ = double.TryParse(args.Value.ToString(), out num);
2020-03-26 10:45:35 +08:00
}
if (num >= Min && num <= Max)
2020-03-26 10:45:35 +08:00
{
Value = num;
}
}
private string GetIconClass(string direction)
{
2020-04-24 18:32:50 +08:00
string cls;
2020-03-26 10:45:35 +08:00
if (direction == "up")
{
cls = $"ant-input-number-handler ant-input-number-handler-up " + (Value >= Max ? "ant-input-number-handler-up-disabled" : string.Empty);
2020-03-26 10:45:35 +08:00
}
else
{
cls = $"ant-input-number-handler ant-input-number-handler-down " + (Value <= Min ? "ant-input-number-handler-down-disabled" : string.Empty);
2020-03-26 10:45:35 +08:00
}
return cls;
}
private string DisplayValue()
{
if (Formatter != null)
2020-03-26 10:45:35 +08:00
{
return Formatter(Value);
2020-03-26 10:45:35 +08:00
}
2020-04-24 18:32:50 +08:00
return Value.ToString(_format, CultureInfo.InvariantCulture);
2020-03-26 10:45:35 +08:00
}
}
}