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!
I'm going to be implementing this functionality with Go, a-h/Templ, Tailwind and my beloved Alpine.js to make life easy.
We start by writing a basic template for our integer spinbox:
templ IntSpinbox(name, label, value, tooltip string, saveinput bool, interval *IntInterval) { ... }
We define IntInterval as follows:
type IntInterval struct { A, B int }
With the interval, we will set min and max of the input. As we are making an integer spinbox, the step will always be set to '1'.
templ IntSpinbox(name, label, value, tooltip string, saveinput bool, interval *IntInterval) { }
Let's now start adding some tw classes, following are some special properties and pseudo-elements that control the rendering of the input.
select-none [-moz-user-select:none] [-ms-user-select:none] [-o-user-select:none] [-webkit-user-select:none]
The following extra classes are used to remove the default spinner buttons:
[&::-webkit-inner-spin-button]:[-webkit-appearance:none] [&::-webkit-outer-spin-button]:[-webkit-appearance:none] [-moz-appearance:textfield]
Finally, let's add some basic padding, ring, colors, etc...
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
Adding it to our template, we get the following:
templ IntSpinbox(name, label, value, tooltip string, saveinput bool, interval *IntInterval) { }
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.
The basic idea of an integer spinbox is of an input that only accepts integers. I initially attempted to implement this function by using HTML's pattern attribute as follows:
The pattern 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.
Every time the user presses any key inside the input box, a oninput event is generated. capture this event with Alpine's syntax x-on:input and rectify the value accordingly for the input element. Let's create a parent div with an x-data 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.
For those who don't know Alpine, $el here is used to refer to the current DOM element.
In the parent div created previously, we add the following class="flex" and add an x-ref="spinbox" attrib to the input so that our buttons can modify it's state through the magic property $refs.spinbox:
We then add a new child after the input, which will contain our buttons:
Here, we use flex-col-reverse as an easy way to keep the tab order correct, it should first tab to '-', then ' '.
We then add the event handlers to the buttons using x-on:click, the full code (excluding CSS) is as follows:
We have to convert e.value and e.step before doing any arithmetic as those are strings.
When it comes to CSS for the spinners buttons, they are styled very similarly to the input, the full code is below .
templ IntSpinbox(name, label, value, tooltip string, saveinput bool, interval *IntInterval) {@InputLabel(name, label " " interval.toString(), tooltip)}
Enjoy :)
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3