CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/components/data-grid/headings.tsx
Views: 687
1
import Draggable from "react-draggable";
2
import { CSSProperties, ReactNode, useRef, useState } from "react";
3
import { Icon } from "@cocalc/frontend/components";
4
5
export type SortDirection = "ascending" | "descending";
6
export function nextSortState(direction?: SortDirection | null) {
7
if (direction == "descending") {
8
return "ascending";
9
} else if (direction == "ascending") {
10
return null;
11
} else {
12
return "descending";
13
}
14
}
15
16
const DIRECTION_STYLE = {
17
float: "right",
18
marginTop: "2.5px",
19
cursor: "pointer",
20
} as CSSProperties;
21
22
export function ColumnHeading({
23
width = 150,
24
title,
25
direction,
26
onSortClick,
27
setWidth,
28
}: {
29
width?: number;
30
title?: ReactNode;
31
direction?: SortDirection;
32
onSortClick?: () => void;
33
setWidth?: (number) => void;
34
}) {
35
const ignoreClickRef = useRef<boolean>(false);
36
return (
37
<th
38
style={{
39
cursor: "pointer",
40
color: "#428bca",
41
background: "rgb(250, 250, 250)",
42
padding: "10px 5px",
43
border: "1px solid #eee",
44
position: "relative",
45
}}
46
>
47
<div
48
style={{ width: width ?? 150 }}
49
onClick={
50
onSortClick
51
? () => {
52
if (ignoreClickRef.current) {
53
ignoreClickRef.current = false;
54
return;
55
}
56
onSortClick();
57
}
58
: undefined
59
}
60
>
61
{title ? title : <>&nbsp;</>}
62
{direction && (
63
<Icon
64
style={DIRECTION_STYLE}
65
name={direction == "ascending" ? "caret-down" : "caret-up"}
66
/>
67
)}
68
{setWidth && (
69
<ResizeHandle
70
setWidth={setWidth}
71
width={width}
72
ignoreClick={() => {
73
ignoreClickRef.current = true;
74
}}
75
/>
76
)}
77
</div>
78
</th>
79
);
80
}
81
82
function ResizeHandle({ setWidth, width, ignoreClick }) {
83
const [pos, setPos] = useState<any>(undefined);
84
return (
85
<Draggable
86
onMouseDown={ignoreClick}
87
position={pos}
88
axis="x"
89
onStop={() => {
90
setPos({ x: 0, y: 0 });
91
}}
92
onDrag={(_, data) => {
93
setPos({ x: 0, y: 0 });
94
ignoreClick();
95
setWidth(width + data.deltaX);
96
}}
97
>
98
<span className="cocalc-data-grid-column-resizer"></span>
99
</Draggable>
100
);
101
}
102
103