-
-
Notifications
You must be signed in to change notification settings - Fork 775
Expand file tree
/
Copy pathTrack.tsx
More file actions
79 lines (66 loc) · 2.17 KB
/
Track.tsx
File metadata and controls
79 lines (66 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { clsx } from 'clsx';
import * as React from 'react';
import SliderContext from '../context';
import type { OnStartMove } from '../interface';
import { getOffset } from '../util';
export interface TrackProps {
prefixCls: string;
style?: React.CSSProperties;
/** Replace with origin prefix concat className */
replaceCls?: string;
start: number;
end: number;
index: number;
onStartMove?: OnStartMove;
}
const Track: React.FC<TrackProps> = (props) => {
const { prefixCls, style, start, end, index, onStartMove, replaceCls } = props;
const { direction, min, max, disabled, range, classNames } = React.useContext(SliderContext);
const trackPrefixCls = `${prefixCls}-track`;
const offsetStart = getOffset(start, min, max);
const offsetEnd = getOffset(end, min, max);
// ============================ Events ============================
const onInternalStartMove = (e: React.MouseEvent | React.TouchEvent) => {
if (!disabled && onStartMove) {
onStartMove(e, -1);
}
};
// ============================ Render ============================
const positionStyle: React.CSSProperties = {};
switch (direction) {
case 'rtl':
positionStyle.right = `${offsetStart * 100}%`;
positionStyle.width = `${offsetEnd * 100 - offsetStart * 100}%`;
break;
case 'btt':
positionStyle.bottom = `${offsetStart * 100}%`;
positionStyle.height = `${offsetEnd * 100 - offsetStart * 100}%`;
break;
case 'ttb':
positionStyle.top = `${offsetStart * 100}%`;
positionStyle.height = `${offsetEnd * 100 - offsetStart * 100}%`;
break;
default:
positionStyle.left = `${offsetStart * 100}%`;
positionStyle.width = `${offsetEnd * 100 - offsetStart * 100}%`;
}
const className =
replaceCls ||
clsx(
trackPrefixCls,
{
[`${trackPrefixCls}-${index + 1}`]: index !== null && range,
[`${prefixCls}-track-draggable`]: onStartMove,
},
classNames.track,
);
return (
<div
className={className}
style={{ ...positionStyle, ...style }}
onMouseDown={onInternalStartMove}
onTouchStart={onInternalStartMove}
/>
);
};
export default Track;