-
Notifications
You must be signed in to change notification settings - Fork 518
/
CategoriesComponent.razor
61 lines (55 loc) · 1.92 KB
/
CategoriesComponent.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@inject HttpClient _http
@inject IStringLocalizer<Resource> _localizer
<style>
.editor-add-category {
color: #FFF;
overflow: hidden;
background-color: transparent;
border: none;
outline: none;
font-weight: 600;
font-size: 0.95em;
}
</style>
<div class="d-flex">
@if (PostCategories != null)
{
foreach (var item in PostCategories)
{
<div>
<label>@item.Content</label>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-x" viewBox="0 0 16 16" @onclick="()=> Remove(item.Content)">
<path d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z" />
</svg>
</div>
}
}
<input type="text" id="add_category_control" class="editor-add-category" @bind="Tag" @onkeyup="KeyPressed" name="tagItem" placeholder="@_localizer["add-category"]" />
</div>
@code {
[Parameter] public Post Post { get; set; }
protected string Tag { get; set; }
public List<Category> PostCategories { get; set; }
protected override async Task OnInitializedAsync()
{
await Load();
}
protected async Task Load()
{
Tag = "";
PostCategories = (await _http.GetFromJsonAsync<ICollection<Category>>($"api/category/{Post.Id}")).ToList();
}
protected async Task KeyPressed(KeyboardEventArgs eventArgs)
{
if (eventArgs.Code == "Enter")
{
PostCategories.Add(new Category { Content = Tag, DateUpdated = DateTime.UtcNow });
Tag = await Task.FromResult("");
}
}
protected async Task Remove(string tag)
{
var tagToRemove = await Task.FromResult(PostCategories.Where(c => c.Content == tag).FirstOrDefault());
PostCategories.Remove(tagToRemove);
}
}