CatalogCountryPicker.razor

Catalog country picker panel. Renders a toggle button showing the selected country and opens a CatalogCountryPopup for choosing one, with reset to All and optional Russian labels.

@using Sandbox
@using Sandbox.UI
@using Socer
@using System
@inherits Panel

<root class="catalog-country-picker localized-text @(Selected!=CatalogCountryNames.All?"has-country":"") @(Open?"country-picker-open":"")">
    <button @ref="ToggleButton" class="catalog-country-toggle" onclick=@Toggle title="@(GamePreferences.Russian?"Выбрать страну":"Choose a country")"><label class="country-picker-caption">@(GamePreferences.Russian?"Страна":"Country")</label><label class="country-picker-value">@CatalogCountryNames.Display(Selected)</label><span class="country-picker-arrow">⌄</span></button>
    @if(Selected!=CatalogCountryNames.All){<button class="catalog-country-reset" onclick=@(()=>Select(CatalogCountryNames.All)) title="@(GamePreferences.Russian?"Все страны":"All countries")">×</button>}
</root>

@code {
    [Parameter] public string Selected {get;set;}=CatalogCountryNames.All;
    [Parameter] public Action<string> OnSelected {get;set;}
    Panel ToggleButton;
    CatalogCountryPopup popup;
    bool wasOpen;
    public bool Open=>popup!=null && !popup.IsDeleting;
    public CatalogCountryPopup ActivePopup=>Open?popup:null;
    public string SearchText=>Open?popup.Query:"";
    void Toggle()
    {
        if(Open){popup.Delete();StateHasChanged();return;}
        popup=new CatalogCountryPopup(ToggleButton??this,Selected,Select);
        StateHasChanged();
    }
    void Select(string country)
    {
        if(Open)popup.Delete();
        Selected=country;OnSelected?.Invoke(country);
        ToggleButton?.Focus();StateHasChanged();
    }
    public override void Tick()
    {
        base.Tick();
        if(Open && !IsVisible)popup.Delete();
        if(wasOpen!=Open){wasOpen=Open;StateHasChanged();}
    }
    public override void OnDeleted(){if(Open)popup.Delete(true);base.OnDeleted();}
    protected override int BuildHash()=>HashCode.Combine(Selected,Open,GamePreferences.Revision);
}