acud
Version:
acg react 组件库
89 lines • 2.91 kB
JavaScript
import React, { useMemo, useRef } from 'react';
import classNames from 'classnames';
;
var CarouselIndicator = function CarouselIndicator(_ref) {
var prefixCls = _ref.prefixCls,
_ref$dotPosition = _ref.dotPosition,
dotPosition = _ref$dotPosition === void 0 ? 'bottom' : _ref$dotPosition,
_ref$autoplay = _ref.autoplay,
autoplay = _ref$autoplay === void 0 ? true : _ref$autoplay,
length = _ref.length,
setKey = _ref.setKey,
currentKey = _ref.currentKey,
playInterval = _ref.playInterval,
_ref$dotType = _ref.dotType,
dotType = _ref$dotType === void 0 ? 'dot' : _ref$dotType,
multi = _ref.multi,
_ref$showNumber = _ref.showNumber,
showNumber = _ref$showNumber === void 0 ? 1 : _ref$showNumber,
style = _ref.style;
var timer = useRef();
var extraCount = useMemo(function () {
return multi ? showNumber : 1;
}, [multi, showNumber]);
var key = useMemo(function () {
if (currentKey >= length + extraCount) {
return currentKey - length - extraCount;
}
if (currentKey < extraCount) {
return length - extraCount + currentKey;
}
return currentKey - extraCount;
}, [currentKey, extraCount, length]);
var handleClick = function handleClick(idx) {
if (currentKey === 0) {
setKey({
lastKey: 0,
currentKey: length
});
clearTimeout(timer.current);
timer.current = setTimeout(function () {
setKey({
lastKey: length + 1,
currentKey: idx + extraCount
});
});
return;
}
if (currentKey === length + extraCount) {
setKey({
lastKey: currentKey,
currentKey: extraCount
});
clearTimeout(timer.current);
timer.current = setTimeout(function () {
setKey({
lastKey: extraCount,
currentKey: idx + extraCount
});
});
return;
}
setKey({
lastKey: currentKey,
currentKey: idx + extraCount
});
};
return /*#__PURE__*/React.createElement("div", {
style: style,
className: classNames("".concat(prefixCls, "-indicator"), "".concat(prefixCls, "-indicator-").concat(dotType), "".concat(prefixCls, "-indicator-").concat(dotPosition))
}, dotType === 'dot' ? /*#__PURE__*/React.createElement("ul", null, Array.from({
length: length
}, function (_v, k) {
return /*#__PURE__*/React.createElement("li", {
onClick: function onClick() {
return handleClick(k);
},
key: k,
className: k === key ? 'active' : ''
}, /*#__PURE__*/React.createElement("div", {
className: "fill",
style: {
transitionDuration: "".concat(k === key && autoplay ? playInterval : 0, "ms")
}
}));
})) : /*#__PURE__*/React.createElement("div", null, key + 1, "/", /*#__PURE__*/React.createElement("span", {
className: "total"
}, length)));
};
export default CarouselIndicator;