using System; using System.Collections.Generic; using System.Text.Json; using System.Threading.Tasks; using AntDesign.JsInterop; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; using Microsoft.JSInterop; #pragma warning disable 1591 // Disable missing XML comment namespace AntDesign.Select.Internal { public partial class SelectContent: IDisposable { [CascadingParameter(Name = "ParentSelect")] internal Select ParentSelect { get; set; } [CascadingParameter(Name = "ParentLabelTemplate")] internal RenderFragment 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 OnInput { get; set; } [Parameter] public EventCallback OnKeyUp { get; set; } [Parameter] public EventCallback OnKeyDown { get; set; } [Parameter] public EventCallback OnFocus { get; set; } [Parameter] public EventCallback OnBlur { get; set; } [Parameter] public EventCallback OnClearClick { get; set; } [Parameter] public EventCallback> OnRemoveSelected { get; set; } [Parameter] public string SearchValue { get; set; } [Parameter] public ForwardRef RefBack { get; set; } = new ForwardRef(); [Inject] protected IJSRuntime Js { get; set; } [Inject] private DomEventService DomEventService { get; set; } protected ElementReference Ref { get { return _ref; } set { _ref = value; RefBack?.Set(value); } } private string _inputStyle = string.Empty; private string _inputWidth; private bool _suppressInput; private ElementReference _ref; protected override void OnInitialized() { SetSuppressInput(); } protected override async Task OnAfterRenderAsync(bool firstRender) { SetSuppressInput(); if (firstRender && ParentSelect.EnableSearch) { DomEventService.AddEventListener("window", "beforeunload", Reloading, false); await Js.InvokeVoidAsync(JSInteropConstants.AddPreventCursorMoveOnArrowUp, ParentSelect._inputRef); } await base.OnAfterRenderAsync(firstRender); } 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) { _inputWidth = "width: 4px;"; //ToDo fix class } else { _inputWidth = "width: 4px; margin-left: 6.5px;"; //ToDo fix class } } } 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 AdditonalAttributes() { var dict = new Dictionary(); if (ParentSelect.Disabled) dict.Add("tabindex", "-1"); return dict; } /// /// Indicates that a page is being refreshed /// 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("window", "beforeunload", Reloading); if (IsDisposed) return; IsDisposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } ~SelectContent() { // Finalizer calls Dispose(false) Dispose(false); } } }