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

72 lines
1.8 KiB
Plaintext

@using AyCode.Blazor.Components.Components
@using TIAMWebApp.Shared.Application.Services
@inject GooglePlacesService GooglePlacesService
<DxTextBox @bind-Text="Address"
CssClass="form-field"
@oninput="OnInputChanged"
BindValueMode="BindValueMode.OnDelayedInput"
InputDelay="700">
</DxTextBox>
@if (Predictions.Count > 0)
{
<DxComboBox Data="@Predictions"
ValueFieldName="Description"
TextFieldName="Description"
@bind-Value="SelectedPrediction">
</DxComboBox>
}
else
{
<p>No address found</p>
}
@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; }
public PredictionItem SelectedPrediction
{
get => selectedPrediction;
set
{
SetNewDestination(value.Description);
selectedPrediction = value;
}
}
public async Task SetNewDestination(string address)
{
await AddressChanged.InvokeAsync(address);
}
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();
}
}
public class PredictionItem
{
public string Description { get; set; }
}
}