ant-design-vue/components/vc-slick/dots.jsx

87 lines
2.1 KiB
Vue
Raw Normal View History

import classnames from '../_util/classNames';
import { cloneElement } from '../_util/vnode';
import { clamp } from './utils/innerSliderUtils';
2018-07-20 16:13:21 +08:00
2021-06-23 23:08:16 +08:00
const getDotCount = function (spec) {
2019-01-12 11:33:27 +08:00
let dots;
2018-07-20 16:13:21 +08:00
if (spec.infinite) {
2019-01-12 11:33:27 +08:00
dots = Math.ceil(spec.slideCount / spec.slidesToScroll);
2018-07-20 16:13:21 +08:00
} else {
2019-01-12 11:33:27 +08:00
dots = Math.ceil((spec.slideCount - spec.slidesToShow) / spec.slidesToScroll) + 1;
2018-07-20 16:13:21 +08:00
}
2019-01-12 11:33:27 +08:00
return dots;
};
2018-07-20 16:13:21 +08:00
2020-07-20 16:29:46 +08:00
const Dots = (_, { attrs }) => {
const {
slideCount,
slidesToScroll,
slidesToShow,
infinite,
currentSlide,
appendDots,
customPaging,
clickHandler,
dotsClass,
onMouseenter,
onMouseover,
onMouseleave,
} = attrs;
const dotCount = getDotCount({
slideCount,
slidesToScroll,
slidesToShow,
infinite,
});
2018-07-20 16:13:21 +08:00
2020-07-20 16:29:46 +08:00
// Apply join & split to Array to pre-fill it for IE8
//
// Credit: http://stackoverflow.com/a/13735425/1849458
const mouseEvents = { onMouseenter, onMouseover, onMouseleave };
let dots = [];
for (let i = 0; i < dotCount; i++) {
2023-08-30 21:44:03 +08:00
const _rightBound = (i + 1) * slidesToScroll - 1;
const rightBound = infinite ? _rightBound : clamp(_rightBound, 0, slideCount - 1);
const _leftBound = rightBound - (slidesToScroll - 1);
const leftBound = infinite ? _leftBound : clamp(_leftBound, 0, slideCount - 1);
2023-08-30 21:44:03 +08:00
const className = classnames({
'slick-active': infinite
? currentSlide >= leftBound && currentSlide <= rightBound
: currentSlide === leftBound,
2020-07-20 16:29:46 +08:00
});
2018-07-20 16:13:21 +08:00
2023-08-30 21:44:03 +08:00
const dotOptions = {
2020-07-20 16:29:46 +08:00
message: 'dots',
index: i,
slidesToScroll,
currentSlide,
};
2020-07-20 16:29:46 +08:00
function onClick(e) {
// In Autoplay the focus stays on clicked button even after transition
// to next slide. That only goes away by click somewhere outside
if (e) {
e.preventDefault();
2018-07-20 16:13:21 +08:00
}
2020-07-20 16:29:46 +08:00
clickHandler(dotOptions);
}
dots = dots.concat(
2020-07-20 16:29:46 +08:00
<li key={i} class={className}>
{cloneElement(customPaging({ i }), { onClick })}
</li>,
2020-07-20 16:29:46 +08:00
);
}
2018-07-20 16:13:21 +08:00
2020-07-20 16:29:46 +08:00
return cloneElement(appendDots({ dots }), {
class: dotsClass,
...mouseEvents,
});
2019-01-12 11:33:27 +08:00
};
2020-07-20 16:29:46 +08:00
Dots.inheritAttrs = false;
export default Dots;