The tags input in Apple Reminders app looks so simple in the first glance. But as I dug into the functionalities, I could see that it has some interesting features —
- If we type a tag and enter a space, a tag name is made prefixed by a
#
sign - If we type Backspace, the tag is removed
- If we enter the same tag again, only the existing tag is shown
So, I thought of implementing the same in Svelte.
Handling the input
Output —
Now, if we try to add a tag, it works as expected. But if we enter the same tag, it is added again, which is not the expected behavior. This can be fixed as follows —
Output —
Handling Backspace
Next, we need to handle the event on entering the Backspace key. This can be done as follows —
Output —
Handling blur
event
Next, say we have entered a tag and clicked outside the input field. The tag should be added. This can be done as follows —
Output —
Addition: Removing a tag on clicking it
Output —
I found it very interesting about how such a small feature can have so many things going on under the hood. Hope you like it too.