From c87330a0322f072f769460b24b7bcd42bc88cda3 Mon Sep 17 00:00:00 2001 From: femsci Date: Sat, 18 Nov 2023 15:06:16 +0100 Subject: [PATCH] autocomplete --- .../Pages/Contracts.razor | 18 ++++++- .../Shared/Autocomplete.razor | 51 +++++++++++++++++++ .../Shared/Autocomplete.razor.css | 32 ++++++++++++ 3 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 src/Nyanlabs.Umogen.Server/Shared/Autocomplete.razor create mode 100644 src/Nyanlabs.Umogen.Server/Shared/Autocomplete.razor.css diff --git a/src/Nyanlabs.Umogen.Server/Pages/Contracts.razor b/src/Nyanlabs.Umogen.Server/Pages/Contracts.razor index 382af79..9a760e5 100644 --- a/src/Nyanlabs.Umogen.Server/Pages/Contracts.razor +++ b/src/Nyanlabs.Umogen.Server/Pages/Contracts.razor @@ -14,12 +14,12 @@
- + @(employer == null ? "" : $"{employer.GetName()}")
- + @(employee == null ? "" : $"{employee.Name} {employee.Surname}")
@@ -60,6 +60,7 @@ _employerId = (string)e.Value!; employer = await db.Persons.AsNoTracking().SingleOrDefaultAsync(s => s.Pesel == _employerId) as IEntity ?? await db.Companies.AsNoTracking().SingleOrDefaultAsync(s => s.Nip == _employerId); + StateHasChanged(); } private async Task ChgEmployee(ChangeEventArgs e) @@ -96,6 +97,19 @@ _validity = new ValidTime(now, span); } + private IEnumerable<(string, string)> GetEmployers() + { + var pers = db.Persons.AsNoTracking().AsEnumerable().Select(p => ($"{p.Name} {p.Surname}", p.Pesel)); + var comp = db.Companies.AsNoTracking().AsEnumerable().Select(c => (c.Name, c.Nip!)); + + return pers.Union(comp); + } + + private IEnumerable<(string, string)> GetEmployees() + { + return db.Persons.AsNoTracking().AsEnumerable().Select(p => ($"{p.Name} {p.Surname}", p.Pesel)); + } + private async Task Process() { disB = true; diff --git a/src/Nyanlabs.Umogen.Server/Shared/Autocomplete.razor b/src/Nyanlabs.Umogen.Server/Shared/Autocomplete.razor new file mode 100644 index 0000000..c9d5e07 --- /dev/null +++ b/src/Nyanlabs.Umogen.Server/Shared/Autocomplete.razor @@ -0,0 +1,51 @@ +
+ + @if (focus) + { +
+ @foreach (var tup in Filter()) + { +
+ @tup.Item1 (@tup.Item2) +
+ } +
+ } +
+@code { + private bool focus = false; + [Parameter] + public IEnumerable<(string, string)> AcceptedValues { get; set; } = []; + + + [Parameter] + public string Value { get; set; } = ""; + + private async Task Prechange(ChangeEventArgs e) + { + Value = (string)e.Value!; + StateHasChanged(); + await Change.InvokeAsync(e); + } + + [Parameter] + public EventCallback Change { get; set; } + + private async Task Select(string s) + { + focus = false; + Value = s; + + await Prechange(new() + { + Value = s + }); + StateHasChanged(); + } + + private IEnumerable<(string, string)> Filter() + { + return AcceptedValues.Where(v => v.Item1.StartsWith(Value, StringComparison.OrdinalIgnoreCase) || + v.Item2.StartsWith(Value, StringComparison.OrdinalIgnoreCase)); + } +} diff --git a/src/Nyanlabs.Umogen.Server/Shared/Autocomplete.razor.css b/src/Nyanlabs.Umogen.Server/Shared/Autocomplete.razor.css new file mode 100644 index 0000000..5ff9644 --- /dev/null +++ b/src/Nyanlabs.Umogen.Server/Shared/Autocomplete.razor.css @@ -0,0 +1,32 @@ +.relat { + position: relative; +} + +.autocomplete { + position: relative; + display: inline-block; +} + +.autocomplete-items { + position: absolute; + border: 1px solid #d4d4d4; + border-bottom: none; + border-top: none; + z-index: 99; + /*position the autocomplete items to be the same width as the container:*/ + top: 3rem; + left: 0; + right: 0; + width: fit-content; +} + +.autocomplete-items div { + padding: 10px; + cursor: pointer; + background-color: #fff; + border-bottom: 1px solid #d4d4d4; +} + +.autocomplete-items div:hover { + background-color: rgb(156, 237, 248); +}