2020-05-29 00:33:49 +08:00
|
|
|
|
using AntDesign.JsInterop;
|
2020-03-23 09:31:08 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2020-03-31 21:23:25 +08:00
|
|
|
|
using System;
|
2020-03-23 09:31:08 +08:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-05-20 12:57:16 +08:00
|
|
|
|
public partial class TextArea : Input
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-04-16 12:05:59 +08:00
|
|
|
|
private const uint DEFAULT_MIN_ROWS = 1;
|
2020-04-24 18:32:50 +08:00
|
|
|
|
|
2020-03-31 21:23:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// scrollHeight of 1 row
|
|
|
|
|
/// </summary>
|
|
|
|
|
private double _rowHeight;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-04-16 12:05:59 +08:00
|
|
|
|
/// total height = row * <see cref="_rowHeight" /> + <see cref="_offsetHeight" />
|
2020-03-31 21:23:25 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
private double _offsetHeight;
|
|
|
|
|
|
|
|
|
|
private string _hiddenWidth;
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
|
|
|
|
private ElementReference _hiddenEle;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-03-31 21:23:25 +08:00
|
|
|
|
public bool AutoSize { get; set; }
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2020-04-16 12:05:59 +08:00
|
|
|
|
private uint _minRows = DEFAULT_MIN_ROWS;
|
2020-04-24 18:32:50 +08:00
|
|
|
|
|
2020-03-23 09:31:08 +08:00
|
|
|
|
[Parameter]
|
2020-03-31 21:23:25 +08:00
|
|
|
|
public uint MinRows
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _minRows;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2020-04-16 12:05:59 +08:00
|
|
|
|
if (value >= DEFAULT_MIN_ROWS && value <= MaxRows)
|
2020-03-31 21:23:25 +08:00
|
|
|
|
{
|
|
|
|
|
_minRows = value;
|
|
|
|
|
AutoSize = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-16 12:05:59 +08:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(MinRows), $"Please enter a value between {DEFAULT_MIN_ROWS} and {MaxRows}");
|
2020-03-31 21:23:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private uint _maxRows = uint.MaxValue;
|
2020-04-24 18:32:50 +08:00
|
|
|
|
|
2020-03-31 21:23:25 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public uint MaxRows
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _maxRows;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2020-04-16 12:05:59 +08:00
|
|
|
|
if (value >= MinRows)
|
2020-03-31 21:23:25 +08:00
|
|
|
|
{
|
|
|
|
|
_maxRows = value;
|
|
|
|
|
AutoSize = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-24 18:32:50 +08:00
|
|
|
|
throw new ArgumentOutOfRangeException($"Please enter a value between {MinRows} and {uint.MaxValue}");
|
2020-03-31 21:23:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<object> OnResize { get; set; }
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2020-03-31 21:23:25 +08:00
|
|
|
|
protected async override Task OnFirstAfterRenderAsync()
|
|
|
|
|
{
|
|
|
|
|
await base.OnFirstAfterRenderAsync();
|
|
|
|
|
|
|
|
|
|
if (AutoSize)
|
|
|
|
|
{
|
|
|
|
|
await CalculateRowHeightAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 09:31:08 +08:00
|
|
|
|
protected override async void OnInputAsync(ChangeEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
// do not call base method to avoid lost focus
|
|
|
|
|
//base.OnInputAsync(args);
|
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
Value = args?.Value.ToString();
|
2020-03-31 21:23:25 +08:00
|
|
|
|
|
|
|
|
|
if (AutoSize)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
await ChangeSizeAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task ChangeSizeAsync()
|
|
|
|
|
{
|
|
|
|
|
// Ant-design use a hidden textarea to calculate row height, totalHeight = rows * rowHeight
|
|
|
|
|
// TODO: compare with maxheight
|
2020-03-31 21:23:25 +08:00
|
|
|
|
|
|
|
|
|
Element element = await JsInvokeAsync<Element>(JSInteropConstants.getDomInfo, _hiddenEle);
|
|
|
|
|
System.Diagnostics.Debug.WriteLine($"hidden\t{element.scrollHeight}");
|
|
|
|
|
|
|
|
|
|
// do not use %mod in case _rowheight is not an integer
|
|
|
|
|
uint rows = (uint)(element.scrollHeight / _rowHeight);
|
|
|
|
|
rows = Math.Max((uint)MinRows, rows);
|
|
|
|
|
if (rows > MaxRows)
|
|
|
|
|
{
|
|
|
|
|
rows = MaxRows;
|
|
|
|
|
Style = $"height: {rows * _rowHeight + _offsetHeight}px;";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Style = $"height: {rows * _rowHeight + _offsetHeight}px;overflow-y: hidden;";
|
|
|
|
|
}
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-31 21:23:25 +08:00
|
|
|
|
private async Task CalculateRowHeightAsync()
|
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
Element element = await JsInvokeAsync<Element>(JSInteropConstants.getDomInfo, InputEl);
|
2020-03-31 21:23:25 +08:00
|
|
|
|
element.ToString();
|
|
|
|
|
_hiddenWidth = $"width: {element.offsetWidth}px;";
|
|
|
|
|
|
|
|
|
|
// save the content in the textarea
|
|
|
|
|
string str = Value;
|
|
|
|
|
|
|
|
|
|
// total height of 1 row
|
|
|
|
|
Value = " ";
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
element = await JsInvokeAsync<Element>(JSInteropConstants.getDomInfo, _hiddenEle);
|
|
|
|
|
double rHeight = element.scrollHeight;
|
|
|
|
|
|
|
|
|
|
// total height of 2 rows
|
|
|
|
|
Value = " \r\n ";
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
element = await JsInvokeAsync<Element>(JSInteropConstants.getDomInfo, _hiddenEle);
|
|
|
|
|
double rrHeight = element.scrollHeight;
|
|
|
|
|
|
|
|
|
|
_rowHeight = rrHeight - rHeight;
|
|
|
|
|
_offsetHeight = rHeight - _rowHeight;
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2020-03-31 21:23:25 +08:00
|
|
|
|
// revert the value back to original content
|
|
|
|
|
Value = str;
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
await ChangeSizeAsync();
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-24 18:32:50 +08:00
|
|
|
|
}
|