范围滑块:在移动设备Chrome上无法预防被动事件侦听器的侦听器

发布于 2025-01-31 12:11:39 字数 7349 浏览 1 评论 0原文

我正在使用基本的HTML范围输入来构建价格范围滑块,但是在使用React/Next JS应用程序检查同样的Mobile设备上时,获取错误React> react> react-dom.development.js?61BB:6202无法预防被动事件内部的默认事件侦听器调用。

Priceslider.js主要组件代码为:

function PriceSlider({ handleChange, currencyDetails, queryParams }) {
    const [stateMinVal, setStateMinVal] = useState(0);
    const [stateMaxVal, setStateMaxVal] = useState(99999);
    const [inputMinVal, setInputMinVal] = useState(0);
    const [inputMaxVal, setInputMaxVal] = useState(99999);
    const [rangeError, setRangeError] = useState(false);
    let priceGap = 1;
    let { convertToCurrency = "", ipDetails = {} } = currencyDetails || {};

    const handlePriceInput = (e) => {
      e.preventDefault();
      let rangeInput = document.querySelectorAll(".range-input input"),
      priceInput = document.querySelectorAll(".price-input input"),
      range = document.querySelector(".slider .progress");
      let minPrice = priceInput[0].value ? parseFloat(priceInput[0].value) : priceInput[0].value,
      maxPrice = priceInput[1].value ? parseFloat(priceInput[1].value) : priceInput[1].value;
      
      if((minPrice > 0 && maxPrice > 0)){
        if ((maxPrice - minPrice >= priceGap) && maxPrice <= rangeInput[1].max) {
          setRangeError(false);
            if (e.target.className === "input-min") {
                // rangeInput[0].value = minPrice;
                setStateMinVal(minPrice);
                setInputMinVal(minPrice);
                range.style.left = ((minPrice / rangeInput[0].max) * 100) + "%";
            } else {
                // rangeInput[1].value = maxPrice;
                setStateMaxVal(maxPrice);
                setInputMaxVal(maxPrice);
                range.style.right = 100 - (maxPrice / rangeInput[1].max) * 100 + "%";
            }
            // apply price filter
            let minIn = getCurrencySymbol(convertToCurrency, true)+" "+minPrice;
            let maxIn = getCurrencySymbol(convertToCurrency, true)+" "+maxPrice;
            console.log(maxIn, minIn, convertToCurrency);
            let filterId = minIn+" - "+maxIn;
            setTimeout(() => handleChange({filterId: filterId , checked: true, filterType: 'range'}), 500);
            // ---- //
        }else{
          if(minPrice >= maxPrice){
            setRangeError(true);
            setInputMinVal(minPrice);
            setInputMaxVal(maxPrice);
          }else if(maxPrice >= rangeInput[1].max){
            // apply price filter
            let minIn = getCurrencySymbol(convertToCurrency, true)+" "+minPrice;
            let maxIn = getCurrencySymbol(convertToCurrency, true)+" "+maxPrice;
            let filterId = minIn+" - "+maxIn;
            setTimeout(() => handleChange({filterId: filterId , checked: true, filterType: 'range'}), 500);
            // ------ //
            setStateMinVal(minPrice);
            setStateMaxVal(rangeInput[1].max);
            
            setInputMinVal(minPrice);
            setInputMaxVal(rangeInput[1].max);
          }
        }
      }else if(minPrice == "" || maxPrice == ""){
        setInputMinVal(minPrice);
        setInputMaxVal(maxPrice);
      }
    }

    const handleRangeInput = (e) => {
      e.preventDefault();
      let rangeInput = document.querySelectorAll(".range-input input"),
      priceInput = document.querySelectorAll(".price-input input"),
      range = document.querySelector(".slider .progress");
      let minVal = parseFloat(rangeInput[0].value),
      maxVal = parseFloat(rangeInput[1].value);
      
      let diffMinMax = rangeInput[0].max - rangeInput[1].min;
      
      if ((maxVal - minVal) < priceGap) {
          if (e.target.className === "range-min") {
              rangeInput[0].value = maxVal - priceGap
          } else{
              rangeInput[1].value = minVal + priceGap;
          }
      } else {
          priceInput[0].value = minVal;
          priceInput[1].value = maxVal;
          setStateMinVal(minVal);
          setStateMaxVal(maxVal);
          range.style.left = (minVal - rangeInput[0].min) / diffMinMax * 100 + "%";
          range.style.right = (rangeInput[0].max - maxVal) / diffMinMax * 100 + "%";
          // apply price filter
          let minIn = getCurrencySymbol(convertToCurrency, true)+" "+minVal;
          let maxIn = getCurrencySymbol(convertToCurrency, true)+" "+maxVal;
          let filterId = minIn+" - "+maxIn;
          setTimeout(() => handleChange({filterId: filterId , checked: true, filterType: 'range'}), 500);
          // handleChange({filterId: filterId , checked: true, filterType: 'range'});
      }
    }

    useEffect(() => {
      let rangeInput = document.querySelectorAll(".range-input input");
      let priceInput = document.querySelectorAll(".price-input input");
      let range = document.querySelector(".slider .progress");
      priceInput.forEach(input => {
          input.addEventListener("input", handlePriceInput);
      });

      rangeInput.forEach(input => {
          input.addEventListener("input", handleRangeInput);
      });

      priceInput[0].value = stateMinVal;
      priceInput[1].value = stateMaxVal;

      if(queryParams && queryParams['range']){
        let symbol = getCurrencySymbol(convertToCurrency, true);
        let value = queryParams['range'];
        let min = value.split('-')[0].split(symbol)[1]?.trim();
        let max = value.split('-')[1].split(symbol)[1]?.trim();
        console.log('minmax', min, max, symbol)
        setStateMinVal(min);
        setStateMaxVal(max);
        
        setInputMinVal(min);
        setInputMaxVal(max);
        let diffMinMax = rangeInput[0].max - rangeInput[1].min;
        range.style.left = (min - rangeInput[0].min) / diffMinMax * 100 + "%";
        range.style.right = (rangeInput[0].max - max) / diffMinMax * 100 + "%";
      }

      return () => {
        console.log('m here')
        priceInput.forEach(input => {
            input.removeEventListener("input", handlePriceInput);
        });

        rangeInput.forEach(input => {
            input.removeEventListener("input", handleRangeInput);
        });
      }

      //Init max-price input fields
      // handleRangeInput();
    }, []);

    return (
      <div class="price_wrapper">
        <div class="slider">
          <div class="progress"></div>
        </div>
      
        <div class="range-input">
          <input type="range" class="range-min" min="0" max="99999" defaultValue={stateMinVal} step="1" />
          <input type="range" class="range-max" min="0" max="99999" defaultValue={stateMaxVal} step="1" />
        </div>
      
        <div class="price-input">
          <div class="field">
            <input type="number" class="input-min" defaultValue={inputMinVal} />
          </div>
          <div class="field">
            <input type="number" class="input-max" defaultValue={inputMaxVal} />
          </div>
        </div>
        {rangeError && <div className="qa-text-error">
          Please add an appropriate MIN - MAX value
        </div>}
      </div>
    )
  }

