tmarangon

⟵ voltar ao blog

Baixando qualquer música legalmente... ou quase isso.

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?

Vantagens de usar o qobuz-dl

Mão na massa

IMPORTANTE: 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.

Implementing the Key Up Handler

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:

  1. We check if the pressed key is either “ArrowUp” or “ArrowDown”. If not, we return early.
  2. We get the current target input element and determine the direction based on the pressed key (+1 for “ArrowUp”, -1 for “ArrowDown”).
  3. We parse the min, max, and step attributes of the input element using the parseNumericValue utility function.
  4. We parse the current value of the input using parseNumericValue or fallback to the min value or 0 if the input is empty.
  5. We calculate the increment based on the modifier keys: Ctrl/Cmd multiplies the step by 100, Shift multiplies it by 10, Alt divides it by 10, and the default is 1.
  6. We calculate the new value by adding the product of the direction and the increment to the current value, using the preciseRound utility function to handle floating-point precision.
  7. If the new value is different from the current value, we update the input value using the setInputValue function, ensuring it stays within the specified min and max range.
  8. Finally, we call 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!

⟵ voltar