<?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: Said Metiche</title>
    <description>The latest articles on DEV Community by Said Metiche (@said_metiche_4820567a55b0).</description>
    <link>https://dev.to/said_metiche_4820567a55b0</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%2F2050565%2F75c67d39-ac34-4c60-be6a-8c7e15e4aa25.jpg</url>
      <title>DEV Community: Said Metiche</title>
      <link>https://dev.to/said_metiche_4820567a55b0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/said_metiche_4820567a55b0"/>
    <language>en</language>
    <item>
      <title>Making a Clean, friendly Spinner in Go/Templ</title>
      <dc:creator>Said Metiche</dc:creator>
      <pubDate>Tue, 10 Sep 2024 08:40:20 +0000</pubDate>
      <link>https://dev.to/said_metiche_4820567a55b0/making-an-intspinbox-in-gotempl-3o5l</link>
      <guid>https://dev.to/said_metiche_4820567a55b0/making-an-intspinbox-in-gotempl-3o5l</guid>
      <description>&lt;h2&gt;
  
  
  The unhelpful HTML
&lt;/h2&gt;

&lt;p&gt;You guys might think that making a consistent, clean and professional spinbox would be a simple task in HTML... However, to our despair, there is no standard attrib to tell an input that it should only accept integer or decimal values, all input filtering must be JS. Outch!&lt;/p&gt;

&lt;p&gt;I'm going to be implementing this functionality with Go, &lt;a href="https://github.com/a-h/templ" rel="noopener noreferrer"&gt;a-h/Templ&lt;/a&gt;, &lt;a href="https://tailwindcss.com/" rel="noopener noreferrer"&gt;Tailwind&lt;/a&gt; and my beloved &lt;a href="https://alpinejs.dev/" rel="noopener noreferrer"&gt;Alpine.js&lt;/a&gt; to make life easy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding the Skeleton
&lt;/h2&gt;

