shineout
Version:
Shein 前端组件库
180 lines (157 loc) • 6.21 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { PureComponent } from 'react';
import classnames from 'classnames';
import { isRTL } from '../config';
import { sliderClass } from './styles';
import Slider from './Slider';
import { per2value } from './utils';
import getDataset from '../utils/dom/getDataset';
import { getDirectionClass } from '../utils/classname';
import { isFunc, isArray } from '../utils/is';
var DefaultValue = {
height: 200,
scale: [0, 100],
step: 1,
vertical: false,
formatScale: function formatScale(v) {
return v;
}
};
var Container =
/*#__PURE__*/
function (_PureComponent) {
_inheritsLoose(Container, _PureComponent);
function Container(props) {
var _this;
_this = _PureComponent.call(this, props) || this;
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "innerElement", void 0);
_this.state = {};
_this.bindElement = _this.bindElement.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.handleClick = _this.handleClick.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.handleChange = _this.handleChange.bind(_assertThisInitialized(_assertThisInitialized(_this))); // this.handleFirstDrag = this.handleDrag.bind(this, 0)
// this.handleSecondDrag = this.handleDrag.bind(this, 1)
return _this;
}
var _proto = Container.prototype;
_proto.getValue = function getValue() {
var _this$props = this.props,
range = _this$props.range,
value = _this$props.value,
_this$props$scale = _this$props.scale,
scale = _this$props$scale === void 0 ? DefaultValue.scale : _this$props$scale;
var from = scale[0];
if (!range) return value || from;
var val = value;
if (range && !isArray(value)) val = [from, from];
if (isArray(val) && val[0] > val[1]) val = [val[1], val[0]];
return val;
};
_proto.bindElement = function bindElement(el) {
this.innerElement = el;
};
_proto.handleClick = function handleClick(e) {
if (e.target.className.indexOf(sliderClass('indicator')) >= 0) return;
if (this.props.disabled) return;
var _this$props2 = this.props,
_this$props2$scale = _this$props2.scale,
scale = _this$props2$scale === void 0 ? DefaultValue.scale : _this$props2$scale,
_this$props2$step = _this$props2.step,
step = _this$props2$step === void 0 ? DefaultValue.step : _this$props2$step,
vertical = _this$props2.vertical,
range = _this$props2.range;
var rect = this.innerElement.getBoundingClientRect();
var per = vertical ? 1 - (e.clientY - rect.top) / rect.height : (e.clientX - rect.left) / rect.width;
if (isRTL() && !vertical) {
per = 1 - per;
}
var val = per2value(per, scale, step);
if (!range) {
this.props.onChange(val);
return;
}
var values = this.getValue();
if (!isArray(values)) return;
var value = [].concat(values);
if (val < value[0]) value[0] = val;else value[1] = val;
this.props.onChange(value);
};
_proto.handleChange = function handleChange(index, val) {
var range = this.props.range;
if (!range) {
this.props.onChange(val);
return;
}
var values = this.getValue();
if (!isArray(values)) return;
var value = [].concat(values);
value[index] = val;
this.props.onChange(value);
} // handleDrag(index, value) {
// const { range } = this.props
// if (!range) this.props.onDrag(value)
// }
;
_proto.renderScale = function renderScale() {
var _this$props3 = this.props,
autoHide = _this$props3.autoHide,
formatScale = _this$props3.formatScale,
_this$props3$scale = _this$props3.scale,
scale = _this$props3$scale === void 0 ? DefaultValue.scale : _this$props3$scale;
if (!formatScale || !isFunc(formatScale)) return null;
return React.createElement("div", {
className: sliderClass(getDirectionClass('scale'), !autoHide && 'show')
}, scale.map(function (s, i) {
return React.createElement("div", {
key: s
}, React.createElement("span", null, formatScale(s, i)));
}));
};
_proto.render = function render() {
var _this$props4 = this.props,
range = _this$props4.range,
height = _this$props4.height,
style = _this$props4.style,
vertical = _this$props4.vertical,
_this$props4$scale = _this$props4.scale,
scale = _this$props4$scale === void 0 ? DefaultValue.scale : _this$props4$scale,
other = _objectWithoutPropertiesLoose(_this$props4, ["range", "height", "style", "vertical", "scale"]);
var className = classnames(sliderClass('_', vertical && 'vertical', this.props.disabled && 'disabled', isRTL() && 'rtl'), this.props.className);
var value = this.getValue();
if (!Array.isArray(value)) value = [0, value];
var newStyle = style;
if (vertical) newStyle = Object.assign({
height: height
}, style);
return React.createElement("div", _extends({
style: newStyle,
className: className
}, getDataset(other)), React.createElement("div", {
className: sliderClass('background')
}), React.createElement("div", {
ref: this.bindElement,
onClick: this.handleClick,
className: sliderClass('inner')
}, range && React.createElement(Slider, _extends({}, other, {
index: 0,
scale: scale,
max: value[1],
onChange: this.handleChange,
value: value[0],
vertical: vertical
})), React.createElement(Slider, _extends({}, other, {
index: 1,
scale: scale,
min: value[0],
onChange: this.handleChange,
value: value[1],
vertical: vertical
}))), this.renderScale());
};
return Container;
}(PureComponent);
_defineProperty(Container, "defaultProps", DefaultValue);
export default Container;