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

MatAutocomplete new component #550

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
324 changes: 315 additions & 9 deletions src/MatBlazor.Demo/Demo/DemoAutocomplete.razor
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
<DocMatAutocomplete></DocMatAutocomplete>


<MatH3>This component is in progress, use MatAutocompleteList instead!</MatH3>


<h5 class="mat-h5">Example</h5>
<DemoContainer>
<Content>
<MatAutocomplete Items="@options" Label="Pick one" @bind-Value="@value"></MatAutocomplete>
<MatAutocomplete Items="@options" TItem="string" Label="Pick one"></MatAutocomplete>

@code
{

string value;

string[] options = new[]
{
"One",
Expand All @@ -26,13 +21,11 @@
</Content>
<SourceContent>
<BlazorFiddle Template="MatBlazor" Code=@(@"
<MatAutocomplete Items=""@options"" Label=""Pick one"" @bind-Value=""@value""></MatAutocomplete>
<MatAutocomplete Items=""@options"" TItem=""string"" Label=""Pick one""></MatAutocomplete>

@code
{

string value;

string[] options = new[]
{
""One"",
Expand All @@ -46,3 +39,316 @@
</SourceContent>
</DemoContainer>

<h5 class="mat-h5">Icon</h5>
<DemoContainer>
<Content>
<MatAutocomplete Items="@options" Icon="thumb_up_alt" TItem="string" Label="Pick one"></MatAutocomplete>
</Content>
<SourceContent>
<BlazorFiddle Template="MatBlazor" Code=@(@"
<MatAutocomplete Items=""@options"" Icon=""thumb_up_alt"" TItem=""string"" Label=""Pick one""></MatAutocomplete>
")></BlazorFiddle>
</SourceContent>
</DemoContainer>

<h5 class="mat-h5">Clear Button</h5>
<DemoContainer>
<Content>
<MatAutocomplete Items="@options" Value="@options[0]" ShowClearButton="true" Icon="thumb_up_alt" TItem="string" Label="Pick one"></MatAutocomplete>
</Content>
<SourceContent>
<BlazorFiddle Template="MatBlazor" Code=@(@"
<MatAutocomplete Items=""@options"" Value=""@options[0]"" ShowClearButton=""true"" Icon=""thumb_up_alt"" TItem=""string"" Label=""Pick one""></MatAutocomplete>
")></BlazorFiddle>
</SourceContent>
</DemoContainer>

<h5 class="mat-h5">ItemTemplate</h5>
<DemoContainer>
<Content>
<p>
<MatAutocomplete Items="@options2" TItem="Car" Label="Select car" CustomStringSelector="@(i => i.Name)" @bind-SelectedItem="@value">
<ItemTemplate>
<div style="display: flex; flex-direction: row; width: 100%;">
<div style="flex: 1;">@context.Name</div>
<div style="width: 150px;">@context.Price$</div>
</div>
</ItemTemplate>
</MatAutocomplete>
</p>
<p>
Selected value: @(value?.Name)
</p>

@code
{

public class Car
{
public string Name { get; set; }
public double Price { get; set; }

public Car(string name, double price)
{
Name = name;
Price = price;
}
}

Car value = null;

Car[] options2 = new[]
{
new Car("Volkswagen Golf", 10000),
new Car("Volkswagen Passat", 11000),
new Car("Volkswagen Polo", 12000),
new Car("Ford Focus", 13000),
new Car("Ford Fiesta", 14000),
new Car("Ford Fusion", 15000),
new Car("Ford Mondeo", 16000),
};

}

</Content>
<SourceContent>
<BlazorFiddle Template="MatBlazor" Code=@(@"
<p>
<MatAutocomplete Items=""@options2"" TItem=""Car"" Label=""Select car"" CustomStringSelector=""@(i => i.Name)"" @bind-SelectedItem=""@value"">
<ItemTemplate>
<div style=""display: flex; flex-direction: row; width: 100%;"">
<div style=""flex: 1;"">@context.Name</div>
<div style=""width: 150px;"">@context.Price$</div>
</div>
</ItemTemplate>
</MatAutocomplete>
</p>
<p>
Selected value: @(value?.Name)
</p>

@code
{

public class Car
{
public string Name { get; set; }
public double Price { get; set; }

public Car(string name, double price)
{
Name = name;
Price = price;
}
}

Car value = null;

Car[] options2 = new[]
{
new Car(""Volkswagen Golf"", 10000),
new Car(""Volkswagen Passat"", 11000),
new Car(""Volkswagen Polo"", 12000),
new Car(""Ford Focus"", 13000),
new Car(""Ford Fiesta"", 14000),
new Car(""Ford Fusion"", 15000),
new Car(""Ford Mondeo"", 16000),
};

}

")></BlazorFiddle>
</SourceContent>
</DemoContainer>


<h5 class="mat-h5">With Full Width</h5>
<DemoContainer>
<Content>
<MatAutocomplete Items="@options2" FullWidth="true" TItem="Car" Label="Select car" CustomStringSelector="@(i => i.Name)">
<ItemTemplate>
<div style="display: flex; flex-direction: row; width: 100%;">
<div style="flex: 1;">@context.Name</div>
<div style="width: 150px;">@context.Price$</div>
</div>
</ItemTemplate>
</MatAutocomplete>
</Content>
<SourceContent>
<BlazorFiddle Template="MatBlazor" Code=@(@"
<MatAutocomplete Items=""@options2"" FullWidth=""true"" TItem=""Car"" Label=""Select car"" CustomStringSelector=""@(i => i.Name)"">
<ItemTemplate>
<div style=""display: flex; flex-direction: row; width: 100%;"">
<div style=""flex: 1;"">@context.Name</div>
<div style=""width: 150px;"">@context.Price$</div>
</div>
</ItemTemplate>
</MatAutocomplete>
")></BlazorFiddle>
</SourceContent>
</DemoContainer>

<h5 class="mat-h5">With Disabled</h5>
<DemoContainer>
<Content>
<MatAutocomplete Items="@options2" Disabled="true" TItem="Car" Label="Select car" CustomStringSelector="@(i => i.Name)" SelectedItem="@options2[1]">
<ItemTemplate>
<div style="display: flex; flex-direction: row; width: 100%;">
<div style="flex: 1;">@context.Name</div>
<div style="width: 150px;">@context.Price$</div>
</div>
</ItemTemplate>
</MatAutocomplete>
</Content>
<SourceContent>
<BlazorFiddle Template="MatBlazor" Code=@(@"
<MatAutocomplete Items=""@options2"" Disabled=""true"" TItem=""Car"" Label=""Select car"" CustomStringSelector=""@(i => i.Name)"" SelectedItem=""@options2[1]"">
<ItemTemplate>
<div style=""display: flex; flex-direction: row; width: 100%;"">
<div style=""flex: 1;"">@context.Name</div>
<div style=""width: 150px;"">@context.Price$</div>
</div>
</ItemTemplate>
</MatAutocomplete>
")></BlazorFiddle>
</SourceContent>
</DemoContainer>

<h5 class="mat-h5">In an edit form with a Data Annotation Validator</h5>
<DemoContainer>
<Content>
<EditForm Model="@model" OnValidSubmit="HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<MatAutocomplete Items="@model.Options" Icon="thumb_up_alt" TItem="string" @bind-SelectedItem="@model.SelectedOption" Label="Pick one(Required)" ShowClearButton="true"></MatAutocomplete>
<MatButton>Submit</MatButton>
</EditForm>
@code
{
class AutocompleteContextModel
{
public List<string> Options { get; set; } = new List<string>() { "A test option", "Another test option", "One more option" };

[System.ComponentModel.DataAnnotations.Required]
public string SelectedOption { get; set; }
}
AutocompleteContextModel model = new AutocompleteContextModel();

void HandleValidSubmit()
{
Console.WriteLine("On Valid Submit");
}
}
</Content>
<SourceContent>
<BlazorFiddle Template="MatBlazor" Code=@(@"
<EditForm Model=""@model"" OnValidSubmit=""HandleValidSubmit"">
<DataAnnotationsValidator />
<ValidationSummary />
<MatAutocomplete Items=""@model.Options"" Icon=""thumb_up_alt"" TItem=""string"" @bind-SelectedItem=""@model.SelectedOption"" Label=""Pick one(Required)"" ShowClearButton=""true""></MatAutocomplete>
<MatButton>Submit</MatButton>
</EditForm>
@code
{
class AutocompleteContextModel
{
public List<string> Options { get; set; } = new List<string>() { ""A test option"", ""Another test option"", ""One more option"" };

[System.ComponentModel.DataAnnotations.Required]
public string SelectedOption { get; set; }
}
AutocompleteContextModel model = new AutocompleteContextModel();

void HandleValidSubmit()
{
Console.WriteLine(""On Valid Submit"");
}
}
")></BlazorFiddle>
</SourceContent>
</DemoContainer>

<h5 class="mat-h5">With ItemsSource and retrieving data from a backend.</h5>
<p> This is useful when you have a backend with pagination based on the search text and you want to use the pagination directly in the backend server.</p>
<DemoContainer>
<Content>
<MatAutocomplete ItemsSource="@DemoSource" TItem="Car" Label="Select car from backend" CustomStringSelector="@(i => i.Name)">
<ItemTemplate>
<div style="display: flex; flex-direction: row; width: 100%;">
<div style="flex: 1;">@context.Name</div>
<div style="width: 150px;">@context.Price$</div>
</div>
</ItemTemplate>
</MatAutocomplete>
@code
{
public class DemoItemsSource : IAutocompleteItemsSource<Car>
{

Car[] carsInServer = new[]
{
new Car("Volkswagen Golf", 10000),
new Car("Volkswagen Passat", 11000),
new Car("Volkswagen Polo", 12000),
new Car("Ford Focus", 13000),
new Car("Ford Fiesta", 14000),
new Car("Ford Fusion", 15000),
new Car("Ford Mondeo", 16000),
};

public async Task<IEnumerable<Car>> GetFilteredItemsAsync(string searchText)
{
// We can retrieve the results from a backend server.
return await Task.Run(() =>
{
System.Threading.Thread.Sleep(300);
return carsInServer.Where(car => string.IsNullOrEmpty(searchText) || car.Name.Contains(searchText, StringComparison.InvariantCultureIgnoreCase)).Take(10);
});
}
}

DemoItemsSource DemoSource = new DemoItemsSource();
}
</Content>
<SourceContent>
<BlazorFiddle Template="MatBlazor" Code=@(@"
<MatAutocomplete ItemsSource=""@DemoSource"" TItem=""Car"" Label=""Select car from backend"" CustomStringSelector=""@(i => i.Name)"">
<ItemTemplate>
<div style=""display: flex; flex-direction: row; width: 100%;"">
<div style=""flex: 1;"">@context.Name</div>
<div style=""width: 150px;"">@context.Price$</div>
</div>
</ItemTemplate>
</MatAutocomplete>
@code
{
public class DemoItemsSource : IAutocompleteItemsSource<Car>
{

Car[] carsInServer = new[]
{
new Car(""Volkswagen Golf"", 10000),
new Car(""Volkswagen Passat"", 11000),
new Car(""Volkswagen Polo"", 12000),
new Car(""Ford Focus"", 13000),
new Car(""Ford Fiesta"", 14000),
new Car(""Ford Fusion"", 15000),
new Car(""Ford Mondeo"", 16000),
};

public async Task<IEnumerable<Car>> GetFilteredItemsAsync(string searchText)
{
// We can retrieve the results from a backend server.
return await Task.Run(() =>
{
System.Threading.Thread.Sleep(300);
return carsInServer.Where(car => string.IsNullOrEmpty(searchText) || car.Name.Contains(searchText, StringComparison.InvariantCultureIgnoreCase)).Take(10);
});
}
}

DemoItemsSource DemoSource = new DemoItemsSource();
}
")></BlazorFiddle>
</SourceContent>
</DemoContainer>
30 changes: 30 additions & 0 deletions src/MatBlazor.Demo/Doc/DocAutocompleteSearchResult.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@inherits MatBlazor.Demo.Components.BaseDocComponent

@* THIS FILE IS AUTOGENERATED FROM C# XML Comments! *@
@* ALL MANUAL CHANGES WILL BE REMOVED! *@


@if (!Secondary) {<h3 class="mat-h3">AutocompleteSearchResult</h3> } else { <h5 class="mat-h5">AutocompleteSearchResult</h5> }

<div><table class="article-table mat-elevation-z5">
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td style="font-weight: bold;">TItem</td>
<td>Generic argument</td>
<td></td>
</tr>
<tr>
<td>ListResult</td>
<td>List&lt;MatAutocompleteItem&lt;TItem&gt;&gt;</td>
<td></td>
</tr>
<tr>
<td>SearchText</td>
<td>String</td>
<td></td>
</tr>
</table></div>
Loading