Dashboard sipadu mbip
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Carousel.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. "use strict";
  2. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
  3. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  4. exports.__esModule = true;
  5. exports.default = void 0;
  6. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  7. var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
  8. var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
  9. var _classnames = _interopRequireDefault(require("classnames"));
  10. var _style = _interopRequireDefault(require("dom-helpers/style"));
  11. var _transition = _interopRequireDefault(require("dom-helpers/transition"));
  12. var _react = _interopRequireWildcard(require("react"));
  13. var _uncontrollable = require("uncontrollable");
  14. var _CarouselCaption = _interopRequireDefault(require("./CarouselCaption"));
  15. var _CarouselItem = _interopRequireDefault(require("./CarouselItem"));
  16. var _SafeAnchor = _interopRequireDefault(require("./SafeAnchor"));
  17. var _ElementChildren = require("./utils/ElementChildren");
  18. var _triggerBrowserReflow = _interopRequireDefault(require("./utils/triggerBrowserReflow"));
  19. var _ThemeProvider = require("./ThemeProvider");
  20. var countChildren = function countChildren(c) {
  21. return _react.default.Children.toArray(c).filter(_react.default.isValidElement).length;
  22. };
  23. var SWIPE_THRESHOLD = 40; // TODO: `slide` should be `animate`.
  24. var defaultProps = {
  25. slide: true,
  26. fade: false,
  27. interval: 5000,
  28. keyboard: true,
  29. pauseOnHover: true,
  30. wrap: true,
  31. indicators: true,
  32. controls: true,
  33. activeIndex: 0,
  34. prevIcon: _react.default.createElement("span", {
  35. "aria-hidden": "true",
  36. className: "carousel-control-prev-icon"
  37. }),
  38. prevLabel: 'Previous',
  39. nextIcon: _react.default.createElement("span", {
  40. "aria-hidden": "true",
  41. className: "carousel-control-next-icon"
  42. }),
  43. nextLabel: 'Next',
  44. touch: true
  45. };
  46. var Carousel =
  47. /*#__PURE__*/
  48. function (_React$Component) {
  49. (0, _inheritsLoose2.default)(Carousel, _React$Component);
  50. function Carousel() {
  51. var _this;
  52. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  53. args[_key] = arguments[_key];
  54. }
  55. _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
  56. _this.state = {
  57. prevClasses: '',
  58. currentClasses: 'active',
  59. touchStartX: 0
  60. };
  61. _this.isUnmounted = false;
  62. _this.carousel = _react.default.createRef();
  63. _this.handleTouchStart = function (e) {
  64. _this.setState({
  65. touchStartX: e.changedTouches[0].screenX
  66. });
  67. };
  68. _this.handleTouchEnd = function (e) {
  69. // If the swipe is under the threshold, don't do anything.
  70. if (Math.abs(e.changedTouches[0].screenX - _this.state.touchStartX) < SWIPE_THRESHOLD) return;
  71. if (e.changedTouches[0].screenX < _this.state.touchStartX) {
  72. // Swiping left to navigate to next item.
  73. _this.handleNext(e);
  74. } else {
  75. // Swiping right to navigate to previous item.
  76. _this.handlePrev(e);
  77. }
  78. };
  79. _this.handleSlideEnd = function () {
  80. var pendingIndex = _this._pendingIndex;
  81. _this._isSliding = false;
  82. _this._pendingIndex = null;
  83. if (pendingIndex != null) _this.to(pendingIndex);else _this.cycle();
  84. };
  85. _this.handleMouseOut = function () {
  86. _this.cycle();
  87. };
  88. _this.handleMouseOver = function () {
  89. if (_this.props.pauseOnHover) _this.pause();
  90. };
  91. _this.handleKeyDown = function (event) {
  92. if (/input|textarea/i.test(event.target.tagName)) return;
  93. switch (event.key) {
  94. case 'ArrowLeft':
  95. event.preventDefault();
  96. _this.handlePrev(event);
  97. break;
  98. case 'ArrowRight':
  99. event.preventDefault();
  100. _this.handleNext(event);
  101. break;
  102. default:
  103. break;
  104. }
  105. };
  106. _this.handleNextWhenVisible = function () {
  107. if (!_this.isUnmounted && !document.hidden && (0, _style.default)(_this.carousel.current, 'visibility') !== 'hidden') {
  108. _this.handleNext();
  109. }
  110. };
  111. _this.handleNext = function (e) {
  112. if (_this._isSliding) return;
  113. var _this$props = _this.props,
  114. wrap = _this$props.wrap,
  115. activeIndex = _this$props.activeIndex;
  116. var index = activeIndex + 1;
  117. var count = countChildren(_this.props.children);
  118. if (index > count - 1) {
  119. if (!wrap) return;
  120. index = 0;
  121. }
  122. _this.select(index, e, 'next');
  123. };
  124. _this.handlePrev = function (e) {
  125. if (_this._isSliding) return;
  126. var _this$props2 = _this.props,
  127. wrap = _this$props2.wrap,
  128. activeIndex = _this$props2.activeIndex;
  129. var index = activeIndex - 1;
  130. if (index < 0) {
  131. if (!wrap) return;
  132. index = countChildren(_this.props.children) - 1;
  133. }
  134. _this.select(index, e, 'prev');
  135. };
  136. return _this;
  137. }
  138. var _proto = Carousel.prototype;
  139. _proto.componentDidMount = function componentDidMount() {
  140. this.cycle();
  141. };
  142. Carousel.getDerivedStateFromProps = function getDerivedStateFromProps(nextProps, _ref) {
  143. var previousActiveIndex = _ref.activeIndex;
  144. if (nextProps.activeIndex !== previousActiveIndex) {
  145. var lastPossibleIndex = countChildren(nextProps.children) - 1;
  146. var nextIndex = Math.max(0, Math.min(nextProps.activeIndex, lastPossibleIndex));
  147. var direction;
  148. if (nextIndex === 0 && previousActiveIndex >= lastPossibleIndex || previousActiveIndex <= nextIndex) {
  149. direction = 'next';
  150. } else {
  151. direction = 'prev';
  152. }
  153. return {
  154. direction: direction,
  155. previousActiveIndex: previousActiveIndex,
  156. activeIndex: nextIndex
  157. };
  158. }
  159. return null;
  160. };
  161. _proto.componentDidUpdate = function componentDidUpdate(_, prevState) {
  162. var _this2 = this;
  163. var _this$props3 = this.props,
  164. bsPrefix = _this$props3.bsPrefix,
  165. slide = _this$props3.slide,
  166. onSlideEnd = _this$props3.onSlideEnd;
  167. if (!slide || this.state.activeIndex === prevState.activeIndex || this._isSliding) return;
  168. var _this$state = this.state,
  169. activeIndex = _this$state.activeIndex,
  170. direction = _this$state.direction;
  171. var orderClassName, directionalClassName;
  172. if (direction === 'next') {
  173. orderClassName = bsPrefix + "-item-next";
  174. directionalClassName = bsPrefix + "-item-left";
  175. } else if (direction === 'prev') {
  176. orderClassName = bsPrefix + "-item-prev";
  177. directionalClassName = bsPrefix + "-item-right";
  178. }
  179. this._isSliding = true;
  180. this.pause(); // eslint-disable-next-line react/no-did-update-set-state
  181. this.safeSetState({
  182. prevClasses: 'active',
  183. currentClasses: orderClassName
  184. }, function () {
  185. var items = _this2.carousel.current.children;
  186. var nextElement = items[activeIndex];
  187. (0, _triggerBrowserReflow.default)(nextElement);
  188. _this2.safeSetState({
  189. prevClasses: (0, _classnames.default)('active', directionalClassName),
  190. currentClasses: (0, _classnames.default)(orderClassName, directionalClassName)
  191. }, function () {
  192. return _transition.default.end(nextElement, function () {
  193. _this2.safeSetState({
  194. prevClasses: '',
  195. currentClasses: 'active'
  196. }, _this2.handleSlideEnd);
  197. if (onSlideEnd) {
  198. onSlideEnd();
  199. }
  200. });
  201. });
  202. });
  203. };
  204. _proto.componentWillUnmount = function componentWillUnmount() {
  205. clearTimeout(this.timeout);
  206. this.isUnmounted = true;
  207. };
  208. _proto.safeSetState = function safeSetState(state, cb) {
  209. var _this3 = this;
  210. if (this.isUnmounted) return;
  211. this.setState(state, function () {
  212. return !_this3.isUnmounted && cb();
  213. });
  214. } // This might be a public API.
  215. ;
  216. _proto.pause = function pause() {
  217. this._isPaused = true;
  218. clearInterval(this._interval);
  219. this._interval = null;
  220. };
  221. _proto.cycle = function cycle() {
  222. this._isPaused = false;
  223. clearInterval(this._interval);
  224. this._interval = null;
  225. if (this.props.interval && !this._isPaused) {
  226. this._interval = setInterval(document.visibilityState ? this.handleNextWhenVisible : this.handleNext, this.props.interval);
  227. }
  228. };
  229. _proto.to = function to(index, event) {
  230. var children = this.props.children;
  231. if (index < 0 || index > countChildren(children) - 1) {
  232. return;
  233. }
  234. if (this._isSliding) {
  235. this._pendingIndex = index;
  236. return;
  237. }
  238. this.select(index, event);
  239. };
  240. _proto.select = function select(index, event, direction) {
  241. var _this4 = this;
  242. clearTimeout(this.selectThrottle);
  243. if (event && event.persist) event.persist(); // The timeout throttles fast clicks, in order to give any pending state
  244. // a chance to update and propagate back through props
  245. this.selectThrottle = setTimeout(function () {
  246. clearTimeout(_this4.timeout);
  247. var _this4$props = _this4.props,
  248. activeIndex = _this4$props.activeIndex,
  249. onSelect = _this4$props.onSelect;
  250. if (index === activeIndex || _this4._isSliding || _this4.isUnmounted) return;
  251. onSelect(index, direction || (index < activeIndex ? 'prev' : 'next'), event);
  252. }, 50);
  253. };
  254. _proto.renderControls = function renderControls(properties) {
  255. var bsPrefix = this.props.bsPrefix;
  256. var wrap = properties.wrap,
  257. children = properties.children,
  258. activeIndex = properties.activeIndex,
  259. prevIcon = properties.prevIcon,
  260. nextIcon = properties.nextIcon,
  261. prevLabel = properties.prevLabel,
  262. nextLabel = properties.nextLabel;
  263. var count = countChildren(children);
  264. return [(wrap || activeIndex !== 0) && _react.default.createElement(_SafeAnchor.default, {
  265. key: "prev",
  266. className: bsPrefix + "-control-prev",
  267. onClick: this.handlePrev
  268. }, prevIcon, prevLabel && _react.default.createElement("span", {
  269. className: "sr-only"
  270. }, prevLabel)), (wrap || activeIndex !== count - 1) && _react.default.createElement(_SafeAnchor.default, {
  271. key: "next",
  272. className: bsPrefix + "-control-next",
  273. onClick: this.handleNext
  274. }, nextIcon, nextLabel && _react.default.createElement("span", {
  275. className: "sr-only"
  276. }, nextLabel))];
  277. };
  278. _proto.renderIndicators = function renderIndicators(children, activeIndex) {
  279. var _this5 = this;
  280. var bsPrefix = this.props.bsPrefix;
  281. var indicators = [];
  282. (0, _ElementChildren.forEach)(children, function (child, index) {
  283. indicators.push(_react.default.createElement("li", {
  284. key: index,
  285. className: index === activeIndex ? 'active' : null,
  286. onClick: function onClick(e) {
  287. return _this5.to(index, e);
  288. }
  289. }), // Force whitespace between indicator elements. Bootstrap requires
  290. // this for correct spacing of elements.
  291. ' ');
  292. });
  293. return _react.default.createElement("ol", {
  294. className: bsPrefix + "-indicators"
  295. }, indicators);
  296. };
  297. _proto.render = function render() {
  298. var _this$props4 = this.props,
  299. _this$props4$as = _this$props4.as,
  300. Component = _this$props4$as === void 0 ? 'div' : _this$props4$as,
  301. bsPrefix = _this$props4.bsPrefix,
  302. slide = _this$props4.slide,
  303. fade = _this$props4.fade,
  304. indicators = _this$props4.indicators,
  305. controls = _this$props4.controls,
  306. wrap = _this$props4.wrap,
  307. touch = _this$props4.touch,
  308. prevIcon = _this$props4.prevIcon,
  309. prevLabel = _this$props4.prevLabel,
  310. nextIcon = _this$props4.nextIcon,
  311. nextLabel = _this$props4.nextLabel,
  312. className = _this$props4.className,
  313. children = _this$props4.children,
  314. keyboard = _this$props4.keyboard,
  315. _5 = _this$props4.activeIndex,
  316. _4 = _this$props4.pauseOnHover,
  317. _3 = _this$props4.interval,
  318. _2 = _this$props4.onSelect,
  319. _1 = _this$props4.onSlideEnd,
  320. props = (0, _objectWithoutPropertiesLoose2.default)(_this$props4, ["as", "bsPrefix", "slide", "fade", "indicators", "controls", "wrap", "touch", "prevIcon", "prevLabel", "nextIcon", "nextLabel", "className", "children", "keyboard", "activeIndex", "pauseOnHover", "interval", "onSelect", "onSlideEnd"]);
  321. var _this$state2 = this.state,
  322. activeIndex = _this$state2.activeIndex,
  323. previousActiveIndex = _this$state2.previousActiveIndex,
  324. prevClasses = _this$state2.prevClasses,
  325. currentClasses = _this$state2.currentClasses;
  326. return (// eslint-disable-next-line jsx-a11y/no-static-element-interactions
  327. _react.default.createElement(Component, (0, _extends2.default)({
  328. onTouchStart: touch ? this.handleTouchStart : undefined,
  329. onTouchEnd: touch ? this.handleTouchEnd : undefined
  330. }, props, {
  331. className: (0, _classnames.default)(className, bsPrefix, slide && 'slide', fade && bsPrefix + "-fade"),
  332. onKeyDown: keyboard ? this.handleKeyDown : undefined,
  333. onMouseOver: this.handleMouseOver,
  334. onMouseOut: this.handleMouseOut
  335. }), indicators && this.renderIndicators(children, activeIndex), _react.default.createElement("div", {
  336. className: bsPrefix + "-inner",
  337. ref: this.carousel
  338. }, (0, _ElementChildren.map)(children, function (child, index) {
  339. var current = index === activeIndex;
  340. var previous = index === previousActiveIndex;
  341. return (0, _react.cloneElement)(child, {
  342. className: (0, _classnames.default)(child.props.className, bsPrefix + "-item", current && currentClasses, previous && prevClasses)
  343. });
  344. })), controls && this.renderControls({
  345. wrap: wrap,
  346. children: children,
  347. activeIndex: activeIndex,
  348. prevIcon: prevIcon,
  349. prevLabel: prevLabel,
  350. nextIcon: nextIcon,
  351. nextLabel: nextLabel
  352. }))
  353. );
  354. };
  355. return Carousel;
  356. }(_react.default.Component);
  357. Carousel.defaultProps = defaultProps;
  358. var DecoratedCarousel = (0, _ThemeProvider.createBootstrapComponent)((0, _uncontrollable.uncontrollable)(Carousel, {
  359. activeIndex: 'onSelect'
  360. }), 'carousel');
  361. DecoratedCarousel.Caption = _CarouselCaption.default;
  362. DecoratedCarousel.Item = _CarouselItem.default;
  363. var _default = DecoratedCarousel;
  364. exports.default = _default;
  365. module.exports = exports["default"];