@patternfly/react-drag-drop 6.0.0-alpha.41 → 6.0.0-alpha.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/cjs.tsbuildinfo +1 -1
  3. package/dist/esm/components/DragDrop/DragButton.js +2 -2
  4. package/dist/esm/components/DragDrop/DragButton.js.map +1 -1
  5. package/dist/esm/components/DragDrop/DragDropSort.d.ts +2 -2
  6. package/dist/esm/components/DragDrop/DragDropSort.d.ts.map +1 -1
  7. package/dist/esm/components/DragDrop/DragDropSort.js +1 -1
  8. package/dist/esm/components/DragDrop/DragDropSort.js.map +1 -1
  9. package/dist/esm/components/DragDrop/Draggable.js +1 -1
  10. package/dist/esm/components/DragDrop/Draggable.js.map +1 -1
  11. package/dist/esm/components/DragDrop/DraggableDataListItem.js +2 -2
  12. package/dist/esm/components/DragDrop/DraggableDataListItem.js.map +1 -1
  13. package/dist/esm/components/DragDrop/DraggableDualListSelectorListItem.js +2 -2
  14. package/dist/esm/components/DragDrop/DraggableDualListSelectorListItem.js.map +1 -1
  15. package/dist/esm/next/components/DragDrop/DragButton.d.ts +11 -0
  16. package/dist/esm/next/components/DragDrop/DragButton.d.ts.map +1 -0
  17. package/dist/esm/next/components/DragDrop/DragButton.js +14 -0
  18. package/dist/esm/next/components/DragDrop/DragButton.js.map +1 -0
  19. package/dist/esm/next/components/DragDrop/DragDropSort.d.ts +35 -0
  20. package/dist/esm/next/components/DragDrop/DragDropSort.d.ts.map +1 -0
  21. package/dist/esm/next/components/DragDrop/DragDropSort.js +73 -0
  22. package/dist/esm/next/components/DragDrop/DragDropSort.js.map +1 -0
  23. package/dist/esm/next/components/DragDrop/Draggable.d.ts +13 -0
  24. package/dist/esm/next/components/DragDrop/Draggable.d.ts.map +1 -0
  25. package/dist/esm/next/components/DragDrop/Draggable.js +22 -0
  26. package/dist/esm/next/components/DragDrop/Draggable.js.map +1 -0
  27. package/dist/esm/next/components/DragDrop/DraggableDataListItem.d.ts +15 -0
  28. package/dist/esm/next/components/DragDrop/DraggableDataListItem.d.ts.map +1 -0
  29. package/dist/esm/next/components/DragDrop/DraggableDataListItem.js +26 -0
  30. package/dist/esm/next/components/DragDrop/DraggableDataListItem.js.map +1 -0
  31. package/dist/esm/next/components/DragDrop/DraggableDualListSelectorListItem.d.ts +23 -0
  32. package/dist/esm/next/components/DragDrop/DraggableDualListSelectorListItem.d.ts.map +1 -0
  33. package/dist/esm/next/components/DragDrop/DraggableDualListSelectorListItem.js +37 -0
  34. package/dist/esm/next/components/DragDrop/DraggableDualListSelectorListItem.js.map +1 -0
  35. package/dist/esm/next/components/DragDrop/Droppable.d.ts +16 -0
  36. package/dist/esm/next/components/DragDrop/Droppable.d.ts.map +1 -0
  37. package/dist/esm/next/components/DragDrop/Droppable.js +11 -0
  38. package/dist/esm/next/components/DragDrop/Droppable.js.map +1 -0
  39. package/dist/esm/next/components/DragDrop/DroppableContext.d.ts +6 -0
  40. package/dist/esm/next/components/DragDrop/DroppableContext.d.ts.map +1 -0
  41. package/dist/esm/next/components/DragDrop/DroppableContext.js +6 -0
  42. package/dist/esm/next/components/DragDrop/DroppableContext.js.map +1 -0
  43. package/dist/esm/next/components/DragDrop/index.d.ts +2 -0
  44. package/dist/esm/next/components/DragDrop/index.d.ts.map +1 -0
  45. package/dist/esm/next/components/DragDrop/index.js +2 -0
  46. package/dist/esm/next/components/DragDrop/index.js.map +1 -0
  47. package/dist/esm/next/components/index.d.ts +2 -0
  48. package/dist/esm/next/components/index.d.ts.map +1 -0
  49. package/dist/esm/next/components/index.js +2 -0
  50. package/dist/esm/next/components/index.js.map +1 -0
  51. package/dist/esm/next/index.d.ts +2 -0
  52. package/dist/esm/next/index.d.ts.map +1 -0
  53. package/dist/esm/next/index.js +2 -0
  54. package/dist/esm/next/index.js.map +1 -0
  55. package/dist/esm.tsbuildinfo +1 -1
  56. package/dist/js/components/DragDrop/DragButton.js +1 -1
  57. package/dist/js/components/DragDrop/DragButton.js.map +1 -1
  58. package/dist/js/components/DragDrop/DragDropSort.d.ts +2 -2
  59. package/dist/js/components/DragDrop/DragDropSort.d.ts.map +1 -1
  60. package/dist/js/components/DragDrop/DragDropSort.js.map +1 -1
  61. package/dist/js/components/DragDrop/DraggableDualListSelectorListItem.js +1 -1
  62. package/dist/js/components/DragDrop/DraggableDualListSelectorListItem.js.map +1 -1
  63. package/dist/js/next/components/DragDrop/DragButton.d.ts +11 -0
  64. package/dist/js/next/components/DragDrop/DragButton.d.ts.map +1 -0
  65. package/dist/js/next/components/DragDrop/DragButton.js +18 -0
  66. package/dist/js/next/components/DragDrop/DragButton.js.map +1 -0
  67. package/dist/js/next/components/DragDrop/DragDropSort.d.ts +35 -0
  68. package/dist/js/next/components/DragDrop/DragDropSort.d.ts.map +1 -0
  69. package/dist/js/next/components/DragDrop/DragDropSort.js +77 -0
  70. package/dist/js/next/components/DragDrop/DragDropSort.js.map +1 -0
  71. package/dist/js/next/components/DragDrop/Draggable.d.ts +13 -0
  72. package/dist/js/next/components/DragDrop/Draggable.d.ts.map +1 -0
  73. package/dist/js/next/components/DragDrop/Draggable.js +26 -0
  74. package/dist/js/next/components/DragDrop/Draggable.js.map +1 -0
  75. package/dist/js/next/components/DragDrop/DraggableDataListItem.d.ts +15 -0
  76. package/dist/js/next/components/DragDrop/DraggableDataListItem.d.ts.map +1 -0
  77. package/dist/js/next/components/DragDrop/DraggableDataListItem.js +30 -0
  78. package/dist/js/next/components/DragDrop/DraggableDataListItem.js.map +1 -0
  79. package/dist/js/next/components/DragDrop/DraggableDualListSelectorListItem.d.ts +23 -0
  80. package/dist/js/next/components/DragDrop/DraggableDualListSelectorListItem.d.ts.map +1 -0
  81. package/dist/js/next/components/DragDrop/DraggableDualListSelectorListItem.js +41 -0
  82. package/dist/js/next/components/DragDrop/DraggableDualListSelectorListItem.js.map +1 -0
  83. package/dist/js/next/components/DragDrop/Droppable.d.ts +16 -0
  84. package/dist/js/next/components/DragDrop/Droppable.d.ts.map +1 -0
  85. package/dist/js/next/components/DragDrop/Droppable.js +15 -0
  86. package/dist/js/next/components/DragDrop/Droppable.js.map +1 -0
  87. package/dist/js/next/components/DragDrop/DroppableContext.d.ts +6 -0
  88. package/dist/js/next/components/DragDrop/DroppableContext.d.ts.map +1 -0
  89. package/dist/js/next/components/DragDrop/DroppableContext.js +10 -0
  90. package/dist/js/next/components/DragDrop/DroppableContext.js.map +1 -0
  91. package/dist/js/next/components/DragDrop/index.d.ts +2 -0
  92. package/dist/js/next/components/DragDrop/index.d.ts.map +1 -0
  93. package/dist/js/next/components/DragDrop/index.js +5 -0
  94. package/dist/js/next/components/DragDrop/index.js.map +1 -0
  95. package/dist/js/next/components/index.d.ts +2 -0
  96. package/dist/js/next/components/index.d.ts.map +1 -0
  97. package/dist/js/next/components/index.js +5 -0
  98. package/dist/js/next/components/index.js.map +1 -0
  99. package/dist/js/next/index.d.ts +2 -0
  100. package/dist/js/next/index.d.ts.map +1 -0
  101. package/dist/js/next/index.js +5 -0
  102. package/dist/js/next/index.js.map +1 -0
  103. package/package.json +5 -9
  104. package/src/next/components/DragDrop/DragButton.tsx +27 -0
  105. package/src/next/components/DragDrop/DragDropSort.tsx +186 -0
  106. package/src/next/components/DragDrop/Draggable.tsx +58 -0
  107. package/src/next/components/DragDrop/DraggableDataListItem.tsx +61 -0
  108. package/src/next/components/DragDrop/DraggableDualListSelectorListItem.tsx +93 -0
  109. package/src/next/components/DragDrop/Droppable.tsx +27 -0
  110. package/src/next/components/DragDrop/DroppableContext.ts +6 -0
  111. package/src/next/components/DragDrop/__tests__/DragDrop.test.tsx +24 -0
  112. package/src/next/components/DragDrop/__tests__/__snapshots__/DragDrop.test.tsx.snap +59 -0
  113. package/src/next/components/DragDrop/examples/BasicSorting.tsx +19 -0
  114. package/src/next/components/DragDrop/examples/BasicSortingWithDragButton.tsx +20 -0
  115. package/src/next/components/DragDrop/examples/DataListDraggable.tsx +38 -0
  116. package/src/next/components/DragDrop/examples/DragDrop.md +32 -0
  117. package/src/next/components/DragDrop/examples/DragDropDemos.md +37 -0
  118. package/src/next/components/DragDrop/examples/DualListSelectorDraggable.tsx +154 -0
  119. package/src/next/components/DragDrop/index.ts +1 -0
  120. package/src/next/components/index.ts +1 -0
  121. package/src/next/index.ts +1 -0
