Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

427
Visualizações
Prevent range slider from resetting if user clears input

I have a range slider with input boxes. So far I have it mostly working but I need the thumb sliders to not reset in the middle if the user clears the input from the boxes. If a user clears the input I want the slider to stay at their current position.

Sliders reset in the middle if user clears and input

Slider reset

Slider reset max value

Here is the CodeSandbox

over 4 years ago · Santiago Trujillo
4 Respostas
Responde à pergunta

0

You can control the values of input and slide separately, that will be much more convenient and conforms to React's state control.

Code fragment sample

    const [min, setMin] = useState({ input: 0, slide: 0 });
    const [max, setMax] = useState({ input: 100, slide: 100 });

    const handleMaxChange = (e) => {
    const v = parseInt(e.target.value, 10);
    if (v) {
      if (v < min.input && !!min.input) {
        setMax({ input: min.input + 1, slide: min.slide + 1 });
      } else if (v > extremeMax) {
        setMax({ input: extremeMax, slide: extremeMax });
      } else {
        setMax({ input: v, slide: v });
      }
    } else {
      setMax((prev) => ({ ...prev, input: "" }));
    }
  };

Full code sample

Edit TextField selectionStart

over 4 years ago · Santiago Trujillo Relatório

0

it's not clear to me what you really want to do. Because let's say Min value is 40, and user clears the textbox with backspaces: first time, it's 4, the second time backspace is hit it will be empty. Do you want the slider stay at 40 or 4?

in any case, you can hold the value for previous state of min/max and if the new value of textbox is empty, don't update the slider state.

over 4 years ago · Santiago Trujillo Relatório

0

You can simply setup the slider like this:

<MultiRangeSlider
      minVal={min ? min : 0}
      maxVal={max ? max : 0}
      ...
/>

Demo on CodeSandbox.

over 4 years ago · Santiago Trujillo Relatório

0

This is a bit of an advanced solution, but you can use debouce from lodash (or code your own debounce function, but use packages instead)

What you can do is have a debounce on the user input for the sliders On the 'onChange' for your input component, debounce updating the slider value in your state, and have a check that if the input is null/undefined/empty, then don't update the state :)

I'm taking a break at work, but if you need me to provide exact code, I can do so ^-^

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda