Create an Angular custom input component with grouping
Learn to create your own tags like control

I am a developer from Toronto, Canada and I enjoy writing about JavaScript, Angular and the .NET framework.
Search for a command to run...
Learn to create your own tags like control

I am a developer from Toronto, Canada and I enjoy writing about JavaScript, Angular and the .NET framework.
No comments yet. Be the first to comment.
What does 99.99% mean?

Using Projections - getting related data

Expose an entity using HotChocolate

Reactive programming is somewhat of a shift in thinking on how you get values from a source of data. Instead of you asking the source every few seconds for an update, the source sends you data you are interested in over time. In other words, we are t...

In this article, we will create an Angular component that takes in some text from the user, and as the user presses the Enter key, we will store that value into a visual representation that looks like the picture below:

Here is demo link. Note that the component in the demo is within a parent with a width of 200px. This is so you can play around with line wrapping.
To make things simple so we can focus only on the control functionality, I will be skipping any typeahead dropdown features (maybe we can do that in the future), and I will also be skipping any fancy UI implementations (such as adding an X on the grouped countries). I will also refrain from using any third party library.
This article is purposefully kept simple - the idea is to give you the building blocks.
Note: In the screenshot above, Canada, Japan and Algeria are 3 groups created by the control.
Features of the control:
Missing features: This control does not take a data source to validate against, nor does it display and dropdown for user to select from. Furthermore, I have not focused on updating this control to work with Angular forms. This can by implementing the required interface.
Ideas:
Here is our component template structure:
<div class="ig-container">
<div *ngFor="let group of groups">
<span class="ig-groups" (dblclick)="remove(group)">{{
group
}}</span>
</div>
<input
class="ig-input"
[(ngModel)]="userInput"
style=""
type="text"
(keydown.backspace)="removeLastGroup()"
(keydown.enter)="createGroup()"
/>
</div>
And here are the styles:
.ig-container {
display: flex;
flex-wrap:wrap;
border: 1px solid black;
padding-bottom: 4px;
padding:4px;
}
.ig-groups {
margin:4px 2px;
padding:2px;
border: 1px solid gray;
line-height:30px;
cursor: pointer;
word-break: break-all;
}
.ig-input {
border: 0px solid black;
outline:none;
flex-grow: 1;
width: 20px;
}
We have our parent that is set to display of flexbox. This allows us to place the children (groups and input) next to each other. The input element's border and outline is removed, and the parent is given a border. We let the input control grow to take remaining horizontal space, and we set our parent to wrap.
Also notice that we used ngModel. I did not want to pass the value of our input text through every method, so I decided to make it a two way binding. This way, when a user presses the enter key, I can just clear the userInput variable (and the UI will update automatically).
Here is the typescript file:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
userInput: string = '';
groups = [];
createGroup() {
this.userInput.length > 0 ? this.groups.push(this.userInput): null;
this.userInput = '';
}
remove(value) {
this.groups = this.groups.filter(selection => selection !== value);
}
removeLastGroup(){
if(!this.userInput && this.groups.length > 0) {
this.groups.pop();
return;
}
}
}
The typescript file should be self explanatory now. When a user presses the enter key on our input field, we call the createGroup method. All we want to do in this method is to add our user input into our group array if validation passes.
If the backspace key is pressed, we call the removeLastGroup method. This function does some validation and pops item out of our array (which turns out to be the last item added).
The remove method is called when a user double-clicks on a group. All we do here is to remove the group and assign our new array to the groups variable.
There are a number of improvements that can be made to this component, including: