forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortableItem.tsx
More file actions
106 lines (99 loc) · 2.34 KB
/
SortableItem.tsx
File metadata and controls
106 lines (99 loc) · 2.34 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React from 'react';
import { useIntl } from '@edx/frontend-platform/i18n';
import { useSortable } from '@dnd-kit/sortable';
import { CSS } from '@dnd-kit/utilities';
import {
Col, Icon, Row,
} from '@openedx/paragon';
import { DragIndicator } from '@openedx/paragon/icons';
import messages from './messages';
interface SortableItemProps {
id: string;
data: {
category: string;
childAddable?: boolean;
displayName: string;
status: string;
}
isDroppable?: boolean;
isDraggable?: boolean;
children: React.ReactNode;
componentStyle?: object;
onClick?: (e: React.MouseEvent) => void;
}
const SortableItem = ({
id,
isDraggable = true,
isDroppable = true,
componentStyle,
data,
children,
onClick,
}: SortableItemProps) => {
const intl = useIntl();
const {
attributes,
listeners,
setNodeRef,
transform,
transition,
isDragging,
setActivatorNodeRef,
} = useSortable({
id,
data,
disabled: {
draggable: !isDraggable,
droppable: !isDroppable,
},
animateLayoutChanges: () => false,
});
const style = {
position: 'relative',
zIndex: isDragging ? 200 : undefined,
transform: CSS.Translate.toString(transform),
transition,
background: 'white',
padding: '1rem 1.5rem',
marginBottom: '1.5rem',
borderRadius: '0.35rem',
boxShadow: '0 0 .125rem rgba(0, 0, 0, .15), 0 0 .25rem rgba(0, 0, 0, .15)',
...componentStyle,
};
return (
<Row
ref={setNodeRef}
tabIndex={onClick ? 0 : -1}
style={style}
className="mx-0"
onClick={onClick}
onKeyDown={(e) => {
if (!onClick) { return; }
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
onClick(e);
}
}}
>
<Col className="extend-margin px-0">
{children}
</Col>
{isDraggable && (
<button
ref={setActivatorNodeRef}
key="drag-to-reorder-icon"
aria-label={intl.formatMessage(messages.tooltipContent)}
className="btn-icon btn-icon-secondary btn-icon-md"
type="button"
{...attributes}
{...listeners}
>
<span className="btn-icon__icon-container">
<Icon src={DragIndicator} />
</span>
</button>
)}
</Row>
);
};
export default SortableItem;