在将EventListener添加到元素时尝试添加{valsive:false},但这也不起来。 任何建议/信息都将在这里有很大的帮助。 提前致谢 :)

I am building a price range slider using basic HTML range input but while checking the same with react/next JS application on mobile device, getting the error react-dom.development.js?61bb:6202 Unable to preventDefault inside passive event listener invocation.

PriceSlider.js where the main component code is:

function PriceSlider({ handleChange, currencyDetails, queryParams }) {
    const [stateMinVal, setStateMinVal] = useState(0);
    const [stateMaxVal, setStateMaxVal] = useState(99999);
    const [inputMinVal, setInputMinVal] = useState(0);
    const [inputMaxVal, setInputMaxVal] = useState(99999);
    const [rangeError, setRangeError] = useState(false);
    let priceGap = 1;
    let { convertToCurrency = "", ipDetails = {} } = currencyDetails || {};

    const handlePriceInput = (e) => {
      e.preventDefault();
      let rangeInput = document.querySelectorAll(".range-input input"),
      priceInput = document.querySelectorAll(".price-input input"),
      range = document.querySelector(".slider .progress");
      let minPrice = priceInput[0].value ? parseFloat(priceInput[0].value) : priceInput[0].value,
      maxPrice = priceInput[1].value ? parseFloat(priceInput[1].value) : priceInput[1].value;
      
      if((minPrice > 0 && maxPrice > 0)){
        if ((maxPrice - minPrice >= priceGap) && maxPrice <= rangeInput[1].max) {
          setRangeError(false);
            if (e.target.className === "input-min") {
                // rangeInput[0].value = minPrice;
                setStateMinVal(minPrice);
                setInputMinVal(minPrice);
                range.style.left = ((minPrice / rangeInput[0].max) * 100) + "%";
            } else {
                // rangeInput[1].value = maxPrice;
                setStateMaxVal(maxPrice);
                setInputMaxVal(maxPrice);
                range.style.right = 100 - (maxPrice / rangeInput[1].max) * 100 + "%";
            }
            // apply price filter
            let minIn = getCurrencySymbol(convertToCurrency, true)+" "+minPrice;
            let maxIn = getCurrencySymbol(convertToCurrency, true)+" "+maxPrice;
            console.log(maxIn, minIn, convertToCurrency);
            let filterId = minIn+" - "+maxIn;
            setTimeout(() => handleChange({filterId: filterId , checked: true, filterType: 'range'}), 500);
            // ---- //
        }else{
          if(minPrice >= maxPrice){
            setRangeError(true);
            setInputMinVal(minPrice);
            setInputMaxVal(maxPrice);
          }else if(maxPrice >= rangeInput[1].max){
            // apply price filter
            let minIn = getCurrencySymbol(convertToCurrency, true)+" "+minPrice;
            let maxIn = getCurrencySymbol(convertToCurrency, true)+" "+maxPrice;
            let filterId = minIn+" - "+maxIn;
            setTimeout(() => handleChange({filterId: filterId , checked: true, filterType: 'range'}), 500);
            // ------ //
            setStateMinVal(minPrice);
            setStateMaxVal(rangeInput[1].max);
            
            setInputMinVal(minPrice);
            setInputMaxVal(rangeInput[1].max);
          }
        }
      }else if(minPrice == "" || maxPrice == ""){
        setInputMinVal(minPrice);
        setInputMaxVal(maxPrice);
      }
    }

    const handleRangeInput = (e) => {
      e.preventDefault();
      let rangeInput = document.querySelectorAll(".range-input input"),
      priceInput = document.querySelectorAll(".price-input input"),
      range = document.querySelector(".slider .progress");
      let minVal = parseFloat(rangeInput[0].value),
      maxVal = parseFloat(rangeInput[1].value);
      
      let diffMinMax = rangeInput[0].max - rangeInput[1].min;
      
      if ((maxVal - minVal) < priceGap) {
          if (e.target.className === "range-min") {
              rangeInput[0].value = maxVal - priceGap
          } else{
              rangeInput[1].value = minVal + priceGap;
          }
      } else {
          priceInput[0].value = minVal;
          priceInput[1].value = maxVal;
          setStateMinVal(minVal);
          setStateMaxVal(maxVal);
          range.style.left = (minVal - rangeInput[0].min) / diffMinMax * 100 + "%";
          range.style.right = (rangeInput[0].max - maxVal) / diffMinMax * 100 + "%";
          // apply price filter
          let minIn = getCurrencySymbol(convertToCurrency, true)+" "+minVal;
          let maxIn = getCurrencySymbol(convertToCurrency, true)+" "+maxVal;
          let filterId = minIn+" - "+maxIn;
          setTimeout(() => handleChange({filterId: filterId , checked: true, filterType: 'range'}), 500);
          // handleChange({filterId: filterId , checked: true, filterType: 'range'});
      }
    }

    useEffect(() => {
      let rangeInput = document.querySelectorAll(".range-input input");
      let priceInput = document.querySelectorAll(".price-input input");
      let range = document.querySelector(".slider .progress");
      priceInput.forEach(input => {
          input.addEventListener("input", handlePriceInput);
      });

      rangeInput.forEach(input => {
          input.addEventListener("input", handleRangeInput);
      });

      priceInput[0].value = stateMinVal;
      priceInput[1].value = stateMaxVal;

      if(queryParams && queryParams['range']){
        let symbol = getCurrencySymbol(convertToCurrency, true);
        let value = queryParams['range'];
        let min = value.split('-')[0].split(symbol)[1]?.trim();
        let max = value.split('-')[1].split(symbol)[1]?.trim();
        console.log('minmax', min, max, symbol)
        setStateMinVal(min);
        setStateMaxVal(max);
        
        setInputMinVal(min);
        setInputMaxVal(max);
        let diffMinMax = rangeInput[0].max - rangeInput[1].min;
        range.style.left = (min - rangeInput[0].min) / diffMinMax * 100 + "%";
        range.style.right = (rangeInput[0].max - max) / diffMinMax * 100 + "%";
      }

      return () => {
        console.log('m here')
        priceInput.forEach(input => {
            input.removeEventListener("input", handlePriceInput);
        });

        rangeInput.forEach(input => {
            input.removeEventListener("input", handleRangeInput);
        });
      }

      //Init max-price input fields
      // handleRangeInput();
    }, []);

    return (
      <div class="price_wrapper">
        <div class="slider">
          <div class="progress"></div>
        </div>
      
        <div class="range-input">
          <input type="range" class="range-min" min="0" max="99999" defaultValue={stateMinVal} step="1" />
          <input type="range" class="range-max" min="0" max="99999" defaultValue={stateMaxVal} step="1" />
        </div>
      
        <div class="price-input">
          <div class="field">
            <input type="number" class="input-min" defaultValue={inputMinVal} />
          </div>
          <div class="field">
            <input type="number" class="input-max" defaultValue={inputMaxVal} />
          </div>
        </div>
        {rangeError && <div className="qa-text-error">
          Please add an appropriate MIN - MAX value
        </div>}
      </div>
    )
  }

Tried with adding {passive: false} while adding eventlistener to elements but that didn’t work either.
Any suggestion/info will be of great help here.
Thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文