TourIAm/TIAMSharedUI/Pages/Components/AddressSearchAndSelectCompo...

57 lines
1.5 KiB
Plaintext

@using AyCode.Blazor.Components.Components
@using TIAMWebApp.Shared.Application.Services
@inject GooglePlacesService GooglePlacesService
<h4>
@NullText
</h4>
<DxTextBox @bind-Text="Address"
@oninput="OnInputChanged"
BindValueMode="BindValueMode.OnDelayedInput"
InputDelay="500">
</DxTextBox>
@if (Predictions.Count > 0)
{
<DxComboBox Data="@Predictions"
ValueFieldName="Description"
TextFieldName="Description"
@bind-Value="SelectedPrediction">
</DxComboBox>
}
@code {
[Parameter] public string NullText { get; set; }
[Parameter] public EventCallback<string> AddressChanged { get; set; }
private string Address { get; set; }
private List<PredictionItem> Predictions { get; set; } = new();
private PredictionItem SelectedPrediction { get; set; }
private async Task OnInputChanged(ChangeEventArgs e)
{
var input = Address;
if (!string.IsNullOrWhiteSpace(input))
{
var predictions = await GooglePlacesService.GetPlacePredictionsAsync(input);
Predictions = predictions.ConvertAll(p => new PredictionItem { Description = p });
SelectedPrediction = Predictions[0];
}
else
{
Predictions.Clear();
}
}
private class PredictionItem
{
public string Description { get; set; }
}
}