<?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: Juan Manuel Crego Risso</title>
    <description>The latest articles on Forem by Juan Manuel Crego Risso (@juanmanuelcrego).</description>
    <link>https://forem.com/juanmanuelcrego</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%2F635522%2F35674468-adbd-4325-a65d-a286fb2cf05c.png</url>
      <title>Forem: Juan Manuel Crego Risso</title>
      <link>https://forem.com/juanmanuelcrego</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/juanmanuelcrego"/>
    <language>en</language>
    <item>
      <title>Input Mask in React without libraries</title>
      <dc:creator>Juan Manuel Crego Risso</dc:creator>
      <pubDate>Sat, 29 May 2021 03:30:30 +0000</pubDate>
      <link>https://forem.com/juanmanuelcrego/input-mask-in-react-without-libraries-5akf</link>
      <guid>https://forem.com/juanmanuelcrego/input-mask-in-react-without-libraries-5akf</guid>
      <description>&lt;h2&gt;
  
  
  How to create a simple input mask only with React:
&lt;/h2&gt;

&lt;p&gt;There are many libraries to do this, but it is always good to know how to do things that work in any type of web development. In this case we will create an entry for credit cards.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1:
&lt;/h3&gt;

&lt;p&gt;In a functional component we import&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect, useRef } from 'react';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can created the component, in this case, the name is &lt;code&gt;InputMask&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const InputMask = () =&amp;gt; {

}

export default InputMask;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2:
&lt;/h3&gt;

&lt;p&gt;We create first a new state, called &lt;code&gt;card&lt;/code&gt;, and &lt;code&gt;setCard&lt;/code&gt; is the function to modify that &lt;code&gt;state&lt;/code&gt;, later we create a &lt;code&gt;const&lt;/code&gt; called &lt;code&gt;inputCard&lt;/code&gt; for the &lt;code&gt;useRef&lt;/code&gt; hook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect, useRef } from "react";

const InputMask = () =&amp;gt; {
  const [card, setCard] = useState();
  const inputCard = useRef()
}

export default InputMask;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we return an input with a ref property binding the InputCard const&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect, useRef } from 'react';

const InputMask = () =&amp;gt; {
  const [card, setCard] = useState();
  const inputCard = useRef();

  return (
    &amp;lt;&amp;gt;
      &amp;lt;input type="text" ref={inputCard} /&amp;gt;
    &amp;lt;/&amp;gt;
  );
};

export default InputMask;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you do not know the hook &lt;code&gt;useRef&lt;/code&gt; I share the official React documentation for &lt;code&gt;useRef&lt;/code&gt; &lt;a href="https://reactjs.org/docs/hooks-reference.html#useref"&gt;in this link&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3:
&lt;/h3&gt;

&lt;p&gt;Now, we can target the events of the input, useRef works like a eventListener in JavaScript! For that, we create a function called &lt;code&gt;handleChange&lt;/code&gt; and pass this function to the input in a &lt;code&gt;onChange&lt;/code&gt; event&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect, useRef } from 'react';

const InputMask = () =&amp;gt; {
  const [card, setCard] = useState();
  const inputCard = useRef();

  const handleChange = () =&amp;gt; {

  };

  return (
    &amp;lt;&amp;gt;
      &amp;lt;input type="text" ref={inputCard} onChange={handleChange} /&amp;gt;
    &amp;lt;/&amp;gt;
  );
};

export default InputMask;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4:
&lt;/h3&gt;

&lt;p&gt;In &lt;code&gt;handleChange&lt;/code&gt; we use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"&gt;regex&lt;/a&gt; (Regular expressions) in a first step we use &lt;code&gt;replace&lt;/code&gt; to replace all the expressions that not numbers with a blank space, later we use &lt;code&gt;match&lt;/code&gt; for grouping the digits of the  credit card in four groups of four digits each one&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect, useRef } from 'react';

const InputMask = () =&amp;gt; {
  const [card, setCard] = useState();
  const inputCard = useRef();

  const handleChange = () =&amp;gt; {
    const cardValue = inputCard.current.value
      .replace(/\D/g, '')
      .match(/(\d{1,4})(\d{0,4})(\d{0,4})(\d{0,4})/);
    inputCard.current.value = !cardValue[2]
      ? cardValue[1]
      : `${cardValue[1]}-${cardValue[2]}
      ${(`${cardValue[3] ? `-${cardValue[3]}` : ''}`)}
      ${(`${cardValue[4] ? `-${cardValue[4]}` : ''}`)}`;
  };

  return (
    &amp;lt;&amp;gt;
      &amp;lt;input type="text" ref={inputCard} onChange={handleChange} /&amp;gt;
    &amp;lt;/&amp;gt;
  );
};

export default InputMask;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look at this, after &lt;code&gt;match&lt;/code&gt;, we use a&lt;code&gt;ternary operator&lt;/code&gt; to set the &lt;code&gt;value&lt;/code&gt; for&lt;code&gt;inputCard&lt;/code&gt; in a first step, we set the condition of the second group to false, because group one will always exist, in a second step , we write many conditions, at the beginning group one, then the second group will continue, then if group three exists it will be after a &lt;code&gt;-&lt;/code&gt;, the same is for group four...&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5:
&lt;/h3&gt;

&lt;p&gt;Finally, we use the &lt;code&gt;useEffect&lt;/code&gt; hook to manage the component lifecycle, inside useEffect, we set the callback for handleChange, and we specify the render when the &lt;code&gt;card state&lt;/code&gt; changes, for this, we use &lt;code&gt;setCard&lt;/code&gt; to save the input value on &lt;code&gt;card state&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect, useRef } from 'react';

const InputMask = () =&amp;gt; {
  const [card, setCard] = useState();
  const inputCard = useRef();

  const handleChange = () =&amp;gt; {
    const cardValue = inputCard.current.value
      .replace(/\D/g, '')
      .match(/(\d{0,4})(\d{0,4})(\d{0,4})(\d{0,4})/);
    inputCard.current.value = !cardValue[2]
      ? cardValue[1]
      : `${cardValue[1]}-${cardValue[2]}${`${
          cardValue[3] ? `-${cardValue[3]}` : ''
        }`}${`${cardValue[4] ? `-${cardValue[4]}` : ''}`}`;
    const numbers = inputCard.current.value.replace(/(\D)/g, '');
    setCard(numbers);
  };

  useEffect(() =&amp;gt; {
    handleChange();
  }, [card]);

  return (
    &amp;lt;&amp;gt;
      &amp;lt;input type="text" ref={inputCard} onChange={handleChange} /&amp;gt;
    &amp;lt;/&amp;gt;
  );
};

export default InputMask;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code works on phone inputs! in that case we must use &lt;code&gt;(xxx)&lt;/code&gt; in the beginning of the input.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://maskinput.stackblitz.io/"&gt;Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope you like this post!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