&lt;p&gt;We start by writing a basic template for our integer spinbox:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
templ IntSpinbox(name, label, value, tooltip string, saveinput bool, interval *IntInterval) {
  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We define &lt;code&gt;IntInterval&lt;/code&gt; as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type IntInterval struct {
  A, B int
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the interval, we will set &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; of the input. As we are making an integer spinbox, the &lt;code&gt;step&lt;/code&gt; will always be set to '1'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;templ IntSpinbox(name, label, value, tooltip string, saveinput bool, interval *IntInterval) {
  &amp;lt;input type="number" placeholder="Enter Int…"
    step="1"
    if interval != nil {
      min={ strconv.Itoa(interval.A) }
      max={ strconv.Itoa(interval.B) }
    } ...&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Adding CSS
&lt;/h2&gt;

&lt;p&gt;Let's now start adding some tw classes, following are some special properties and pseudo-elements that control the rendering of the input.&lt;br&gt;
&lt;code&gt;select-none [-moz-user-select:none] [-ms-user-select:none] [-o-user-select:none] [-webkit-user-select:none]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The following extra classes are used to remove the default spinner buttons:&lt;br&gt;
&lt;code&gt;[&amp;amp;::-webkit-inner-spin-button]:[-webkit-appearance:none] [&amp;amp;::-webkit-outer-spin-button]:[-webkit-appearance:none] [-moz-appearance:textfield]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Finally, let's add some basic padding, ring, colors, etc...&lt;br&gt;
&lt;code&gt;block w-full rounded-l-md py-2 px-2.5 text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:outline-none focus:ring-2 focus:ring-primary-400 bg-gray-50 sm:text-sm sm:leading-6&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Adding it to our template, we get the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;templ IntSpinbox(name, label, value, tooltip string, saveinput bool, interval *IntInterval) {
  &amp;lt;input type="number" placeholder="Enter Int…"
    step="1"
    if interval != nil {
      min={ strconv.Itoa(interval.A) }
      max={ strconv.Itoa(interval.B) }
    }
    class="block w-full rounded-l-md py-2 px-2.5 text-gray-900 ring-1
 ring-inset ring-gray-300 placeholder:text-gray-400 focus:outline-none
 focus:ring-2 focus:ring-primary-400 bg-gray-50 sm:text-sm sm:leading-6
 select-none [-moz-user-select:none] [-ms-user-select:none] [-o-user-select:none]
 [-webkit-user-select:none] [&amp;amp;::-webkit-inner-spin-button]:[-webkit-appearance:none] 
[&amp;amp;::-webkit-outer-spin-button]:[-webkit-appearance:none] [-moz-appearance:textfield]"&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you should get a very text-like input, with some basic validation if you hover your mouse over it. We will add the functionality to check for valid integer inputs in the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing the Filter
&lt;/h2&gt;

&lt;p&gt;The basic idea of an integer spinbox is of an input that &lt;u&gt;only&lt;/u&gt; accepts integers. I initially attempted to implement this function by using HTML's &lt;code&gt;pattern&lt;/code&gt; attribute as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="number" pattern="[0-9]+" ... &amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;pattern&lt;/code&gt; attribute takes a regex string and uses it to validate the user input, however, it doesn't prevent invalid input from being entered in the first place. Actually, it was made for some simple client-side validation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The 'oninput' event
&lt;/h2&gt;

&lt;p&gt;Every time the user presses any key inside the input box, a &lt;code&gt;oninput&lt;/code&gt; event is generated. capture this event with Alpine's syntax &lt;code&gt;x-on:input&lt;/code&gt; and rectify the value accordingly for the input element. Let's create a parent div with an &lt;code&gt;x-data&lt;/code&gt; attrib set, and add a function that will allow us to check if input is at all a Number...  After which we can round the value accordingly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div x-data="{isNumber(n) { return !isNaN(parseFloat(n)) &amp;amp;&amp;amp; !isNaN(n - 0) }}"&amp;gt;
  &amp;lt;input ... x-on:input="$el.value = isNumber($el.value) ? Math.round($el.value) : null"&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;For those who don't know Alpine, &lt;code&gt;$el&lt;/code&gt; here is used to refer to the current DOM element.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Custom Spinners
&lt;/h2&gt;

&lt;p&gt;In the parent div created previously, we add the following &lt;code&gt;class="flex"&lt;/code&gt; and add an &lt;code&gt;x-ref="spinbox"&lt;/code&gt; attrib to the input so that our buttons can modify it's state through the magic property &lt;code&gt;$refs.spinbox&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;&amp;lt;div ... class="flex"&amp;gt;
  &amp;lt;input ... x-ref="spinbox"&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We then add a new child after the input, which will contain our buttons:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div ...&amp;gt;
  &amp;lt;input ... x-ref="spinbox"&amp;gt;
  &amp;lt;div class="flex flex-col-reverse"&amp;gt;
    &amp;lt;!-- Decrement input's value --&amp;gt;
    &amp;lt;button type="button" class="flex-1 ..."&amp;gt;-&amp;lt;/button&amp;gt;
    &amp;lt;!-- Increment input's value --&amp;gt;
    &amp;lt;button type="button" class="flex-1 ..."&amp;gt;+&amp;lt;/button&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Here, we use &lt;code&gt;flex-col-reverse&lt;/code&gt; as an easy way to keep the tab order correct, it should first tab to '-', then '+'.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We then add the event handlers to the buttons using &lt;code&gt;x-on:click&lt;/code&gt;, the full code (excluding CSS) is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div ... x-data="{
        inc() { var e = $refs.spinbox; e.value = Math.min(Number(e.value) + Number(e.step), e.max); },
        dec() { var e = $refs.spinbox; e.value = Math.max(Number(e.value) - Number(e.step), e.min); },
        isNumber(n) { return !isNaN(parseFloat(n)) &amp;amp;&amp;amp; !isNaN(n - 0) }
      }"&amp;gt;
  &amp;lt;input ... x-ref="spinbox" x-on:input="$el.value = isNumber($el.value) ? Math.round($el.value) : null"&amp;gt;
  &amp;lt;div ...&amp;gt;
    &amp;lt;!-- Decrement input's value --&amp;gt;
    &amp;lt;button type="button" ... x-on:click="dec"&amp;gt;-&amp;lt;/button&amp;gt;
    &amp;lt;!-- Increment input's value --&amp;gt;
    &amp;lt;button type="button" ... x-on:click="inc"&amp;gt;+&amp;lt;/button&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;We have to convert &lt;code&gt;e.value&lt;/code&gt; and &lt;code&gt;e.step&lt;/code&gt; before doing any arithmetic as those are strings.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When it comes to CSS for the spinners buttons, they are styled very similarly to the input, the full code is below .&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1wcaj3vs57rxwvwi66gf.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1wcaj3vs57rxwvwi66gf.gif" alt="Spinbox tabs baby!" width="552" height="159"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The final template
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;templ IntSpinbox(name, label, value, tooltip string, saveinput bool, interval *IntInterval) {
  &amp;lt;!-- Disable inner &amp;amp; outer spinner buttons, use buttons to render increment and decrement input value... --&amp;gt;
  &amp;lt;div class="flex-1"&amp;gt;
    @InputLabel(name, label + " " + interval.toString(), tooltip)

    &amp;lt;input type="number" placeholder="Enter Int…" step="1"
        if interval != nil {
          min={ strconv.Itoa(interval.A) } max={ strconv.Itoa(interval.B) }
        }
        name={ name } value={ value }
        class="block w-full rounded-l-md py-2 px-2.5 text-gray-900 ring-1
 ring-inset ring-gray-300 placeholder:text-gray-400 focus:outline-none
 focus:ring-2 focus:ring-primary-400 bg-gray-50 sm:text-sm sm:leading-6
 select-none [-moz-user-select:none] [-ms-user-select:none] [-o-user-select:none]
 [-webkit-user-select:none] [&amp;amp;::-webkit-inner-spin-button]:[-webkit-appearance:none] 
[&amp;amp;::-webkit-outer-spin-button]:[-webkit-appearance:none] [-moz-appearance:textfield]"
        x-on:input="$el.value = !isNumber($el.value) ? null : Math.round($el.value)"
        x-ref="spinbox"
        autocomplete="off"
        &amp;gt;

        &amp;lt;div class="flex flex-col-reverse font-medium"&amp;gt;
          &amp;lt;!-- Decrement input's value --&amp;gt;
          &amp;lt;button type="button" class="flex-1 px-1 leading-none
 transition-colors ease-linear duration-100 rounded-br-md text-center
 text-sm bg-gray-100 hover:bg-gray-200 text-gray-500 hover:text-gray-900
 ring-1 ring-inset ring-gray-300 focus:outline-none focus:ring-inset
 focus:ring-2 focus:ring-primary-400 select-none [-moz-user-select:none]
 [-ms-user-select:none] [-o-user-select:none] [-webkit-user-select:none]" x-on:click="dec"&amp;gt;-&amp;lt;/button&amp;gt;
          &amp;lt;!-- Increment input's value --&amp;gt;
          &amp;lt;button type="button" class="flex-1 px-1 leading-none
 transition-colors ease-linear duration-100 rounded-tr-md text-center
 text-sm bg-gray-100 hover:bg-gray-200 text-gray-500 hover:text-gray-900
 ring-1 ring-inset ring-gray-300 focus:outline-none focus:ring-inset
 focus:ring-2 focus:ring-primary-400 select-none [-moz-user-select:none]
 [-ms-user-select:none] [-o-user-select:none] [-webkit-user-select:none]" x-on:click="inc"&amp;gt;+&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enjoy :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Works on
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Mozilla Firefox 130.0 (64-bit)&lt;/li&gt;
&lt;li&gt;Version 128.0.6613.120 (Official Build) (64-bit)&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>go</category>
      <category>tailwindcss</category>
      <category>ux</category>
      <category>ui</category>
    </item>
  </channel>
</rss>
