Cory Rylan

My name is , Google Developer Expert, Speaker, Software Developer. Building Design Systems and Web Components.

Follow @coryrylan
VMWare Clarity

Building forms with Angular and Clarity Design

Cory Rylan

- 3 minutes

This article has been updated to the latest version Angular 17 and tested with Angular 16. The content is likely still applicable for all Angular 2 + versions.

Clarity is an open-source design system for Web UI. The Clarity Design System is built and maintained by VMware. Clarity supports many different frameworks, including Angular, React, Vue, or event just plain JavaScript.

In this post, we will learn how to build a login form with Angular and the latest version of Clarity.

Clarity Design Web Components in Angular

First, you will want to follow the get started guide to install Clarity to your Angular application.

Clarity components are built using Web Components, which allows the components to work in any UI framework. To build our Angular form, we will use the Reactive Forms Module.

import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import '@cds/core/forms/register.js';
import '@cds/core/input/register.js';
import '@cds/core/password/register.js';
import '@cds/core/button/register.js';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  form: FormGroup;

  get email() {
    return this.form.controls.email;
  }

  get password() {
    return this.form.controls.password;
  }

  constructor(private formBuilder: FormBuilder) {
    this.form = this.formBuilder.group({
      email: ['', [Validators.required, Validators.email]],
      password: ['', Validators.required]
    });
  }
}

The Reactive Forms Module provides an API to set and get input updates as well as validation messaging. Below is the basic HTML template used to connect our inputs to the Angular form. We can bind the form group and each form control name to the inputs.

<form [formGroup]="form" (ngSubmit)="submit()">
  <label>Email</label>
  <input formControlName="email" type="email" />

  <label>Password</label>
  <input formControlName="password" type="password" value="hello there" />
</form>

While this will enable us to get value updates from our form, let's add some components from Clarity to get the appropriate UI and validation.

<form [formGroup]="form" (ngSubmit)="submit()">
  <cds-form-group>
    <cds-input>
      <label>Email</label>
      <input formControlName="email" type="email" />
    </cds-input>

    <cds-password>
      <label>Password</label>
      <input formControlName="password" type="password" value="hello there" />
    </cds-password>

    <cds-button type="submit">login</cds-button>
  </cds-form-group>
</form>

The cds-input and cds-password components provide the Clarity look and feel but also ensure the accessibility is appropriately set up.
Clarity also provides a component called cds-form-control. This component provides messaging validation.

<form [formGroup]="form" (ngSubmit)="submit()">
  <cds-form-group>
    <cds-input>
      <label>Email</label>
      <input formControlName="email" type="email" />
      <cds-control-message *ngIf="email.touched && email.hasError('required')" status="error">required</cds-control-message>
      <cds-control-message *ngIf="email.touched && email.hasError('email')" status="error">invalid email</cds-control-message>
    </cds-input>

    <cds-password>
      <label>Password</label>
      <input formControlName="password" type="password" value="hello there" />
      <cds-control-message *ngIf="password.touched && password.hasError('required')" status="error">required</cds-control-message>
    </cds-password>

    <cds-button type="submit">login</cds-button>
  </cds-form-group>
</form>

The cds-control-message component has a status property that can be set to success or error. Using *ngIf, we can toggle when the error messages are shown to the user.

Clarity Design Web Components in Angular

Angular has fantastic support for Web Components, and Clarity provides a full suite of UI components for your application needs. Check out the full working demo below!

View Demo Code   
Twitter Facebook LinkedIn Email
 

No spam. Short occasional updates on Web Development articles, videos, and new courses in your inbox.

Related Posts

VMWare Clarity

Starting a new Adventure

The Clarity Design System and saying goodbye to start a new adventure.

Read Article
VMWare Clarity

Static Sites with Hugo and Clarity Design

Learn how to build a site with the Hugo static site generator and the Clarity Design System

Read Article
Blazor

Next-Gen Apps with the Clarity Design System and Blazor

Learn how to use the Clarity Design System in .NET Blazor and C# for your next-gen application.

Read Article