Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Combobox, Select] Fix the Placeholder attribute #2311

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@inject DataSource Data

<h4>Select the best song from the list or type your own</h4>
<FluentCombobox Label="Best song" Autofocus="true" Items="@Data.Hits" @bind-Value="@hit" Height="200px" />
<FluentCombobox Placeholder="Make a selection..." Label="Best song" Autofocus="true" Items="@Data.Hits" @bind-Value="@hit" Height="200px" />
<p>
Selected hit: @hit
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Id="people-listbox"
Width="200px"
Height="250px"
Placeholder="Make a selection..."
OptionValue="@(p => p.PersonId.ToString())"
OptionText="@(p => p.LastName + ", " + p.FirstName)"
@bind-Value="@SelectedValue"
Expand All @@ -18,5 +19,5 @@
@code
{
Person? SelectedPerson;
string? SelectedValue = "4";
}
string? SelectedValue;
}
1 change: 1 addition & 0 deletions src/Core/Components/List/FluentSelect.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public partial class FluentSelect<TOption> : ListComponentBase<TOption> where TO
.AddStyle($"#{Id}::part(selected-value)", "white-space", "nowrap")
.AddStyle($"#{Id}::part(selected-value)", "overflow", "hidden")
.AddStyle($"#{Id}::part(selected-value)", "text-overflow", "ellipsis")
.AddStyle($"#{Id}::part(selected-value)", "color", "var(--neutral-base-color)", when: !string.IsNullOrEmpty(Placeholder) && SelectedOption is null)
.BuildMarkupString();

protected override string? StyleValue => new StyleBuilder(base.StyleValue)
Expand Down
5 changes: 5 additions & 0 deletions src/Core/Components/List/ListComponentBase.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
private void RenderOptions(RenderTreeBuilder __builder)
{

if (!string.IsNullOrEmpty(Placeholder) && this is FluentSelect<TOption>)
{
<FluentOption TOption="TOption" Value="" Style="display:none;" aria-hidden="true">@Placeholder</FluentOption>
}

if (Items is null)
{
@ChildContent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

<style>
#myselect::part(listbox) { z-index: 9995; }
#myselect::part(selected-value) { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--neutral-base-color); }

</style>
<fluent-select id="xxx" blazor:onchange="1" blazor:onkeydown="2" blazor:elementreference="xxx">
<fluent-option id="xxx" style="display:none;" value="" blazor:onclick="3" aria-hidden="true" blazor:elementreference="xxx">Make a selection...</fluent-option>
<fluent-option id="xxx" value="1-Denis Voituron" blazor:onclick="4" blazor:elementreference="xxx">1-Denis Voituron</fluent-option>
<fluent-option id="xxx" value="2-Vincent Baaij" blazor:onclick="5" blazor:elementreference="xxx">2-Vincent Baaij</fluent-option>
<fluent-option id="xxx" value="3-Bill Gates" blazor:onclick="6" blazor:elementreference="xxx">3-Bill Gates</fluent-option>
</fluent-select>
19 changes: 18 additions & 1 deletion tests/Core/List/FluentSelectTests.razor
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,26 @@
<FluentIcon Value="@(new SampleIcons.Samples.MyCircle())" Slot="end" />
@(context.Name)
</OptionTemplate>
</FluentSelect>);
</FluentSelect>
);

// Assert
cut.Verify();
}

[Fact]
public void FluentSelect_Placeholder()
{
// Arrange
var cut = RenderComponent<FluentSelect<Customer>>(parameters =>
{
parameters.Add(p => p.Id, "myselect");
parameters.Add(p => p.Items, Customers.Get());
parameters.Add(p => p.Placeholder, "Make a selection...");
});

// Assert
Assert.Equal("Make a selection...", cut.Find("fluent-option").InnerHtml);
cut.Verify();
}
}
Loading