Debian Codegraph @

node/@patternfly/react-core/src/deprecated/components/DragDrop/examples/DragDropBasic.tsx

raw
1 import { useState } from 'react';
2 import { DragDrop, Draggable, Droppable } from '@patternfly/react-core/deprecated';
3
4 interface ItemType {
5 id: string;
6 content: string;
7 }
8
9 interface SourceType {
10 droppableId: string;
11 index: number;
12 }
13
14 interface DestinationType extends SourceType {}
15
16 const getItems = (count: number) =>
17 Array.from({ length: count }, (_, idx) => idx).map((idx) => ({
18 id: `item-${idx}`,
19 content: `item ${idx} `.repeat(idx === 4 ? 20 : 1)
20 }));
21
22 const reorder = (list: ItemType[], startIndex: number, endIndex: number) => {
23 const result = [...list];
24 const [removed] = result.splice(startIndex, 1);
25 result.splice(endIndex, 0, removed);
26 return result;
27 };
28
29 export const DragDropBasic: React.FunctionComponent = () => {
30 const [items, setItems] = useState<ItemType[]>(getItems(10));
31
32 function onDrop(source: SourceType, dest: DestinationType) {
33 if (dest) {
34 const newItems = reorder(items, source.index, dest.index);
35 setItems(newItems);
36
37 return true;
38 }
39 return false;
40 }
41
42 return (
43 <DragDrop onDrop={onDrop}>
44 <Droppable>
45 {items.map(({ id, content }) => (
46 <Draggable key={id} style={{ padding: '8px' }}>
47 {content}
48 </Draggable>
49 ))}
50 </Droppable>
51 </DragDrop>
52 );
53 };