slider icon indicating copy to clipboard operation
slider copied to clipboard

how to update value in slider when i change values ​​in input?

Open deantek opened this issue 1 year ago • 2 comments

image

gif issue: anim

component: ` const MyComponent = () => { const [value, setValue] = useState(1000000)

const min = 0
const max = 5000000

const handleChange = (e) => {
    let val = Math.max(min, Math.min(max, Number(e.target.value)))
    setValue(val)
}

return ( <>

          <input
            type="number"
            value={value}
            onChange={handleChange}
            onKeyUp={(e) => {
              let str = e.target.value
              str = str.replace(/\b0+/g, '')
              setValue(str)
            }}
          />
      
          <Slider
            min={min}
            max={max}
            step={10000}
            className={s.slider}
            onChange={(val) => setValue(val)}
            defaultValue={1000000}
          />
        </div>
      </>

) } `

I do not understand how to update the slider slider when the input changes, help pls

deantek avatar Oct 10 '22 04:10 deantek

Try inserting the prop value in the Slider component.

  <Slider
    min={min}
    max={max}
    step={10000}
    className={s.slider}
    onChange={(val) => setValue(val)}
    defaultValue={1000000}
    value={value}
  />

slobodanpecha23 avatar Oct 21 '22 08:10 slobodanpecha23

In order to determine the input range according to the input value, You need to assign a state value to a specific value.

like this


 const [value, setValue] = useState(100000) 

const handleChange = (e) => {
    let val = Math.max(min, Math.min(max, Number(e.target.value)))
    setValue(val)
}

return (
    <>
        <input
            type="number"
            value={value}
            onChange={handleChange}
            onKeyUp={(e) => {
              let str = e.target.value
              str = str.replace(/\b0+/g, '')
              setValue(str)
            }}
         />

          <Slider
            min={min}
            max={max}
            step={10000}
            className={s.slider}
            onChange={(val) => setValue(val)}
            defaultValue={1000000}
            value={value} //  👈👈👈 put it here state
          />
    </>

)

CaliforniaLuv avatar Oct 24 '22 03:10 CaliforniaLuv