hippy-ui
Version:
> hippy-react ui components for hippy2.0
211 lines (174 loc) • 7.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.Slider = void 0;
var _react = _interopRequireDefault(require("react"));
var _react2 = require("@hippy/react");
var _Panel = _interopRequireDefault(require("./Panel"));
var _props = require("./props");
var _utils = require("../../utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var styles = _react2.StyleSheet.create({
container: {}
});
var Slider =
/*#__PURE__*/
function (_React$Component) {
_inherits(Slider, _React$Component);
function Slider(props) {
var _this;
_classCallCheck(this, Slider);
_this = _possibleConstructorReturn(this, _getPrototypeOf(Slider).call(this, props));
var horizontal = _this.props.horizontal;
_this.axis = horizontal ? 'x' : 'y';
_this.panelRefs = {};
_this.onScroll = _this.onScroll.bind(_assertThisInitialized(_this));
_this.scrollTo = _this.scrollTo.bind(_assertThisInitialized(_this));
return _this;
}
_createClass(Slider, [{
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.clearScrollBuffer();
}
}, {
key: "clearScrollBuffer",
value: function clearScrollBuffer() {
if (this.scrollBuffer) {
clearTimeout(this.scrollBuffer);
}
}
}, {
key: "getClosestPanel",
value: function getClosestPanel() {
var _this2 = this;
var distance = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var minDist = 9999999;
var index = -1;
var location = -1;
Object.keys(this.panelRefs).forEach(function (key, ind) {
var panelLayout = _this2.panelRefs[key].panelLayout;
var currentDist = panelLayout[_this2.axis];
var absDist = Math.abs(currentDist - distance);
if (absDist < minDist) {
minDist = absDist;
index = ind;
location = currentDist;
}
});
return {
index: index,
location: location
};
}
}, {
key: "scrollTo",
value: function scrollTo(value) {
var x = this.axis === 'x' ? value : 0;
var y = this.axis === 'y' ? value : 0;
if (this.scrollerRef) {
_utils.ISWEB ? this.scrollerRef.scrollTo(x, y, true) : this.scrollerRef.scrollTo({
x: x,
y: y,
animated: true
});
}
}
}, {
key: "scrollToIndex",
value: function scrollToIndex(index) {
var selectedPanel = this.panelRefs["".concat(index)];
if (!selectedPanel) return;
var panelLayout = selectedPanel.panelLayout;
var currentDist = panelLayout[this.axis];
this.scrollTo(currentDist);
} // web 需要优化,不需要每次都执行操作,看看速度是否为0
}, {
key: "onScroll",
value: function onScroll(e) {
var _this3 = this;
var distance = e.contentOffset[this.axis];
this.clearScrollBuffer();
this.scrollBuffer = setTimeout(function () {
_this3.clearScrollBuffer();
var _this3$getClosestPane = _this3.getClosestPanel(distance),
location = _this3$getClosestPane.location,
index = _this3$getClosestPane.index;
if (index !== -1) {
_this3.scrollTo(location);
}
}, 180);
}
}, {
key: "getPanelStyle",
value: function getPanelStyle(index) {
var _this$props = this.props,
cellSpacing = _this$props.cellSpacing,
slideWidth = _this$props.slideWidth,
children = _this$props.children,
horizontal = _this$props.horizontal;
var count = _react.default.Children.count(children);
var marginName = horizontal ? 'marginRight' : 'marginBottom';
var style = _defineProperty({}, marginName, count !== index + 1 ? cellSpacing : 0);
if (this.props.hasOwnProperty('slideWidth')) style.width = slideWidth;
return style;
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
var selectedIndex = this.props.selectedIndex;
this.scrollToIndex(selectedIndex);
}
}, {
key: "render",
value: function render() {
var _this4 = this;
var _this$props2 = this.props,
children = _this$props2.children,
horizontal = _this$props2.horizontal,
style = _this$props2.style,
contentContainerStyle = _this$props2.contentContainerStyle;
return (
/*#__PURE__*/
_react.default.createElement(_react2.ScrollView, {
horizontal: horizontal,
style: (0, _utils.flattenStyle)([styles.container, (0, _utils.flattenStyle)(style)]),
contentContainerStyle: (0, _utils.flattenStyle)(contentContainerStyle),
onScroll: this.onScroll,
ref: function ref(el) {
return _this4.scrollerRef = el;
},
scrollEventThrottle: 32,
pagingEnabled: true
}, _react.default.Children.map(children, function (child, index) {
return (
/*#__PURE__*/
_react.default.createElement(_Panel.default, {
style: _this4.getPanelStyle(index),
ref: function ref(_ref) {
return _this4.panelRefs[index] = _ref;
}
}, child)
);
}))
);
}
}]);
return Slider;
}(_react.default.Component);
exports.Slider = Slider;
Slider.propTypes = _props.Props;
Slider.defaultProps = _props.DefaultProps;
var _default = Slider;
exports.default = _default;