Skip to content

Commit 189df72

Browse files
authored
Merge pull request #91 from fboucher/34-add-tags
Fixing tags
2 parents 35f3688 + 17024a3 commit 189df72

File tree

1 file changed

+71
-3
lines changed

1 file changed

+71
-3
lines changed

src/NoteBookmark.BlazorApp/Components/Shared/NoteDialog.razor

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,24 @@
3535
</div>
3636

3737
<div>
38-
<FluentTextField Name="Tags" Label="Tags" @bind-Value="_note.Tags" />
39-
<FluentValidationMessage For="@(() => _note.Tags)" />
38+
<FluentStack Orientation="Orientation.Vertical">
39+
<label>Tags</label>
40+
<FluentStack Orientation="Orientation.Horizontal" Wrap="true" HorizontalGap="4"">
41+
@foreach (var tag in _currentTags)
42+
{
43+
<FluentBadge Appearance="Appearance.Neutral" OnDismissClick="@((e) => RemoveTag(tag))">
44+
@tag
45+
</FluentBadge>
46+
}
47+
</FluentStack>
48+
<FluentStack Orientation="Orientation.Horizontal">
49+
<FluentTextField @bind-Value="_newTagInput"
50+
Placeholder="Add a tag..."
51+
@onkeydown="HandleKeyDown" />
52+
<FluentButton @onclick="() => AddTag(_newTagInput)" Appearance="Appearance.Lightweight" Size="Size.Small">Add</FluentButton>
53+
</FluentStack>
54+
<FluentValidationMessage For="@(() => _note.Tags)" />
55+
</FluentStack>
4056
</div>
4157
</FluentStack>
4258
<div style="color: var(--error);">
@@ -78,11 +94,14 @@
7894
private List<string> _categories = NoteCategories.GetCategories();
7995
private bool _isEditMode = false;
8096

97+
private List<string> _currentTags = new();
98+
private string _newTagInput = string.Empty;
99+
81100
protected override void OnInitialized()
82101
{
83102
// Check if we're editing an existing note or creating a new one
84103
_isEditMode = !string.IsNullOrEmpty(Content.RowKey) && !Content.RowKey.Equals(Guid.Empty.ToString(), StringComparison.OrdinalIgnoreCase);
85-
104+
86105
if (_isEditMode)
87106
{
88107
// Editing mode - use the existing note data
@@ -93,6 +112,8 @@
93112
// Create mode - create a new note with the PostId
94113
_note = new Note { PostId = Content.PostId };
95114
}
115+
116+
ParseTagsFromString();
96117
}
97118

98119
private async Task SaveAsync()
@@ -118,5 +139,52 @@
118139
await Dialog.CloseAsync(new NoteDialogResult { Action = "Delete", Note = _note });
119140
}
120141

142+
private void ParseTagsFromString()
143+
{
144+
_currentTags = string.IsNullOrWhiteSpace(_note.Tags)
145+
? new List<string>()
146+
: _note.Tags.Split(',')
147+
.Select(t => t.Trim().ToLower())
148+
.Where(t => !string.IsNullOrWhiteSpace(t))
149+
.Distinct()
150+
.ToList();
151+
}
152+
153+
private void SerializeTagsToString()
154+
{
155+
_note.Tags = _currentTags.Any() ? string.Join(", ", _currentTags) : null;
156+
}
157+
158+
private void AddTag(string tag)
159+
{
160+
if (string.IsNullOrWhiteSpace(tag)) return;
161+
162+
var normalizedTag = tag.Trim().ToLower();
163+
if (!_currentTags.Contains(normalizedTag))
164+
{
165+
_currentTags.Add(normalizedTag);
166+
SerializeTagsToString();
167+
_newTagInput = string.Empty;
168+
StateHasChanged();
169+
}
170+
}
171+
172+
private void RemoveTag(string tag)
173+
{
174+
_currentTags.Remove(tag);
175+
SerializeTagsToString();
176+
StateHasChanged();
177+
}
178+
179+
private void HandleKeyDown(KeyboardEventArgs e)
180+
{
181+
if (e.Key == "Enter")
182+
{
183+
if (!string.IsNullOrWhiteSpace(_newTagInput))
184+
{
185+
AddTag(_newTagInput);
186+
}
187+
}
188+
}
121189

122190
}

0 commit comments

Comments
 (0)