React, как сделать слайдер(бегунок)?

Добрый день не могу понять две вещи:
1. Как сделать перетаскивания бегунка, сейчас это реализовано с помощью клика. То есть один раз кликнули - бегунок переместился. Зна. что нужно использовать onMouseDown, но как будто происходит простой клик;
2. Как по клику переместиться допустим на значение 2 или 6 и т.д
class Slider extends Component {

    constructor(props) {
        super(props);
        this.state = {
            value: this.props.value
        };
        this.changeValue = this.changeValue.bind(this);


    }

    changeValue(e) {
        console.log(e.target.pageY);
        let stage = 100/this.props.elements;
        let line = document.querySelector(".ui-slider-range.ui-corner-all.ui-widget-header.ui-slider-range-min" + `.${this.props.class}`);
        let slider = document.querySelector(".ui-slider-handle.ui-corner-all.ui-state-default" + `.${this.props.class}`);
        if (this.state.value < this.props.max) {
            this.setState({value: this.props.step + this.state.value});

            let numberPercentageLine = parseInt(line.style.width) + stage;
            console.log(numberPercentageLine);
            numberPercentageLine.toString();
            console.log(numberPercentageLine);
            line.style.width = numberPercentageLine + "%";
            console.log(line.style.width);

            let numberPercentageSlider = parseInt(slider.style.left) + stage;
            numberPercentageSlider.toString();
            slider.style.left = numberPercentageSlider + "%";

        } else {
            return null;
        }
    }

    render () {
        return (
            <div className="sliderblock">
                <div className="row no-gutters align-items-center">
                    <div className="col-auto">
                        <div className="sliderblock__min">{this.props.min}</div>
                    </div>
                    <div className="col">
                        <div className="slider-rangeblock" onMouseDown={this.changeValue}>
                            <div
                                className="slider-range ui-slider ui-corner-all ui-slider-horizontal ui-widget ui-widget-content"
                            >
                                <div className={ `ui-slider-range ui-corner-all ui-widget-header ui-slider-range-min ${this.props.class}`} style={{width: "0%"}}>
                                </div>
                                <span className={ `ui-slider-handle ui-corner-all ui-state-default ${this.props.class}`} style={{left: "0%"}}>
                                </span>
                            </div>

                        </div>
                    </div>
                    <div className="col-auto">
                        <div className="sliderblock__cur">
                            <span className="sliderblock__cur-val">
                                {
                                    this.props.specChar !== undefined ?
                                        `${this.props.specChar}${this.state.value}` : `${this.state.value}`
                                }
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        )
    }

}

export default Slider;


Принимает параметры
    <Slider
              min={0}
              value={0}
              max={10}
              step={1}
              elements={10}
              class={"skill"}
    />
  • Вопрос задан
  • 824 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы