UNPKG

2.13 kBJavaScriptView Raw
1import React, { useRef, useState } from 'react';
2import { useDrag } from '@use-gesture/react';
3import { ThumbIcon } from './thumb-icon';
4import Popover from '../popover';
5import { useConfig } from '../config-provider';
6const classPrefix = `adm-slider`;
7const Thumb = props => {
8 const {
9 value,
10 min,
11 max,
12 disabled,
13 icon,
14 residentPopover,
15 onDrag
16 } = props;
17 const prevValue = useRef(value);
18 const {
19 locale
20 } = useConfig();
21 const currentPosition = () => {
22 return {
23 left: `${(value - min) / (max - min) * 100}%`,
24 right: 'auto'
25 };
26 };
27 const [dragging, setDragging] = useState(false);
28 const bind = useDrag(state => {
29 var _a;
30 if (disabled) return;
31 if (state.first) {
32 prevValue.current = value;
33 }
34 const x = state.xy[0] - state.initial[0];
35 const sliderOffsetWith = (_a = props.trackRef.current) === null || _a === void 0 ? void 0 : _a.offsetWidth;
36 if (!sliderOffsetWith) return;
37 const diff = x / Math.ceil(sliderOffsetWith) * (max - min);
38 onDrag(prevValue.current + diff, state.first, state.last);
39 setDragging(!state.last);
40 }, {
41 axis: 'x',
42 pointer: {
43 touch: true
44 }
45 });
46 const renderPopoverContent = typeof props.popover === 'function' ? props.popover : props.popover ? value => value.toString() : null;
47 const thumbElement = React.createElement("div", {
48 className: `${classPrefix}-thumb`
49 }, icon ? icon : React.createElement(ThumbIcon, {
50 className: `${classPrefix}-thumb-icon`
51 }));
52 return React.createElement("div", Object.assign({
53 className: `${classPrefix}-thumb-container`,
54 style: currentPosition()
55 }, bind(), {
56 role: 'slider',
57 "aria-label": props['aria-label'] || locale.Slider.name,
58 "aria-valuemax": max,
59 "aria-valuemin": min,
60 "aria-valuenow": value,
61 "aria-disabled": disabled
62 }), renderPopoverContent ? React.createElement(Popover, {
63 content: renderPopoverContent(value),
64 placement: 'top',
65 visible: residentPopover || dragging,
66 getContainer: null,
67 mode: 'dark'
68 }, thumbElement) : thumbElement);
69};
70export default Thumb;
\No newline at end of file