-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add first classes * Add missing features * Remove FluentOption.OnChange * Add missing attributes * Add Unit Tests * Fix csproj * Add Unit Tests * Add Unit Tests * Fix doc
- Loading branch information
Showing
23 changed files
with
853 additions
and
17 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...mo/FluentUI.Demo.Client/Documentation/Components/List/Select/Examples/SelectDefault.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<FluentSelect Label="Simple" Items="@Digits" /> | ||
|
||
<FluentSelect Label="Functions" Items="@Digits" | ||
OptionDisabled="@(item => item == "Two")" | ||
OptionText="@(item => item?.ToUpper())" | ||
OptionValue="@(item => $"value-{item}")" | ||
OptionSelected="@(item => item == "One")" /> | ||
|
||
<FluentSelect Label="Binding" Items="@Digits" @bind-Value="@Value" /> | ||
|
||
<FluentSelect Label="Template" Items="@Digits" @bind-Value="@Value"> | ||
<OptionTemplate> | ||
@if (!String.IsNullOrEmpty(context)) | ||
{ | ||
<span>➡️</span> | ||
@context | ||
} | ||
</OptionTemplate> | ||
</FluentSelect> | ||
|
||
<FluentSelect Label="Manual" @bind-Value="@Value"> | ||
<FluentOption Value="One">One</FluentOption> | ||
<FluentOption Value="Two">Two</FluentOption> | ||
<FluentOption Value="Three">Three</FluentOption> | ||
</FluentSelect> | ||
|
||
<FluentSelect Label="Colors" Items="@GetEnumValues()" @bind-Value="@SelectedColor" /> | ||
|
||
<div> | ||
<FluentButton OnClick="@(e => { Value = "One"; })">All to 'One'</FluentButton> | ||
</div> | ||
|
||
<div style="margin-top: 16px;"> | ||
<div>Selected value: @Value</div> | ||
<div>Selected color: @SelectedColor</div> | ||
</div> | ||
|
||
@code { | ||
|
||
private string?[] Digits = new string?[] { null, "One", "Two", "Three" }; | ||
private string? Value; | ||
private Color SelectedColor; | ||
|
||
public static IEnumerable<Color> GetEnumValues() | ||
{ | ||
return Enum.GetValues(typeof(Color)).Cast<Color>(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
.../Demo/FluentUI.Demo.Client/Documentation/Components/List/Select/FluentSelect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
title: Select | ||
route: /List/Select | ||
--- | ||
|
||
# Select | ||
|
||
The `Select` component allows one option to be selected from multiple options. | ||
|
||
View the [Usage Guidance](https://fluent2.microsoft.design/components/web/react/select/usage). | ||
|
||
## TEST | ||
|
||
{{ SelectDefault }} | ||
|
||
## API FluentSelect | ||
|
||
{{ API Type=FluentSelect }} | ||
|
||
{{ API Type=FluentOption }} | ||
|
||
## Migrating to v5 | ||
|
||
TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
@namespace Microsoft.FluentUI.AspNetCore.Components | ||
@using Microsoft.AspNetCore.Components.Rendering | ||
@inherits FluentInputBase<TOption> | ||
@typeparam TOption | ||
|
||
@code | ||
{ | ||
/// <summary> | ||
/// Internal method to render the options. | ||
/// See the <see cref="RenderOptions"/> method for the public API. | ||
/// </summary> | ||
/// <param name="__builder"></param> | ||
private void InternalRenderOptions(RenderTreeBuilder __builder) | ||
{ | ||
if (Items is null) | ||
{ | ||
@ChildContent | ||
} | ||
else | ||
{ | ||
@foreach (TOption item in Items) | ||
{ | ||
<FluentOption Value="@GetOptionValue(item)" | ||
Disabled="@(GetOptionDisabled(item))" | ||
Selected="@GetOptionSelected(item)" | ||
SelectedChanged="@(e => OnSelectedItemChangedHandlerAsync(item))" | ||
aria-selected="@(GetOptionSelected(item) ? "true" : null)"> | ||
@if (OptionTemplate is not null) | ||
{ | ||
@OptionTemplate(item) | ||
} | ||
else | ||
{ | ||
@GetOptionText(item) | ||
} | ||
</FluentOption> | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// ------------------------------------------------------------------------ | ||
// MIT License - Copyright (c) Microsoft Corporation. All rights reserved. | ||
// ------------------------------------------------------------------------ | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.FluentUI.AspNetCore.Components.Extensions; | ||
using Microsoft.FluentUI.AspNetCore.Components.Utilities; | ||
|
||
namespace Microsoft.FluentUI.AspNetCore.Components; | ||
|
||
/// <summary /> | ||
public abstract partial class FluentListBase<TOption> : FluentInputBase<TOption> | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FluentListBase{TOption}"/> class. | ||
/// </summary> | ||
protected FluentListBase() | ||
{ | ||
Id = Identifier.NewId(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the width of the component. | ||
/// </summary> | ||
[Parameter] | ||
public string? Width { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the height of the component. | ||
/// </summary> | ||
[Parameter] | ||
public string? Height { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the content to be rendered inside the component. | ||
/// </summary> | ||
[Parameter] | ||
public virtual RenderFragment? ChildContent { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the content source of all items to display in this list. | ||
/// Each item must be instantiated (cannot be null). | ||
/// </summary> | ||
[Parameter] | ||
public virtual IEnumerable<TOption>? Items { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the template for the <see cref="FluentListBase{TOption}.Items"/> items. | ||
/// </summary> | ||
[Parameter] | ||
public virtual RenderFragment<TOption>? OptionTemplate { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the function used to determine which value to apply to the option value attribute. | ||
/// </summary> | ||
[Parameter] | ||
public virtual Func<TOption?, string>? OptionValue { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the function used to determine which text to display for each option. | ||
/// </summary> | ||
[Parameter] | ||
public virtual Func<TOption?, string>? OptionText { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the function used to determine if an option is initially selected. | ||
/// </summary> | ||
[Parameter] | ||
public virtual Func<TOption?, bool>? OptionSelected { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the function used to determine if an option is disabled. | ||
/// </summary> | ||
[Parameter] | ||
public virtual Func<TOption?, bool>? OptionDisabled { get; set; } | ||
|
||
/// <summary /> | ||
protected virtual bool GetOptionSelected(TOption? item) | ||
{ | ||
return OptionSelected?.Invoke(item) ?? Equals(item, CurrentValue); | ||
} | ||
|
||
/// <summary /> | ||
protected virtual string? GetOptionValue(TOption? item) | ||
{ | ||
return OptionValue?.Invoke(item) ?? item?.ToString() ?? null; | ||
} | ||
|
||
/// <summary /> | ||
protected virtual string? GetOptionText(TOption? item) | ||
{ | ||
return OptionText?.Invoke(item) ?? item?.ToString() ?? string.Empty; | ||
} | ||
|
||
/// <summary /> | ||
protected virtual bool GetOptionDisabled(TOption? item) | ||
{ | ||
return OptionDisabled?.Invoke(item) ?? false; | ||
} | ||
|
||
/// <summary /> | ||
protected virtual async Task OnSelectedItemChangedHandlerAsync(TOption? item) | ||
{ | ||
if (Disabled || item == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (!Equals(item, CurrentValue)) | ||
{ | ||
// Assign the current value and raise the change event | ||
CurrentValue = item; | ||
} | ||
|
||
await Task.CompletedTask; | ||
} | ||
|
||
/// <summary> | ||
/// Renders the list options. | ||
/// </summary> | ||
/// <returns></returns> | ||
protected virtual RenderFragment? RenderOptions() => InternalRenderOptions; | ||
|
||
/// <summary /> | ||
protected override bool TryParseValueFromString(string? value, [MaybeNullWhen(false)] out TOption result, [NotNullWhen(false)] out string? validationErrorMessage) | ||
{ | ||
return this.TryParseSelectableValueFromString(value, out result, out validationErrorMessage); | ||
} | ||
|
||
/// <summary /> | ||
internal InternalListContext<TOption> GetCurrentContext() | ||
{ | ||
return new InternalListContext<TOption>(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@namespace Microsoft.FluentUI.AspNetCore.Components | ||
@inherits FluentComponentBase | ||
|
||
@* This file must be updated when the `fluent-select` web-component will be available *@ | ||
|
||
<option id="@Id" | ||
class="@Class" | ||
style="@Style" | ||
disabled="@Disabled" | ||
value="@Value" | ||
selected="@Selected" | ||
aria-selected="@(Selected ? "true" : null)" | ||
@onclick="@OnSelectHandlerAsync" | ||
@attributes="@AdditionalAttributes"> | ||
@ChildContent | ||
</option> |
Oops, something went wrong.