@@ -0,0 +1,27 @@
1
+ import * as React from 'react';
2
+ import { css } from '@patternfly/react-styles';
3
+ import dragButtonStyles from '@patternfly/react-styles/css/components/DataList/data-list';
4
+ import buttonStyles from '@patternfly/react-styles/css/components/Button/button';
5
+ import GripVerticalIcon from '@patternfly/react-icons/dist/esm/icons/grip-vertical-icon';
6
+
7
+ export interface DragButtonProps extends React.HTMLProps<HTMLButtonElement> {
8
+ /** Additional classes added to the drag button */
9
+ className?: string;
10
+ /** Sets button type */
11
+ type?: 'button' | 'submit' | 'reset';
12
+ /** Flag indicating if drag is disabled for the item */
13
+ isDisabled?: boolean;
14
+ }
15
+
16
+ export const DragButton: React.FunctionComponent<DragButtonProps> = ({ className, ...props }: DragButtonProps) => (
17
+ <button
18
+ className={css(className, buttonStyles.button, buttonStyles.modifiers.plain)}
19
+ aria-label="Drag button"
20
+ {...props}
21
+ >
22
+ <span className={css(dragButtonStyles.dataListItemDraggableIcon)}>
23
+ <GripVerticalIcon />
24
+ </span>
25
+ </button>
26
+ );
27
+ DragButton.displayName = 'DragButton';
@@ -0,0 +1,186 @@
1
+ import * as React from 'react';
2
+ import * as ReactDOM from 'react-dom';
3
+ import { css } from '@patternfly/react-styles';
4
+ import {
5
+ DndContext,
6
+ closestCenter,
7
+ DragOverlay,
8
+ DndContextProps,
9
+ KeyboardSensor,
10
+ PointerSensor,
11
+ useSensor,
12
+ useSensors,
13
+ DragEndEvent,
14
+ DragStartEvent
15
+ } from '@dnd-kit/core';
16
+ import {
17
+ arrayMove,
18
+ SortableContext,
19
+ sortableKeyboardCoordinates,
20
+ verticalListSortingStrategy
21
+ } from '@dnd-kit/sortable';
22
+ import { Draggable } from './Draggable';
23
+ import { DraggableDataListItem } from './DraggableDataListItem';
24
+ import { DraggableDualListSelectorListItem } from './DraggableDualListSelectorListItem';
25
+ import styles from '@patternfly/react-styles/css/components/DragDrop/drag-drop';
26
+ import { canUseDOM } from '@patternfly/react-core';
27
+
28
+ export type DragDropSortDragEndEvent = DragEndEvent;
29
+ export type DragDropSortDragStartEvent = DragStartEvent;
30
+
31
+ export interface DraggableObject {
32
+ /** Unique id of the draggable object */
33
+ id: string;
34
+ /** Content rendered in the draggable object */
35
+ content: React.ReactNode;
36
+ /** Props spread to the rendered wrapper of the draggable object */
37
+ props?: any;
38
+ }
39
+
40
+ /**
41
+ * DragDropSortProps extends dnd-kit's props which may be viewed at https://docs.dndkit.com/api-documentation/context-provider#props.
42
+ */
43
+ export interface DragDropSortProps extends DndContextProps {
44
+ /** Custom defined content wrapper for draggable items. By default, draggable items are wrapped in a styled div.
45
+ * Intended to be a 'DataList' or 'DualListSelectorList' without children. */
46
+ children?: React.ReactElement;
47
+ /** Sorted array of draggable objects */
48
+ items: DraggableObject[];
49
+ /** Callback when user drops a draggable object */
50
+ onDrop: (event: DragDropSortDragEndEvent, items: DraggableObject[], oldIndex?: number, newIndex?: number) => void;
51
+ /** Callback when use begins dragging a draggable object */
52
+ onDrag?: (event: DragDropSortDragStartEvent, oldIndex: number) => void;
53
+ /** The variant determines which component wraps the draggable object.
54
+ * Default and defaultWithHandle varaints wrap the draggable object in a div.
55
+ * DataList vairant wraps the draggable object in a DataListItem
56
+ * DualListSelectorList variant wraps the draggable objects in a DualListSelectorListItem and a div.pf-c-dual-list-selector__item-text element
57
+ * TableComposable variant wraps the draggable objects in TODO
58
+ * */
59
+ variant?: 'default' | 'defaultWithHandle' | 'DataList' | 'DualListSelectorList' | 'TableComposable';
60
+ }
61
+
62
+ export const DragDropSort: React.FunctionComponent<DragDropSortProps> = ({
63
+ items,
64
+ onDrop = () => {},
65
+ onDrag = () => {},
66
+ variant = 'default',
67
+ children,
68
+ ...props
69
+ }: DragDropSortProps) => {
70
+ const [activeId, setActiveId] = React.useState<string>(null);
71
+ const itemIds = React.useMemo(() => (items ? Array.from(items, (item) => item.id as string) : []), [items]);
72
+
73
+ const getItemById = (id: string): DraggableObject => items.find((item) => item.id === id);
74
+
75
+ const sensors = useSensors(
76
+ useSensor(PointerSensor),
77
+ useSensor(KeyboardSensor, {
78
+ coordinateGetter: sortableKeyboardCoordinates
79
+ })
80
+ );
81
+
82
+ const handleDragEnd = (event: DragEndEvent) => {
83
+ const { active, over } = event;
84
+ const oldIndex = itemIds.indexOf(active.id as string);
85
+ const newIndex = itemIds.indexOf(over.id as string);
86
+ const newItems = arrayMove(items, oldIndex, newIndex);
87
+ onDrop(event, newItems, oldIndex, newIndex);
88
+ setActiveId(null);
89
+ };
90
+
91
+ const handleDragStart = (event: DragStartEvent) => {
92
+ setActiveId(event.active.id as string);
93
+ onDrag(event, itemIds.indexOf(event.active.id as string));
94
+ };
95
+
96
+ const getDragOverlay = () => {
97
+ if (!activeId) {
98
+ return;
99
+ }
100
+ const item = getItemById(activeId);
101
+
102
+ let content;
103
+ switch (variant) {
104
+ case 'DualListSelectorList':
105
+ content = (
106
+ <DraggableDualListSelectorListItem key={item.id} id={item.id} {...item.props}>
107
+ {item.content}
108
+ </DraggableDualListSelectorListItem>
109
+ );
110
+ break;
111
+ case 'DataList':
112
+ content = (
113
+ <DraggableDataListItem key={item.id} id={item.id} {...item.props}>
114
+ {item.content}
115
+ </DraggableDataListItem>
116
+ );
117
+ break;
118
+ default:
119
+ content = (
120
+ <Draggable useDragButton={variant === 'defaultWithHandle'} key={item.id} id={item.id} {...item.props}>
121
+ {item.content}
122
+ </Draggable>
123
+ );
124
+ }
125
+
126
+ return (
127
+ <div
128
+ className={css(styles.draggable, styles.modifiers.dragging)}
129
+ style={
130
+ {
131
+ '--pf-v5-c-draggable--m-dragging--BackgroundColor': 'var(--pf-v5-global--BackgroundColor--100)'
132
+ } as React.CSSProperties
133
+ }
134
+ >
135
+ {content}
136
+ </div>
137
+ );
138
+ };
139
+
140
+ const dragOverlay = <DragOverlay>{activeId && getDragOverlay()}</DragOverlay>;
141
+
142
+ const renderedChildren = (
143
+ <SortableContext items={itemIds} strategy={verticalListSortingStrategy} id="droppable">
144
+ {items.map((item: DraggableObject) => {
145
+ switch (variant) {
146
+ case 'DualListSelectorList':
147
+ return (
148
+ <DraggableDualListSelectorListItem key={item.id} id={item.id} {...item.props}>
149
+ {item.content}
150
+ </DraggableDualListSelectorListItem>
151
+ );
152
+ case 'DataList':
153
+ return (
154
+ <DraggableDataListItem key={item.id} id={item.id} {...item.props}>
155
+ {item.content}
156
+ </DraggableDataListItem>
157
+ );
158
+ default:
159
+ return (
160
+ <Draggable useDragButton={variant === 'defaultWithHandle'} key={item.id} id={item.id} {...item.props}>
161
+ {item.content}
162
+ </Draggable>
163
+ );
164
+ }
165
+ })}
166
+ {canUseDOM ? ReactDOM.createPortal(dragOverlay, document.getElementById('root')) : dragOverlay}
167
+ </SortableContext>
168
+ );
169
+
170
+ return (
171
+ <DndContext
172
+ sensors={sensors}
173
+ collisionDetection={closestCenter}
174
+ onDragEnd={handleDragEnd}
175
+ onDragStart={handleDragStart}
176
+ {...props}
177
+ >
178
+ {children &&
179
+ React.cloneElement(children, {
180
+ children: renderedChildren
181
+ })}
182
+ {!children && <div>{renderedChildren}</div>}
183
+ </DndContext>
184
+ );
185
+ };
186
+ DragDropSort.displayName = 'DragDropSort';
@@ -0,0 +1,58 @@
1
+ import * as React from 'react';
2
+ import { useSortable } from '@dnd-kit/sortable';
3
+ import { CSS } from '@dnd-kit/utilities';
4
+ import { css } from '@patternfly/react-styles';
5
+ import styles from '@patternfly/react-styles/css/components/DragDrop/drag-drop';
6
+ import { DragButton } from './DragButton';
7
+
8
+ export interface DraggableProps extends React.HTMLProps<HTMLDivElement> {
9
+ /** Content rendered inside DragDrop */
10
+ children?: React.ReactNode;
11
+ /** Class to add to outer div */
12
+ className?: string;
13
+ /** @hide Id of the sortable context. */
14
+ id?: string;
15
+ /** Flag indicating the draggable element should include a drag button. */
16
+ useDragButton?: boolean;
17
+ }
18
+
19
+ export const Draggable: React.FunctionComponent<DraggableProps> = ({
20
+ children,
21
+ id,
22
+ className,
23
+ useDragButton = false,
24
+ ...props
25
+ }: DraggableProps) => {
26
+ const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
27
+ id
28
+ });
29
+
30
+ const style = {
31
+ transform: CSS.Transform.toString(transform),
32
+ transition
33
+ };
34
+
35
+ return useDragButton ? (
36
+ <div
37
+ className={css(isDragging && styles.droppable, isDragging && styles.modifiers.dragging, className)}
38
+ ref={setNodeRef}
39
+ style={style}
40
+ {...props}
41
+ >
42
+ <DragButton {...attributes} {...listeners} />
43
+ {children}
44
+ </div>
45
+ ) : (
46
+ <div
47
+ className={css(isDragging && styles.droppable, isDragging && styles.modifiers.dragging, className)}
48
+ ref={setNodeRef}
49
+ style={style}
50
+ {...listeners}
51
+ {...attributes}
52
+ {...props}
53
+ >
54
+ {children}
55
+ </div>
56
+ );
57
+ };
58
+ Draggable.displayName = 'Draggable';
@@ -0,0 +1,61 @@
1
+ import * as React from 'react';
2
+ import { useSortable } from '@dnd-kit/sortable';
3
+ import { CSS } from '@dnd-kit/utilities';
4
+ import { css } from '@patternfly/react-styles';
5
+ import styles from '@patternfly/react-styles/css/components/DataList/data-list';
6
+ import dragStyles from '@patternfly/react-styles/css/components/DragDrop/drag-drop';
7
+ import { DragButton } from './DragButton';
8
+ import { DataListItemRow, DataListControl } from '@patternfly/react-core';
9
+
10
+ export interface DraggableDataListItemObject {
11
+ id?: string;
12
+ content?: React.ReactNode;
13
+ }
14
+
15
+ export interface DraggableDataListItemProps extends React.HTMLProps<HTMLLIElement> {
16
+ /** Content rendered inside DragDrop */
17
+ children?: React.ReactNode;
18
+ /** Class to add to outer div */
19
+ className?: string;
20
+ /** @hide Id of the sortable context. */
21
+ id?: string;
22
+ }
23
+
24
+ export const DraggableDataListItem: React.FunctionComponent<DraggableDataListItemProps> = ({
25
+ children,
26
+ id,
27
+ className,
28
+ ...props
29
+ }: DraggableDataListItemProps) => {
30
+ const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
31
+ id
32
+ });
33
+
34
+ const style = {
35
+ transform: CSS.Transform.toString(transform),
36
+ transition
37
+ };
38
+
39
+ return (
40
+ <li
41
+ id={id}
42
+ className={css(
43
+ styles.dataListItem,
44
+ isDragging && dragStyles.droppable,
45
+ isDragging && dragStyles.modifiers.dragging,
46
+ className
47
+ )}
48
+ {...props}
49
+ ref={setNodeRef}
50
+ style={style}
51
+ >
52
+ <DataListItemRow>
53
+ <DataListControl>
54
+ <DragButton className={css(styles.dataListItemDraggableButton)} {...attributes} {...listeners} />
55
+ </DataListControl>
56
+ {children}
57
+ </DataListItemRow>
58
+ </li>
59
+ );
60
+ };
61
+ DraggableDataListItem.displayName = 'DraggableDataListItem';
@@ -0,0 +1,93 @@
1
+ import * as React from 'react';
2
+ import { useSortable } from '@dnd-kit/sortable';
3
+ import { CSS } from '@dnd-kit/utilities';
4
+ import { css } from '@patternfly/react-styles';
5
+ import styles from '@patternfly/react-styles/css/components/DualListSelector/dual-list-selector';
6
+ import dragStyles from '@patternfly/react-styles/css/components/DragDrop/drag-drop';
7
+ import { DragButton } from './DragButton';
8
+ import { DualListSelectorListContext } from '@patternfly/react-core/dist/esm/components/DualListSelector';
9
+
10
+ export interface DraggableDualListSelectorListItemProps extends React.HTMLProps<HTMLLIElement> {
11
+ /** Content rendered inside DragDrop */
12
+ children?: React.ReactNode;
13
+ /** Don't wrap the component in a div. Requires passing a single child. */
14
+ hasNoWrapper?: boolean;
15
+ /** Class to add to outer div */
16
+ className?: string;
17
+ /** @hide Id of the sortable context */
18
+ id?: string;
19
+ /** Flag indicating the list item is currently selected. */
20
+ isSelected?: boolean;
21
+ /** Callback fired when an option is selected. */
22
+ onOptionSelect?: (e: React.MouseEvent | React.ChangeEvent | React.KeyboardEvent, id?: string) => void;
23
+ /** @hide Internal field used to keep track of order of unfiltered options. */
24
+ orderIndex?: number;
25
+ /** @hide Forwarded ref */
26
+ innerRef?: React.RefObject<HTMLLIElement>;
27
+ /** Flag indicating if the dual list selector is in a disabled state */
28
+ isDisabled?: boolean;
29
+ }
30
+
31
+ export const DraggableDualListSelectorListItem: React.FunctionComponent<DraggableDualListSelectorListItemProps> = ({
32
+ children,
33
+ id,
34
+ className,
35
+ orderIndex,
36
+ isSelected,
37
+ onOptionSelect,
38
+ ...props
39
+ }: DraggableDualListSelectorListItemProps) => {
40
+ const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
41
+ id,
42
+ animateLayoutChanges: () => false
43
+ });
44
+
45
+ const { setFocusedOption } = React.useContext(DualListSelectorListContext);
46
+
47
+ const style = {
48
+ transform: CSS.Transform.toString(transform),
49
+ transition
50
+ };
51
+
52
+ return (
53
+ <li
54
+ className={css(
55
+ styles.dualListSelectorListItem,
56
+ isDragging && dragStyles.droppable,
57
+ isDragging && dragStyles.modifiers.dragging,
58
+ className
59
+ )}
60
+ key={orderIndex}
61
+ id={id}
62
+ role="presentation"
63
+ {...props}
64
+ ref={setNodeRef}
65
+ style={style}
66
+ >
67
+ <div className={css(styles.dualListSelectorListItemRow, isSelected && styles.modifiers.selected)}>
68
+ <DragButton className={css(styles.dualListSelectorDraggable)} {...attributes} {...listeners} />
69
+ <span
70
+ className={css(styles.dualListSelectorItem)}
71
+ onKeyDown={(e: React.KeyboardEvent) => {
72
+ if (e.key === ' ' || e.key === 'Enter') {
73
+ (document.activeElement as HTMLElement).click();
74
+ e.preventDefault();
75
+ }
76
+ }}
77
+ aria-selected={isSelected}
78
+ onClick={(e: React.MouseEvent) => {
79
+ setFocusedOption(id);
80
+ onOptionSelect(e, id);
81
+ }}
82
+ role="option"
83
+ tabIndex={-1}
84
+ >
85
+ <span className={css(styles.dualListSelectorItemMain)}>
86
+ <span className={css(styles.dualListSelectorItemText)}>{children}</span>
87
+ </span>
88
+ </span>
89
+ </div>
90
+ </li>
91
+ );
92
+ };
93
+ DraggableDualListSelectorListItem.displayName = 'DraggableDualListSelectorListItem';
@@ -0,0 +1,27 @@
1
+ import * as React from 'react';
2
+ import { useDroppable } from '@dnd-kit/core';
3
+
4
+ interface DroppableProps extends React.HTMLProps<HTMLDivElement> {
5
+ /** Content rendered inside DragDrop */
6
+ children?: React.ReactNode;
7
+ /** Class to add to outer div */
8
+ className?: string;
9
+ /** Name of zone that items can be dragged between. Should specify if there is more than one Droppable on the page. */
10
+ zone?: string;
11
+ /** Id to be passed back on drop events */
12
+ droppableId?: string;
13
+ /** Don't wrap the component in a div. Requires passing a single child. */
14
+ hasNoWrapper?: boolean;
15
+ }
16
+
17
+ export const Droppable: React.FunctionComponent<DroppableProps> = ({ children, ...props }: DroppableProps) => {
18
+ const { isOver, setNodeRef } = useDroppable({ id: 'droppable' });
19
+ const style = { color: isOver ? 'green' : undefined };
20
+
21
+ return (
22
+ <div ref={setNodeRef} style={style} {...props}>
23
+ {children}
24
+ </div>
25
+ );
26
+ };
27
+ Droppable.displayName = 'Droppable';
@@ -0,0 +1,6 @@
1
+ import * as React from 'react';
2
+
3
+ export const DroppableContext = React.createContext({
4
+ zone: 'defaultDroppableZone',
5
+ droppableId: 'defaultDroppableId'
6
+ });
@@ -0,0 +1,24 @@
1
+ import React from 'react';
2
+ import { render } from '@testing-library/react';
3
+ import { DragDropSort } from '../';
4
+
5
+ jest.mock('react-dom', () => ({
6
+ ...jest.requireActual('react-dom'),
7
+ createPortal: jest.fn((node) => node)
8
+ }));
9
+
10
+ test('renders some divs', () => {
11
+ const { asFragment } = render(
12
+ <div className="pf-c-droppable pf-m-dragging">
13
+ <DragDropSort
14
+ items={[
15
+ { id: '1', content: 'one' },
16
+ { id: '2', content: 'two' },
17
+ { id: '3', content: 'three' }
18
+ ]}
19
+ onDrop={() => {}}
20
+ />
21
+ </div>
22
+ );
23
+ expect(asFragment()).toMatchSnapshot();
24
+ });
@@ -0,0 +1,59 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`renders some divs 1`] = `
4
+ <DocumentFragment>
5
+ <div
6
+ class="pf-c-droppable pf-m-dragging"
7
+ >
8
+ <div>
9
+ <div
10
+ aria-describedby="DndDescribedBy-0"
11
+ aria-disabled="false"
12
+ aria-roledescription="sortable"
13
+ class=""
14
+ role="button"
15
+ tabindex="0"
16
+ >
17
+ one
18
+ </div>
19
+ <div
20
+ aria-describedby="DndDescribedBy-0"
21
+ aria-disabled="false"
22
+ aria-roledescription="sortable"
23
+ class=""
24
+ role="button"
25
+ tabindex="0"
26
+ >
27
+ two
28
+ </div>
29
+ <div
30
+ aria-describedby="DndDescribedBy-0"
31
+ aria-disabled="false"
32
+ aria-roledescription="sortable"
33
+ class=""
34
+ role="button"
35
+ tabindex="0"
36
+ >
37
+ three
38
+ </div>
39
+ </div>
40
+ <div
41
+ id="DndDescribedBy-0"
42
+ style="display: none;"
43
+ >
44
+
45
+ To pick up a draggable item, press the space bar.
46
+ While dragging, use the arrow keys to move the item.
47
+ Press space again to drop the item in its new position, or press escape to cancel.
48
+
49
+ </div>
50
+ <div
51
+ aria-atomic="true"
52
+ aria-live="assertive"
53
+ id="DndLiveRegion-0"
54
+ role="status"
55
+ style="position: fixed; width: 1px; height: 1px; margin: -1px; border: 0px; padding: 0px; overflow: hidden; clip-path: inset(100%); white-space: nowrap;"
56
+ />
57
+ </div>
58
+ </DocumentFragment>
59
+ `;
@@ -0,0 +1,19 @@
1
+ import React from 'react';
2
+ import { DragDropSort, DraggableObject } from '@patternfly/react-drag-drop';
3
+
4
+ export const BasicSorting: React.FunctionComponent = () => {
5
+ const [items, setItems] = React.useState<DraggableObject[]>([
6
+ { id: 'basic-1', content: 'one' },
7
+ { id: 'basic-2', content: 'two' },
8
+ { id: 'basic-3', content: 'three' }
9
+ ]);
10
+
11
+ return (
12
+ <DragDropSort
13
+ items={items}
14
+ onDrop={(_, newItems) => {
15
+ setItems(newItems);
16
+ }}
17
+ />
18
+ );
19
+ };
@@ -0,0 +1,20 @@
1
+ import React from 'react';
2
+ import { DragDropSort, DraggableObject } from '@patternfly/react-drag-drop';
3
+
4
+ export const BasicSortingWithDragButton: React.FunctionComponent = () => {
5
+ const [items, setItems] = React.useState<DraggableObject[]>([
6
+ { id: 'with-button-1', content: 'one' },
7
+ { id: 'with-button-2', content: 'two' },
8
+ { id: 'with-button-3', content: 'three' }
9
+ ]);
10
+
11
+ return (
12
+ <DragDropSort
13
+ items={items}
14
+ variant="defaultWithHandle"
15
+ onDrop={(_, newItems) => {
16
+ setItems(newItems);
17
+ }}
18
+ />
19
+ );
20
+ };
@@ -0,0 +1,38 @@
1
+ import React from 'react';
2
+ import { DataList, DataListCell, DataListCheck, DataListControl, DataListItemCells } from '@patternfly/react-core';
3
+ import { DragDropSort, DraggableObject } from '@patternfly/react-drag-drop';
4
+
5
+ const getItems = (count: number): DraggableObject[] =>
6
+ Array.from({ length: count }, (_, idx) => idx).map((idx) => ({
7
+ id: `data-list-item-${idx}`,
8
+ content: (
9
+ <>
10
+ <DataListControl>
11
+ <DataListCheck aria-labelledby={`item-${idx}`} name={`item-${idx}`} otherControls />
12
+ </DataListControl>
13
+ <DataListItemCells
14
+ dataListCells={[
15
+ <DataListCell key={`item-${idx}`}>
16
+ <span id={`item-${idx}`}>{`item-${idx}`}</span>
17
+ </DataListCell>
18
+ ]}
19
+ />
20
+ </>
21
+ )
22
+ }));
23
+
24
+ export const DataListDraggable: React.FunctionComponent = (props) => {
25
+ const [items, setItems] = React.useState<DraggableObject[]>(getItems(10));
26
+
27
+ return (
28
+ <DragDropSort
29
+ items={items}
30
+ onDrop={(_, newItems) => {
31
+ setItems(newItems);
32
+ }}
33
+ variant="DataList"
34
+ >
35
+ <DataList aria-label="draggable data list example" isCompact {...props} />
36
+ </DragDropSort>
37
+ );
38
+ };
@@ -0,0 +1,32 @@
1
+ ---
2
+ id: Drag and drop
3
+ section: components
4
+ cssPrefix: pf-c-drag-drop
5
+ propComponents: ['DragDropSort', 'DraggableObject']
6
+ hideNavItem: true
7
+ beta: true
8
+ ---
9
+
10
+ Note: This drag and drop implementation lives in its own package at [@patternfly/react-drag-drop](https://www.npmjs.com/package/@patternfly/react-drag-drop)!
11
+
12
+ import AngleDoubleLeftIcon from '@patternfly/react-icons/dist/esm/icons/angle-double-left-icon';
13
+ import AngleLeftIcon from '@patternfly/react-icons/dist/esm/icons/angle-left-icon';
14
+ import AngleDoubleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-double-right-icon';
15
+ import AngleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-right-icon';
16
+ import PficonSortCommonAscIcon from '@patternfly/react-icons/dist/esm/icons/pficon-sort-common-asc-icon';
17
+
18
+ import { DragDropSort } from '@patternfly/react-drag-drop';
19
+
20
+ ## Sorting examples
21
+
22
+ ### Basic drag and drop sorting
23
+
24
+ ```ts file="./BasicSorting.tsx"
25
+
26
+ ```
27
+
28
+ ### Basic drag and drop sorting with drag button
29
+
30
+ ```ts file="./BasicSortingWithDragButton.tsx"
31
+
32
+ ```