DEV Community

Cover image for Simple take-away Angular input toggle using Font Awesome
Leo Lanese
Leo Lanese

Posted on

4 3

Simple take-away Angular input toggle using Font Awesome

 What we are going to do:

We are going to create a simple reusable input select toggle. This is a simple take away HTML input check using Angular and fontAwesome that can be handy to use in any application:

fontawesome icons:

Alt Text
Alt Text

https://fontawesome.com/icons/square
https://fontawesome.com/icons/check-square

Demo:

stackblitz Angular input toggle

Setup:

We need to install the next few dependencies under an Angular project.

setup package.json

...
    "@fortawesome/angular-fontawesome": "^0.3.0",
    "@fortawesome/fontawesome-svg-core": "^1.2.25",
    "@fortawesome/free-solid-svg-icons": "^5.5.0",
...

Enter fullscreen mode Exit fullscreen mode

Module declaration:

We need to import the @fortawesome dependencies in the app.module, and use the constructor to add the faSquare andfaCheckSquare from 'free-solid-svg-icons' to the library.

setup app.module.ts

import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
import { library } from '@fortawesome/fontawesome-svg-core';
import { faSquare, faCheckSquare } from '@fortawesome/free-solid-svg-icons';


 imports: [
...
    FontAwesomeModule
...
]

export class AppModule {
  constructor() {
    library.add(faSquare, faCheckSquare);
  }
}

Enter fullscreen mode Exit fullscreen mode

Component Implementation:

Component

import { Component } from '@angular/core';

import { faCoffee } from '@fortawesome/free-solid-svg-icons';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  isActive = true;

  faUser = ['fas', 'square'];
  faUserDefault = ['fas', 'square'];
  faUserCheck = ['fas', 'check-square'];

  toggle(): boolean {
    return this.isActive = !this.isActive;
  }

  onClickBtn(e) {
    this.toggle() ? this.faUserDefault = this.faUser : this.faUserDefault = this.faUserCheck;
  }
}
Enter fullscreen mode Exit fullscreen mode

Template implementation

Template

<fa-icon
  [icon]="faUserDefault"
  (click)="onClickBtn($event)"
></fa-icon>
Enter fullscreen mode Exit fullscreen mode

GitHub

GitHub code example:
https://github.com/leolanese/simple-input-toggle

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (1)

Collapse
 
georgy99 profile image
georgy99

how i can testing with angular testing library? can tell me some tips?

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay