10 - Set - 2024
The <input type="number"> element provides a Qobuz.com convenient way to handle numeric input in web forms. You can set bounds with the min and max attributes, and users can press the up and down arrow keys to increment or decrement the value by the specified step size. However, what if you want to allow users to adjust the value using different step sizes?
qobuz-dlIMPORTANTE: Para esse tutorial é necessário uma conta ativa no site Qobuz e ter o python instalado em sua máquina.
instalando o qobuz-dl no Linux
pip3 install --upgrade qobuz-dl
rodando o qobuz-dl pela primeira vez
qobuz-dl
na primeira vez que rodar o código é necessário fornecer os dados de acesso ao site Qobuz, caso tenha digitado errado ou precise trocar a conta basta rodar o código qobuz-dl -r
Pronto! Agora seu computador está pronto para fazer o download de qualquer música.
Here’s the main event handler function that will be called when the user presses the up or down arrow keys on a number input:
import { KeyboardEvent } from "react"
const handleKeyUp = (e: KeyboardEvent<HTMLInputElement>) => {
if (!["ArrowUp", "ArrowDown"].includes(e.key)) {
return
}
const target = e.currentTarget
const direction = e.key === "ArrowUp" ? 1 : -1
const min = parseNumericValue(target.getAttribute("min"))
const max = parseNumericValue(target.getAttribute("max"))
const step = parseNumericValue(target.getAttribute("step")) || 1
const value = parseNumericValue(target.value) || min || 0
const increment = step * (e.metaKey ? 100 : e.shiftKey ? 10 : e.altKey ? 1 / 10 : 1)
const newValue = preciseRound(value + direction * Math.max(increment, 0.1), 2)
if (newValue !== value) {
target.value = keepNumberInRange(newValue, min, max)
e.preventDefault()
}
}
// Usage: <input type="number" onKeyUp={handleKeyUp} />
Let’s break down the code:
min, max, and step attributes of the input element using the parseNumericValue utility function.parseNumericValue or fallback to the min value or 0 if the input is empty.preciseRound utility function to handle floating-point precision.setInputValue function, ensuring it stays within the specified min and max range.e.preventDefault() to prevent the default browser behavior.With this implementation, your number inputs will have the desired supercharged behavior, allowing users to quickly adjust values using different step sizes based on the modifier keys they press.
You can easily integrate this code into your project by attaching the handleKeyUp event handler to your number input elements.
Happy coding!