Select Supports Generic Option Type#281
Conversation
…brooks/RazorConsole into u/claybrooks/complex-select
🚀 Preview DeploymentA preview build has been generated for this PR from CI run #21182647521! Download the artifact: To view the preview locally:
🌐 Live Preview URL: https://c07a232c.razorconsole.pages.dev The live preview will be automatically updated when you push new |
| private string? FocusedOption = null; | ||
| private bool IsFocused; | ||
| private static readonly string _exampleCode = @"<Rows> | ||
| <Select TItem=""string"" Options=""@(new[] { ""Align"", ""Border"", ""Columns"", ""Panel"", ""Select"", ""Text Input"" })"" |
There was a problem hiding this comment.
Is it necessary to pass TItem here? The code above that actually render the component doesn't expliclty pass the TItem parameter.
There was a problem hiding this comment.
No, in this case it is not necessary. I've removed it. It seems like the only time you need to specify TItem is when you have an explicit ValueChanged binding. For whatever reason, the compiler can't deduce TITem. But if you use @bind-Value, it's fine.
There was a problem hiding this comment.
Pull request overview
This PR makes the Select component generic (Select<TItem>) so it can operate on arbitrary item types, with support for custom display formatting and equality comparison, and updates call sites/tests accordingly.
Changes:
- Generalized
SelecttoSelect<TItem>with newFormatterandComparerparameters and internal helpers to use these for rendering, selection, and type-ahead behavior. - Updated tests and sample usage (gallery and website) to construct
Select<string>explicitly or rely on inference, and added a NavBar example that bindsSelect<Tab>with a custom formatter. - Performed minor layout/copy improvements in the select gallery page (panel title, indentation).
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
src/RazorConsole.Core/Components/Select.razor |
Core change: converts Select to Select<TItem>, wires in Comparer/Formatter, and updates selection, focus, and type-ahead logic to work with generic items instead of hard-coded string. |
src/RazorConsole.Gallery/Components/NavBar.razor |
Switches NavBar to use Select<Tab> with a Formatter projecting labels, demonstrating generic Select with a complex item type. |
src/RazorConsole.Gallery/Pages/SelectGallery.razor |
Keeps the gallery example using string options and updates panel labeling/formatting to clarify it’s the string-based select example. |
src/RazorConsole.Tests/Components/SelectTests.cs |
Adjusts the test hosts to construct Select<string> explicitly so existing behavior tests still compile against the generic component. |
src/RazorConsole.Website/Components/Select_1.razor |
Updates the website sample to declare TItem="string" for the basic select demo so it binds correctly to the new generic component. |
| private string? FocusedOption = null; | ||
| private bool IsFocused; | ||
| private static readonly string _exampleCode = @"<Rows> | ||
| private string Select = "Align"; |
There was a problem hiding this comment.
Field 'Select' can be 'readonly'.
| private bool IsFocused; | ||
| private static readonly string _exampleCode = @"<Rows> | ||
| private string Select = "Align"; | ||
| private string? FocusedOption = null; |
There was a problem hiding this comment.
Field 'FocusedOption' can be 'readonly'.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Xiaoyun Zhang <bigmiao.zhang@gmail.com>
Added generic type param to Select.
Added the following Parameters
Considerations
<Select/>code will break due to not being able to infer type of TItem, especially in theValueChangedparameter.public class Select : Select<string>{}but razor doesn't like that. Seems like there is no way around a breaking change