Skip to content

Fix #307 by moving pupulate key to component upfront in render pipeline#310

Merged
LittleLittleCloud merged 7 commits intomainfrom
u/xiaoyun/2026
Mar 5, 2026
Merged

Fix #307 by moving pupulate key to component upfront in render pipeline#310
LittleLittleCloud merged 7 commits intomainfrom
u/xiaoyun/2026

Conversation

@LittleLittleCloud
Copy link
Member

@LittleLittleCloud LittleLittleCloud commented Mar 1, 2026

fix #307

@page "/issue-307-focus"
@using RazorConsole.Core.Focus
@inject FocusManager FocusManager

<Rows>
    <Markup Content="@_description"
            Foreground="@Color.Grey70" />
    <Newline />

    <Panel Title="Repro" Border="BoxBorder.Rounded" Expand="true">
        <Rows>
            <Markup Content="Main category" Foreground="@Color.Chartreuse1" />
            <Select TItem="string"
                    @key="_mainSelectKey"
                    Options="@_mainCategories"
                    Value="@_selectedCategory"
                    ValueChanged="@OnMainCategoryChanged"
                    Expand="true" />

            @if (_selectedCategory is not null)
            {
                <Newline />
                <Markup Content="Sub category (expected to receive focus automatically)" Foreground="@Color.Yellow" />
                <Select TItem="string"
                        @key="_subSelectKey"
                    Options="@GetSubCategories()"
                        ValueChanged="@OnSubCategoryChanged"
                        Expand="true" />
            }
        </Rows>
    </Panel>

    <Newline />
    <Markup Content=@($"FocusAsync result: {_focusResultText}") Foreground="@_focusResultColor" />
    <Markup Content=@($"CurrentFocusKey: {FocusManager.CurrentFocusKey ?? "(null)"}") Foreground="@Color.Grey70" />
</Rows>

@code {
    private readonly string[] _mainCategories = ["Settings", "Game"];
    private readonly IReadOnlyDictionary<string, string[]> _subCategoriesByMain = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase)
    {
        ["Settings"] = ["Sound", "Video", "Display", "Exit"],
        ["Game"] = ["Keyboard", "Controller", "Gameplay", "Exit"],
    };
    private readonly string _mainSelectKey = "issue307-main-select";
    private readonly string _subSelectKey = "issue307-sub-select";
    private const string _description = "Issue #307 temporary repro: choose a main category, then focus should jump to the second Select keyed with @key.";

    private string? _selectedCategory;
    private string _focusResultText = "Not attempted yet.";
    private Color _focusResultColor = Color.Grey70;

    private string[] GetSubCategories()
    {
        if (_selectedCategory is not null && _subCategoriesByMain.TryGetValue(_selectedCategory, out var categories))
        {
            return categories;
        }

        return [];
    }

    private async Task OnMainCategoryChanged(string value)
    {
        _selectedCategory = value;
        await InvokeAsync(StateHasChanged).ConfigureAwait(false);

        var focused = await FocusManager.FocusAsync(_subSelectKey).ConfigureAwait(false);
        _focusResultText = focused ? "Success" : "Failed";
        _focusResultColor = focused ? Color.Chartreuse1 : Color.Red;
    }

    private async Task OnSubCategoryChanged(string value)
    {
        if (string.Equals(value, "Exit", StringComparison.OrdinalIgnoreCase))
        {
            _selectedCategory = null;
            var focused = await FocusManager.FocusAsync(_mainSelectKey).ConfigureAwait(false);
            _focusResultText = focused ? "Returned focus to main category." : "Sub category closed, but refocus failed.";
            _focusResultColor = focused ? Color.Chartreuse1 : Color.Red;

            await InvokeAsync(StateHasChanged).ConfigureAwait(false);
        }
    }
}
2026-03-03.12.21.45.mov

@LittleLittleCloud LittleLittleCloud changed the title Fix #307 by pupulate key to component in render pipeline Fix #307 by moving pupulate key to component upfront in render pipeline Mar 1, 2026
@github-actions
Copy link

github-actions bot commented Mar 2, 2026

🚀 Preview Deployment

A preview build has been generated for this PR from CI run #22614466738!

Download the artifact:
website-preview-310-e989509ca1ada1c557c23f4bfdb8990772553b47

To view the preview locally:

  1. Download the artifact from the CI workflow run
  2. Extract the ZIP file
  3. Serve the files with a local web server
    (e.g., npx serve dist)

🌐 Live Preview URL: https://1f1863de.razorconsole.pages.dev

The live preview will be automatically updated when you push new
commits to this PR.

@LittleLittleCloud LittleLittleCloud merged commit de8b8db into main Mar 5, 2026
8 checks passed
@LittleLittleCloud LittleLittleCloud deleted the u/xiaoyun/2026 branch March 5, 2026 23:48
@github-actions github-actions bot added this to the v0.5.0 milestone Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Can't set focus to Panel with @key with FocusManager

3 participants