<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: hanneslim</title>
    <description>The latest articles on DEV Community by hanneslim (@hanneslim).</description>
    <link>https://dev.to/hanneslim</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F446609%2F3887e070-f812-4a39-9ee0-061e71070494.png</url>
      <title>DEV Community: hanneslim</title>
      <link>https://dev.to/hanneslim</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hanneslim"/>
    <language>en</language>
    <item>
      <title>Custom Angular reactive form with SVG and clickable elements</title>
      <dc:creator>hanneslim</dc:creator>
      <pubDate>Sat, 01 Jan 2022 21:33:48 +0000</pubDate>
      <link>https://dev.to/hanneslim/how-to-build-a-custom-angular-reactive-svg-form-with-clickable-elements-1a5k</link>
      <guid>https://dev.to/hanneslim/how-to-build-a-custom-angular-reactive-svg-form-with-clickable-elements-1a5k</guid>
      <description>&lt;p&gt;During the Corona pandemic, many companies and employees discovered home office. As a result, it became apparent that not every employee required his own workstation. Instead, companies now offer a shared desk model. This means that employees flexibly share workstations. Often, you can reserve your desired workstation in advance. This workplace booking function has attracted me for some time. I find the possibility to create a Custom Reactive Form in Angular, which also includes an office plan, particularly interesting. In the Custom Angular Reactive SVG Form there is the possibility to select a workstation from the office plan and to mark it with a color. As soon as a workstation has been chosen, the form will be validated and the desk will be bookable. In the following the implementation is explained step by step.&lt;/p&gt;

&lt;p&gt;We need the ReactiveFormsModule for the Reactive Angular Form SVG. Then we create a FormGroup and FormControl fields. I have named the FormGroup reservationForm. It contains a field for the name, a field for the weekday and a field for the place you want to book.&lt;/p&gt;

&lt;p&gt;`public reservationForm: FormGroup = new FormGroup({&lt;br&gt;
    name: new FormControl('', [Validators.required], []),&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;weekday: new FormControl('', [Validators.required], []),

seat: new FormControl('', [Validators.required], []),
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;});`&lt;/p&gt;

&lt;p&gt;To build a custom form field, the first thing we need is a new component. Here we can directly use a tip from Unleashed Design. Angular offers the possibility to include SVG images as templateUrl. We delete the seats.component.html and replace it with our SVG office plan named seats.component.svg. In the seats.component.ts we can replace the templateUrl with the SVG file:&lt;br&gt;
&lt;code&gt;selector: 'app-seats',&lt;br&gt;
templateUrl: './seats.component.svg',&lt;br&gt;
styleUrls: ['./seats.component.sass'],&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can already see our office plan in the Reactive Form, but it is still without functionality. To change that a ControlValueAccessor is needed. The ControlValueAccessor serves as an interface for a custom form control that integrates with Angular forms. The ControlValueAccessor asks for these four methods:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;interface ControlValueAccessor {&lt;br&gt;
  writeValue(obj: any): void&lt;br&gt;
  registerOnChange(fn: any): void&lt;br&gt;
  registerOnTouched(fn: any): void&lt;br&gt;
  setDisabledState(isDisabled: boolean)?: void&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk3wd9t0fwx3j6lb0ziss.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk3wd9t0fwx3j6lb0ziss.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If it is invalid:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ctq52e1rynprtd062ft.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ctq52e1rynprtd062ft.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Source code and more details to the project are on my blog: &lt;a href="https://nerd-corner.com/how-to-build-a-custom-angular-reactive-svg-form-with-clickable-elements/" rel="noopener noreferrer"&gt;https://nerd-corner.com/how-to-build-a-custom-angular-reactive-svg-form-with-clickable-elements/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
