Debian Codegraph @

node/@patternfly/react-table/src/components/Table/examples/TableDraggable.tsx

raw
1 import { useRef, useState } from 'react';
2 import { Table, Thead, Tbody, Tr, Th, Td, TbodyProps, TrProps } from '@patternfly/react-table';
3 import styles from '@patternfly/react-styles/css/components/Table/table';
4
5 export const TableDraggable: React.FunctionComponent = () => {
6 const [draggedItemId, setDraggedItemId] = useState<string | null>(null);
7 const [draggingToItemIndex, setDraggingToItemIndex] = useState<number | null>(null);
8 const [isDragging, setIsDragging] = useState(false);
9 const [itemOrder, setItemOrder] = useState(['row1', 'row2', 'row3']);
10 const [tempItemOrder, setTempItemOrder] = useState<string[]>([]);
11
12 const bodyRef = useRef<HTMLTableSectionElement>(undefined);
13
14 const onDragStart: TrProps['onDragStart'] = (evt) => {
15 evt.dataTransfer.effectAllowed = 'move';
16 evt.dataTransfer.setData('text/plain', evt.currentTarget.id);
17 const draggedItemId = evt.currentTarget.id;
18
19 evt.currentTarget.classList.add(styles.modifiers.ghostRow);
20 evt.currentTarget.setAttribute('aria-pressed', 'true');
21
22 setDraggedItemId(draggedItemId);
23 setIsDragging(true);
24 };
25
26 const moveItem = (arr: string[], i1: string, toIndex: number) => {
27 const fromIndex = arr.indexOf(i1);
28 if (fromIndex === toIndex) {
29 return arr;
30 }
31 const temp = arr.splice(fromIndex, 1);
32 arr.splice(toIndex, 0, temp[0]);
33
34 return arr;
35 };
36
37 const move = (itemOrder: string[]) => {
38 const ulNode = bodyRef.current;
39 const nodes = Array.from(ulNode.children);
40 if (nodes.map((node) => node.id).every((id, i) => id === itemOrder[i])) {
41 return;
42 }
43 while (ulNode.firstChild) {
44 ulNode.removeChild(ulNode.lastChild);
45 }
46
47 itemOrder.forEach((id) => {
48 ulNode.appendChild(nodes.find((n) => n.id === id));
49 });
50 };
51
52 const onDragCancel = () => {
53 Array.from(bodyRef.current.children).forEach((el) => {
54 el.classList.remove(styles.modifiers.ghostRow);
55 el.setAttribute('aria-pressed', 'false');
56 });
57 setDraggedItemId(null);
58 setDraggingToItemIndex(null);
59 setIsDragging(false);
60 };
61
62 const onDragLeave: TbodyProps['onDragLeave'] = (evt) => {
63 if (!isValidDrop(evt)) {
64 move(itemOrder);
65 setDraggingToItemIndex(null);
66 }
67 };
68
69 const isValidDrop = (evt: React.DragEvent<HTMLTableSectionElement | HTMLTableRowElement>) => {
70 const ulRect = bodyRef.current.getBoundingClientRect();
71 return (
72 evt.clientX > ulRect.x &&
73 evt.clientX < ulRect.x + ulRect.width &&
74 evt.clientY > ulRect.y &&
75 evt.clientY < ulRect.y + ulRect.height
76 );
77 };
78
79 const onDrop: TrProps['onDrop'] = (evt) => {
80 if (isValidDrop(evt)) {
81 setItemOrder(tempItemOrder);
82 } else {
83 onDragCancel();
84 }
85 };
86
87 const onDragOver: TbodyProps['onDragOver'] = (evt) => {
88 evt.preventDefault();
89
90 const curListItem = (evt.target as HTMLTableSectionElement).closest('tr');
91 if (!curListItem || !bodyRef.current.contains(curListItem) || curListItem.id === draggedItemId) {
92 return null;
93 } else {
94 const dragId = curListItem.id;
95 const newDraggingToItemIndex = Array.from(bodyRef.current.children).findIndex((item) => item.id === dragId);
96 if (newDraggingToItemIndex !== draggingToItemIndex) {
97 const tempItemOrder = moveItem([...itemOrder], draggedItemId, newDraggingToItemIndex);
98 move(tempItemOrder);
99 setDraggingToItemIndex(newDraggingToItemIndex);
100 setTempItemOrder(tempItemOrder);
101 }
102 }
103 };
104
105 const onDragEnd: TrProps['onDragEnd'] = (evt) => {
106 const target = evt.target as HTMLTableRowElement;
107 target.classList.remove(styles.modifiers.ghostRow);
108 target.setAttribute('aria-pressed', 'false');
109 setDraggedItemId(null);
110 setDraggingToItemIndex(null);
111 setIsDragging(false);
112 };
113
114 const columns = ['Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit'];
115 const rows = [
116 {
117 id: 'row1',
118 repository: 'one',
119 branch: 'two',
120 pullRequest: 'three',
121 workspace: 'four',
122 lastCommit: 'five'
123 },
124 {
125 id: 'row2',
126 repository: 'one -2',
127 branch: null,
128 pullRequest: null,
129 workspace: 'four -2',
130 lastCommit: 'five -2'
131 },
132 {
133 id: 'row3',
134 repository: 'one - 3',
135 branch: 'two - 3',
136 pullRequest: 'three - 3',
137 workspace: 'four - 3',
138 lastCommit: 'five - 3'
139 }
140 ];
141
142 return (
143 <Table aria-label="Draggable table" className={isDragging ? styles.modifiers.dragOver : ''}>
144 <Thead>
145 <Tr>
146 <Th screenReaderText="Drag and drop" />
147 {columns.map((column, columnIndex) => (
148 <Th key={columnIndex}>{column}</Th>
149 ))}
150 </Tr>
151 </Thead>
152 <Tbody ref={bodyRef} onDragOver={onDragOver} onDrop={onDragOver} onDragLeave={onDragLeave}>
153 {rows.map((row, rowIndex) => {
154 const rowCellsToBuild = Object.keys(row).filter((rowCell) => rowCell !== 'id');
155 return (
156 <Tr key={rowIndex} id={row.id} draggable onDrop={onDrop} onDragEnd={onDragEnd} onDragStart={onDragStart}>
157 <Td
158 draggableRow={{
159 id: `draggable-row-${row.id}`
160 }}
161 />
162 {rowCellsToBuild.map((key, keyIndex) => (
163 <Td key={`${rowIndex}_${keyIndex}`} dataLabel={columns[keyIndex]}>
164 {row[key]}
165 </Td>
166 ))}
167 </Tr>
168 );
169 })}
170 </Tbody>
171 </Table>
172 );
173 };