2021-02-04 23:40:47 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text.Json;
|
2020-11-27 13:13:26 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2021-02-04 23:40:47 +08:00
|
|
|
|
using AntDesign.JsInterop;
|
2020-11-27 13:13:26 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
2021-02-04 23:40:47 +08:00
|
|
|
|
using Microsoft.JSInterop;
|
2020-11-27 13:13:26 +08:00
|
|
|
|
|
|
|
|
|
#pragma warning disable 1591 // Disable missing XML comment
|
|
|
|
|
|
|
|
|
|
namespace AntDesign.Select.Internal
|
|
|
|
|
{
|
2021-02-04 23:40:47 +08:00
|
|
|
|
public partial class SelectContent<TItemValue, TItem>: IDisposable
|
2020-11-27 13:13:26 +08:00
|
|
|
|
{
|
|
|
|
|
[CascadingParameter(Name = "ParentSelect")] internal Select<TItemValue, TItem> ParentSelect { get; set; }
|
|
|
|
|
[CascadingParameter(Name = "ParentLabelTemplate")] internal RenderFragment<TItem> ParentLabelTemplate { get; set; }
|
|
|
|
|
[CascadingParameter(Name = "ShowSearchIcon")] internal bool ShowSearchIcon { get; set; }
|
|
|
|
|
[CascadingParameter(Name = "ShowArrowIcon")] internal bool ShowArrowIcon { get; set; }
|
|
|
|
|
[Parameter] public string Prefix { get; set; }
|
|
|
|
|
[Parameter] public string Placeholder { get; set; }
|
|
|
|
|
[Parameter] public bool IsOverlayShow { get; set; }
|
|
|
|
|
[Parameter] public bool ShowPlaceholder { get; set; }
|
|
|
|
|
[Parameter] public EventCallback<ChangeEventArgs> OnInput { get; set; }
|
|
|
|
|
[Parameter] public EventCallback<KeyboardEventArgs> OnKeyUp { get; set; }
|
|
|
|
|
[Parameter] public EventCallback<KeyboardEventArgs> OnKeyDown { get; set; }
|
|
|
|
|
[Parameter] public EventCallback<FocusEventArgs> OnFocus { get; set; }
|
|
|
|
|
[Parameter] public EventCallback<FocusEventArgs> OnBlur { get; set; }
|
|
|
|
|
[Parameter] public EventCallback<MouseEventArgs> OnClearClick { get; set; }
|
|
|
|
|
[Parameter] public EventCallback<SelectOptionItem<TItemValue, TItem>> OnRemoveSelected { get; set; }
|
|
|
|
|
[Parameter] public string SearchValue { get; set; }
|
2021-01-21 17:20:10 +08:00
|
|
|
|
[Parameter] public ForwardRef RefBack { get; set; } = new ForwardRef();
|
2021-02-04 23:40:47 +08:00
|
|
|
|
[Inject] protected IJSRuntime Js { get; set; }
|
|
|
|
|
[Inject] private DomEventService DomEventService { get; set; }
|
2021-01-21 17:20:10 +08:00
|
|
|
|
protected ElementReference Ref
|
|
|
|
|
{
|
|
|
|
|
get { return _ref; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_ref = value;
|
|
|
|
|
RefBack?.Set(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-27 13:13:26 +08:00
|
|
|
|
|
|
|
|
|
private string _inputStyle = string.Empty;
|
|
|
|
|
private string _inputWidth;
|
|
|
|
|
private bool _suppressInput;
|
2021-01-21 17:20:10 +08:00
|
|
|
|
private ElementReference _ref;
|
2020-11-27 13:13:26 +08:00
|
|
|
|
|
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
SetSuppressInput();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-04 23:40:47 +08:00
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
2020-11-27 13:13:26 +08:00
|
|
|
|
{
|
|
|
|
|
SetSuppressInput();
|
2021-02-04 23:40:47 +08:00
|
|
|
|
if (firstRender && ParentSelect.EnableSearch)
|
|
|
|
|
{
|
|
|
|
|
DomEventService.AddEventListener("window", "beforeunload", Reloading, false);
|
|
|
|
|
await Js.InvokeVoidAsync(JSInteropConstants.AddPreventCursorMoveOnArrowUp, ParentSelect._inputRef);
|
|
|
|
|
}
|
|
|
|
|
await base.OnAfterRenderAsync(firstRender);
|
2020-11-27 13:13:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override Task OnParametersSetAsync()
|
|
|
|
|
{
|
|
|
|
|
if (ParentSelect.SelectMode != SelectMode.Default) // ToDo Fix class
|
|
|
|
|
SetInputWidth();
|
|
|
|
|
|
|
|
|
|
return base.OnParametersSetAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetInputWidth()
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(SearchValue))
|
|
|
|
|
{
|
|
|
|
|
_inputWidth = $"width: {4 + SearchValue.Length * 8}px;";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (ParentSelect.HasValue)
|
|
|
|
|
{
|
2020-11-28 10:02:35 +08:00
|
|
|
|
_inputWidth = "width: 4px;"; //ToDo fix class
|
2020-11-27 13:13:26 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-11-28 10:02:35 +08:00
|
|
|
|
_inputWidth = "width: 4px; margin-left: 6.5px;"; //ToDo fix class
|
2020-11-27 13:13:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetSuppressInput()
|
|
|
|
|
{
|
|
|
|
|
if (!ParentSelect.IsSearchEnabled)
|
|
|
|
|
{
|
|
|
|
|
if (!_suppressInput)
|
|
|
|
|
{
|
|
|
|
|
_suppressInput = true;
|
|
|
|
|
_inputStyle = "caret-color: transparent;";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (_suppressInput)
|
|
|
|
|
{
|
|
|
|
|
_suppressInput = false;
|
|
|
|
|
_inputStyle = string.Empty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void OnKeyPressEventHandler(KeyboardEventArgs _)
|
|
|
|
|
{
|
|
|
|
|
if (!ParentSelect.IsSearchEnabled)
|
|
|
|
|
SearchValue = string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Dictionary<string, object> AdditonalAttributes()
|
|
|
|
|
{
|
|
|
|
|
var dict = new Dictionary<string, object>();
|
|
|
|
|
|
|
|
|
|
if (ParentSelect.Disabled)
|
|
|
|
|
dict.Add("tabindex", "-1");
|
|
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
|
}
|
2021-02-04 23:40:47 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Indicates that a page is being refreshed
|
|
|
|
|
/// </summary>
|
|
|
|
|
private bool _isReloading;
|
|
|
|
|
|
|
|
|
|
private void Reloading(JsonElement jsonElement) => _isReloading = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsDisposed { get; private set; }
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (!_isReloading)
|
|
|
|
|
{
|
|
|
|
|
_ = InvokeAsync(async () =>
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(100);
|
|
|
|
|
await Js.InvokeVoidAsync(JSInteropConstants.RemovePreventCursorMoveOnArrowUp, ParentSelect._inputRef);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
DomEventService.RemoveEventListerner<JsonElement>("window", "beforeunload", Reloading);
|
|
|
|
|
|
|
|
|
|
if (IsDisposed) return;
|
|
|
|
|
|
|
|
|
|
IsDisposed = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~SelectContent()
|
|
|
|
|
{
|
|
|
|
|
// Finalizer calls Dispose(false)
|
|
|
|
|
Dispose(false);
|
|
|
|
|
}
|
2020-11-27 13:13:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|