<?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>Forem: Manoj</title>
    <description>The latest articles on Forem by Manoj (@virtualmanu).</description>
    <link>https://forem.com/virtualmanu</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%2F1016619%2Fe8f00fb4-3e36-415d-ad94-c5495f17cebe.jpeg</url>
      <title>Forem: Manoj</title>
      <link>https://forem.com/virtualmanu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/virtualmanu"/>
    <language>en</language>
    <item>
      <title>How to use Cleave.js in Angular application?</title>
      <dc:creator>Manoj</dc:creator>
      <pubDate>Sun, 29 Jan 2023 16:49:39 +0000</pubDate>
      <link>https://forem.com/virtualmanu/how-to-use-cleavejs-in-angular-application-42f4</link>
      <guid>https://forem.com/virtualmanu/how-to-use-cleavejs-in-angular-application-42f4</guid>
      <description>&lt;h2&gt;
  
  
  Quick intro to Cleave.js:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://bit.ly/3WGTNtj" rel="noopener noreferrer"&gt;Cleave.js&lt;/a&gt; is a library used to format the input as a user starts typing in an  field without the user having to type the delimiter “ — ” as shown in the following gif&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F6h4f2b6ut1gvpd271aoz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F6h4f2b6ut1gvpd271aoz.gif" alt="gif" width="322" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, I will be showing date format with “ / ” as delimiter and “dd/mm/yyyy” as the date pattern, i.e.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  date: true,
  delimiter: '/',
  datePattern: ['d', 'm', 'Y']
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Steps to start using Cleave.js in your angular application:
&lt;/h2&gt;

&lt;p&gt;1) Install Cleave.js using a package manager from terminal, here I’m using npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i cleave.js

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) Next, add the path to Cleave.js minimal bundle to angular.json file (as shown in the image below)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"./node_modules/cleave.js/dist/cleave.min.js"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fzjxx6r7cccu8nom2mjq3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fzjxx6r7cccu8nom2mjq3.png" alt="angular.json" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3) Now, declare that there will be some object called Cleave in the runtime in your component using the following snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;declare let Cleave: any;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fxyyg2tk0vlg642ziy4z5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fxyyg2tk0vlg642ziy4z5.png" alt="declare" width="800" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4) I will be using angular material form input, but you can use regular form tags and Cleave.js will work fine. Add a custom class to the form input field as shown (you can name it anything):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fnt4gqharworjrai8uktj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fnt4gqharworjrai8uktj.png" alt="form class" width="800" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;5) Next, let’s implement the Cleave object in the component constructor like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* 
Cleave takes in two parameters, the first is classname and second is the
object which contains data describing the input format desired.
In this example, we pass in input type, delimiter and datepattern.
*/
new Cleave('.cleave-date', {
      date: true,
      delimiter: '/',
      datePattern: ['d', 'm', 'Y']
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Ftcefkym42ierevh7ruq8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ftcefkym42ierevh7ruq8.png" alt="constructor implementation" width="800" height="504"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice the yellow box, it should be the same class name that has been applied to the input field in the corresponding HTML file.&lt;/p&gt;

&lt;p&gt;Now, let’s serve it using ng serve command :)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng serve -o&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;6) Uh-oh, did you also get this error and an empty screen?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F6k6mrdj8s8clmhpuhia7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F6k6mrdj8s8clmhpuhia7.png" alt="error" width="623" height="264"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What it means is, we are trying to access an element in the DOM that hasn’t been rendered yet. To solve this, we have to implement the code in ngOnInit() lifecycle hook and not the constructor as shown in the image below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fgem1x77266wk9me1538a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fgem1x77266wk9me1538a.png" alt="error fix with ngOnInit" width="717" height="692"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;7) and… Voila! Now you can see a form field and no error in the console and when you type in the date, you can see Cleave.js into the action :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fb5a2fmsk0xkd49fee5ps.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fb5a2fmsk0xkd49fee5ps.png" alt="final view" width="800" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have only showed how to use Cleave.js with date format, learn other formats from here: &lt;a href="https://bit.ly/3WGTNtj" rel="noopener noreferrer"&gt;https://bit.ly/3WGTNtj&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
      <category>support</category>
      <category>help</category>
      <category>indonesia</category>
    </item>
  </channel>
</rss>
