@patternfly/react-drag-drop 5.0.0-alpha.0

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 (102) hide show
  1. package/dist/cjs.tsbuildinfo +1 -0
  2. package/dist/esm/components/DragDrop/DragButton.d.ts +11 -0
  3. package/dist/esm/components/DragDrop/DragButton.d.ts.map +1 -0
  4. package/dist/esm/components/DragDrop/DragButton.js +13 -0
  5. package/dist/esm/components/DragDrop/DragButton.js.map +1 -0
  6. package/dist/esm/components/DragDrop/DragDropSort.d.ts +27 -0
  7. package/dist/esm/components/DragDrop/DragDropSort.d.ts.map +1 -0
  8. package/dist/esm/components/DragDrop/DragDropSort.js +45 -0
  9. package/dist/esm/components/DragDrop/DragDropSort.js.map +1 -0
  10. package/dist/esm/components/DragDrop/Draggable.d.ts +15 -0
  11. package/dist/esm/components/DragDrop/Draggable.d.ts.map +1 -0
  12. package/dist/esm/components/DragDrop/Draggable.js +22 -0
  13. package/dist/esm/components/DragDrop/Draggable.js.map +1 -0
  14. package/dist/esm/components/DragDrop/DraggableDataListItem.d.ts +17 -0
  15. package/dist/esm/components/DragDrop/DraggableDataListItem.d.ts.map +1 -0
  16. package/dist/esm/components/DragDrop/DraggableDataListItem.js +25 -0
  17. package/dist/esm/components/DragDrop/DraggableDataListItem.js.map +1 -0
  18. package/dist/esm/components/DragDrop/DraggableDualListSelectorListItem.d.ts +27 -0
  19. package/dist/esm/components/DragDrop/DraggableDualListSelectorListItem.d.ts.map +1 -0
  20. package/dist/esm/components/DragDrop/DraggableDualListSelectorListItem.js +35 -0
  21. package/dist/esm/components/DragDrop/DraggableDualListSelectorListItem.js.map +1 -0
  22. package/dist/esm/components/DragDrop/Droppable.d.ts +16 -0
  23. package/dist/esm/components/DragDrop/Droppable.d.ts.map +1 -0
  24. package/dist/esm/components/DragDrop/Droppable.js +11 -0
  25. package/dist/esm/components/DragDrop/Droppable.js.map +1 -0
  26. package/dist/esm/components/DragDrop/DroppableContext.d.ts +6 -0
  27. package/dist/esm/components/DragDrop/DroppableContext.d.ts.map +1 -0
  28. package/dist/esm/components/DragDrop/DroppableContext.js +6 -0
  29. package/dist/esm/components/DragDrop/DroppableContext.js.map +1 -0
  30. package/dist/esm/components/DragDrop/index.d.ts +2 -0
  31. package/dist/esm/components/DragDrop/index.d.ts.map +1 -0
  32. package/dist/esm/components/DragDrop/index.js +2 -0
  33. package/dist/esm/components/DragDrop/index.js.map +1 -0
  34. package/dist/esm/components/index.d.ts +2 -0
  35. package/dist/esm/components/index.d.ts.map +1 -0
  36. package/dist/esm/components/index.js +2 -0
  37. package/dist/esm/components/index.js.map +1 -0
  38. package/dist/esm/index.d.ts +2 -0
  39. package/dist/esm/index.d.ts.map +1 -0
  40. package/dist/esm/index.js +2 -0
  41. package/dist/esm/index.js.map +1 -0
  42. package/dist/esm.tsbuildinfo +1 -0
  43. package/dist/js/components/DragDrop/DragButton.d.ts +11 -0
  44. package/dist/js/components/DragDrop/DragButton.d.ts.map +1 -0
  45. package/dist/js/components/DragDrop/DragButton.js +17 -0
  46. package/dist/js/components/DragDrop/DragButton.js.map +1 -0
  47. package/dist/js/components/DragDrop/DragDropSort.d.ts +27 -0
  48. package/dist/js/components/DragDrop/DragDropSort.d.ts.map +1 -0
  49. package/dist/js/components/DragDrop/DragDropSort.js +50 -0
  50. package/dist/js/components/DragDrop/DragDropSort.js.map +1 -0
  51. package/dist/js/components/DragDrop/Draggable.d.ts +15 -0
  52. package/dist/js/components/DragDrop/Draggable.d.ts.map +1 -0
  53. package/dist/js/components/DragDrop/Draggable.js +26 -0
  54. package/dist/js/components/DragDrop/Draggable.js.map +1 -0
  55. package/dist/js/components/DragDrop/DraggableDataListItem.d.ts +17 -0
  56. package/dist/js/components/DragDrop/DraggableDataListItem.d.ts.map +1 -0
  57. package/dist/js/components/DragDrop/DraggableDataListItem.js +29 -0
  58. package/dist/js/components/DragDrop/DraggableDataListItem.js.map +1 -0
  59. package/dist/js/components/DragDrop/DraggableDualListSelectorListItem.d.ts +27 -0
  60. package/dist/js/components/DragDrop/DraggableDualListSelectorListItem.d.ts.map +1 -0
  61. package/dist/js/components/DragDrop/DraggableDualListSelectorListItem.js +39 -0
  62. package/dist/js/components/DragDrop/DraggableDualListSelectorListItem.js.map +1 -0
  63. package/dist/js/components/DragDrop/Droppable.d.ts +16 -0
  64. package/dist/js/components/DragDrop/Droppable.d.ts.map +1 -0
  65. package/dist/js/components/DragDrop/Droppable.js +15 -0
  66. package/dist/js/components/DragDrop/Droppable.js.map +1 -0
  67. package/dist/js/components/DragDrop/DroppableContext.d.ts +6 -0
  68. package/dist/js/components/DragDrop/DroppableContext.d.ts.map +1 -0
  69. package/dist/js/components/DragDrop/DroppableContext.js +10 -0
  70. package/dist/js/components/DragDrop/DroppableContext.js.map +1 -0
  71. package/dist/js/components/DragDrop/index.d.ts +2 -0
  72. package/dist/js/components/DragDrop/index.d.ts.map +1 -0
  73. package/dist/js/components/DragDrop/index.js +5 -0
  74. package/dist/js/components/DragDrop/index.js.map +1 -0
  75. package/dist/js/components/index.d.ts +2 -0
  76. package/dist/js/components/index.d.ts.map +1 -0
  77. package/dist/js/components/index.js +5 -0
  78. package/dist/js/components/index.js.map +1 -0
  79. package/dist/js/index.d.ts +2 -0
  80. package/dist/js/index.d.ts.map +1 -0
  81. package/dist/js/index.js +5 -0
  82. package/dist/js/index.js.map +1 -0
  83. package/package.json +50 -0
  84. package/src/components/DragDrop/DragButton.tsx +22 -0
  85. package/src/components/DragDrop/DragDropSort.tsx +123 -0
  86. package/src/components/DragDrop/Draggable.tsx +55 -0
  87. package/src/components/DragDrop/DraggableDataListItem.tsx +51 -0
  88. package/src/components/DragDrop/DraggableDualListSelectorListItem.tsx +90 -0
  89. package/src/components/DragDrop/Droppable.tsx +27 -0
  90. package/src/components/DragDrop/DroppableContext.ts +6 -0
  91. package/src/components/DragDrop/__tests__/DragDrop.test.tsx +19 -0
  92. package/src/components/DragDrop/__tests__/__snapshots__/DragDrop.test.tsx.snap +57 -0
  93. package/src/components/DragDrop/examples/BasicSorting.tsx +24 -0
  94. package/src/components/DragDrop/examples/BasicSortingWithDragButton.tsx +25 -0
  95. package/src/components/DragDrop/examples/DataListDraggable.tsx +46 -0
  96. package/src/components/DragDrop/examples/DragDrop.md +57 -0
  97. package/src/components/DragDrop/examples/DualListSelectorDraggable.tsx +174 -0
  98. package/src/components/DragDrop/index.ts +1 -0
  99. package/src/components/index.ts +1 -0
  100. package/src/index.ts +1 -0
  101. package/tsconfig.cjs.json +8 -0
  102. package/tsconfig.json +23 -0
@@ -0,0 +1,51 @@
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 { DragButton } from './DragButton';
7
+ import { DataListItemRow, DataListControl } from '@patternfly/react-core';
8
+
9
+ export interface DraggableDataListItemObject {
10
+ id?: string;
11
+ content?: React.ReactNode;
12
+ }
13
+
14
+ export interface DraggableDataListItemProps extends React.HTMLProps<HTMLLIElement> {
15
+ /** Content rendered inside DragDrop */
16
+ children?: React.ReactNode;
17
+ /** Don't wrap the component in a div. Requires passing a single child. */
18
+ hasNoWrapper?: boolean;
19
+ /** Class to add to outer div */
20
+ className?: string;
21
+ /** */
22
+ id?: string;
23
+ }
24
+
25
+ export const DraggableDataListItem: React.FunctionComponent<DraggableDataListItemProps> = ({
26
+ children,
27
+ id,
28
+ className,
29
+ ...props
30
+ }: DraggableDataListItemProps) => {
31
+ const { attributes, listeners, setNodeRef, transform, transition } = useSortable({
32
+ id
33
+ });
34
+
35
+ const style = {
36
+ transform: CSS.Transform.toString(transform),
37
+ transition
38
+ };
39
+
40
+ return (
41
+ <li id={id} className={css(styles.dataListItem, className)} {...props} ref={setNodeRef} style={style}>
42
+ <DataListItemRow>
43
+ <DataListControl>
44
+ <DragButton className={css(styles.dataListItemDraggableButton)} {...attributes} {...listeners} />
45
+ </DataListControl>
46
+ {children}
47
+ </DataListItemRow>
48
+ </li>
49
+ );
50
+ };
51
+ DraggableDataListItem.displayName = 'DraggableDataListItem';
@@ -0,0 +1,90 @@
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 { DragButton } from './DragButton';
7
+ import { DualListSelectorListContext } from '@patternfly/react-core';
8
+
9
+ export interface DraggableObject {
10
+ id?: string;
11
+ content?: React.ReactNode;
12
+ }
13
+
14
+ export interface DraggableDualListSelectorListItemProps extends React.HTMLProps<HTMLLIElement> {
15
+ /** Content rendered inside DragDrop */
16
+ children?: React.ReactNode;
17
+ /** Don't wrap the component in a div. Requires passing a single child. */
18
+ hasNoWrapper?: boolean;
19
+ /** Class to add to outer div */
20
+ className?: string;
21
+ /** */
22
+ id?: string;
23
+ /** Flag indicating the list item is currently selected. */
24
+ isSelected?: boolean;
25
+ /** Callback fired when an option is selected. */
26
+ onOptionSelect?: (e: React.MouseEvent | React.ChangeEvent | React.KeyboardEvent, id?: string) => void;
27
+ /** @hide Internal field used to keep track of order of unfiltered options. */
28
+ orderIndex?: number;
29
+ /** @hide Forwarded ref */
30
+ innerRef?: React.RefObject<HTMLLIElement>;
31
+ /** Flag indicating if the dual list selector is in a disabled state */
32
+ isDisabled?: boolean;
33
+ }
34
+
35
+ export const DraggableDualListSelectorListItem: React.FunctionComponent<DraggableDualListSelectorListItemProps> = ({
36
+ children,
37
+ id,
38
+ className,
39
+ orderIndex,
40
+ isSelected,
41
+ onOptionSelect,
42
+ ...props
43
+ }: DraggableDualListSelectorListItemProps) => {
44
+ const { attributes, listeners, setNodeRef, transform, transition } = useSortable({
45
+ id
46
+ });
47
+
48
+ const { setFocusedOption } = React.useContext(DualListSelectorListContext);
49
+
50
+ const style = {
51
+ transform: CSS.Transform.toString(transform),
52
+ transition
53
+ };
54
+
55
+ return (
56
+ <li
57
+ className={css(styles.dualListSelectorListItem, className)}
58
+ key={orderIndex}
59
+ id={id}
60
+ ref={setNodeRef}
61
+ style={style}
62
+ {...props}
63
+ >
64
+ <div className={css(styles.dualListSelectorListItemRow, isSelected && styles.modifiers.selected)}>
65
+ <DragButton className={css(styles.dualListSelectorDraggable)} {...attributes} {...listeners} />
66
+ <span
67
+ className={css(styles.dualListSelectorItem)}
68
+ onKeyDown={(e: React.KeyboardEvent) => {
69
+ if (e.key === ' ' || e.key === 'Enter') {
70
+ (document.activeElement as HTMLElement).click();
71
+ e.preventDefault();
72
+ }
73
+ }}
74
+ aria-selected={isSelected}
75
+ onClick={(e: React.MouseEvent) => {
76
+ setFocusedOption(id);
77
+ onOptionSelect(e, id);
78
+ }}
79
+ role="option"
80
+ tabIndex={-1}
81
+ >
82
+ <span className={css(styles.dualListSelectorItemMain)}>
83
+ <span className={css(styles.dualListSelectorItemText)}>{children}</span>
84
+ </span>
85
+ </span>
86
+ </div>
87
+ </li>
88
+ );
89
+ };
90
+ 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,19 @@
1
+ import React from 'react';
2
+ import { render } from '@testing-library/react';
3
+ import { DragDropSort } from '../';
4
+
5
+ test('renders some divs', () => {
6
+ const { asFragment } = render(
7
+ <div className='pf-c-droppable pf-m-dragging'>
8
+ <DragDropSort
9
+ items={[
10
+ { id: '1', content: 'one' },
11
+ { id: '2', content: 'two' },
12
+ { id: '3', content: 'three' }
13
+ ]}
14
+ onDrop={() => {}}
15
+ />
16
+ </div>
17
+ );
18
+ expect(asFragment()).toMatchSnapshot();
19
+ });
@@ -0,0 +1,57 @@
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
+ aria-describedby="DndDescribedBy-0"
10
+ aria-disabled="false"
11
+ aria-roledescription="sortable"
12
+ class="pf-c-draggable"
13
+ role="button"
14
+ tabindex="0"
15
+ >
16
+ one
17
+ </div>
18
+ <div
19
+ aria-describedby="DndDescribedBy-0"
20
+ aria-disabled="false"
21
+ aria-roledescription="sortable"
22
+ class="pf-c-draggable"
23
+ role="button"
24
+ tabindex="0"
25
+ >
26
+ two
27
+ </div>
28
+ <div
29
+ aria-describedby="DndDescribedBy-0"
30
+ aria-disabled="false"
31
+ aria-roledescription="sortable"
32
+ class="pf-c-draggable"
33
+ role="button"
34
+ tabindex="0"
35
+ >
36
+ three
37
+ </div>
38
+ <div
39
+ id="DndDescribedBy-0"
40
+ style="display: none;"
41
+ >
42
+
43
+ To pick up a draggable item, press the space bar.
44
+ While dragging, use the arrow keys to move the item.
45
+ Press space again to drop the item in its new position, or press escape to cancel.
46
+
47
+ </div>
48
+ <div
49
+ aria-atomic="true"
50
+ aria-live="assertive"
51
+ id="DndLiveRegion-0"
52
+ role="status"
53
+ style="position: fixed; width: 1px; height: 1px; margin: -1px; border: 0px; padding: 0px; overflow: hidden; clip-path: inset(100%); white-space: nowrap;"
54
+ />
55
+ </div>
56
+ </DocumentFragment>
57
+ `;
@@ -0,0 +1,24 @@
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: '1', content: 'one' },
7
+ { id: '2', content: 'two' },
8
+ { id: '3', content: 'three' }
9
+ ]);
10
+ const [dragging, setDragging] = React.useState(false);
11
+
12
+ return (
13
+ <div className={`pf-c-droppable ${dragging ? 'pf-m-dragging' : ''}`}>
14
+ <DragDropSort
15
+ items={items}
16
+ onDrop={newItems => {
17
+ setItems(newItems);
18
+ setDragging(false);
19
+ }}
20
+ onDrag={() => setDragging(true)}
21
+ />
22
+ </div>
23
+ );
24
+ };
@@ -0,0 +1,25 @@
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: '1', content: 'one' },
7
+ { id: '2', content: 'two' },
8
+ { id: '3', content: 'three' }
9
+ ]);
10
+ const [dragging, setDragging] = React.useState(false);
11
+
12
+ return (
13
+ <div className={`pf-c-droppable ${dragging ? 'pf-m-dragging' : ''}`}>
14
+ <DragDropSort
15
+ items={items}
16
+ variant="defaultWithHandle"
17
+ onDrop={newItems => {
18
+ setItems(newItems);
19
+ setDragging(false);
20
+ }}
21
+ onDrag={() => setDragging(true)}
22
+ />
23
+ </div>
24
+ );
25
+ };
@@ -0,0 +1,46 @@
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: `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
+ const [dragging, setDragging] = React.useState(false);
27
+
28
+ return (
29
+ <DataList
30
+ aria-label="draggable data list example"
31
+ isCompact
32
+ className={`pf-c-droppable ${dragging ? 'pf-m-dragging' : ''}`}
33
+ {...props}
34
+ >
35
+ <DragDropSort
36
+ items={items}
37
+ onDrop={newItems => {
38
+ setItems(newItems);
39
+ setDragging(false);
40
+ }}
41
+ onDrag={() => setDragging(true)}
42
+ variant="DataList"
43
+ />
44
+ </DataList>
45
+ );
46
+ };
@@ -0,0 +1,57 @@
1
+ ---
2
+ id: Drag and drop
3
+ section: components
4
+ cssPrefix: pf-c-drag-drop
5
+ propComponents: [
6
+ DragDropSort,
7
+ DraggableObject
8
+ ]
9
+ hideNavItem: true
10
+ ---
11
+ Note: DragDrop lives in its own package at @patternfly/react-drag-drop!'
12
+
13
+ import AngleDoubleLeftIcon from '@patternfly/react-icons/dist/esm/icons/angle-double-left-icon';
14
+ import AngleLeftIcon from '@patternfly/react-icons/dist/esm/icons/angle-left-icon';
15
+ import AngleDoubleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-double-right-icon';
16
+ import AngleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-right-icon';
17
+ import PficonSortCommonAscIcon from '@patternfly/react-icons/dist/esm/icons/pficon-sort-common-asc-icon';
18
+
19
+ import { DragDropSort, DraggableObject } from '@patternfly/react-drag-drop';
20
+
21
+ ## Sorting examples
22
+ ### Basic drag and drop sorting
23
+ ```ts isBeta file="./BasicSorting.tsx"
24
+ ```
25
+
26
+ ### Basic drag and drop sorting with drag button
27
+
28
+ ```ts isBeta file="./BasicSortingWithDragButton.tsx"
29
+ ```
30
+
31
+ ### Drag and drop sortable data list
32
+
33
+ Draggable data lists used to have their own HTML5-based API for drag and drop, which wasn't able to fulfill requirements such as custom styling on items being dragged. So we wrote generic `DragDrop`, `Draggable`, and `Droppable` components for this purpose. Use those new components instead of the deprecated (and buggy!) HTML5-based API.
34
+
35
+ Note: Keyboard accessibility and screen reader accessibility for the `DragDrop` component are still in development.
36
+
37
+ ```ts isBeta file="./DataListDraggable.tsx"
38
+ ```
39
+
40
+ ### Drag and drop sortable dual list selector
41
+
42
+ This example only allows reordering the contents of the "chosen" pane with drag and drop. To make a pane able to be reordered:
43
+
44
+ - wrap the `DualListSelectorPane` in a `DragDropSort` component
45
+ - wrap the `DualListSelectorList` in a `Droppable` component
46
+ - wrap the `DualListSelectorListItem` components in a `Draggable` component
47
+ - define an `onDrop` callback which reorders the sortable options.
48
+ - The `onDrop` function provides the starting location and destination location for a dragged item. It should return
49
+ true to enable the 'drop' animation in the new location and false to enable the 'drop' animation back to the item's
50
+ old position.
51
+ - define an `onDrag` callback which ensures that the drag event will not cross hairs with the `onOptionSelect` click
52
+ event set on the option. Note: the `ignoreNextOptionSelect` state value is used to prevent selection while dragging.
53
+
54
+ Note: Keyboard accessibility and screen reader accessibility for the `DragDropSort` component are still in development.
55
+
56
+ ```ts file="./DualListSelectorDraggable.tsx"
57
+ ```
@@ -0,0 +1,174 @@
1
+ import React from 'react';
2
+ import {
3
+ DualListSelector,
4
+ DualListSelectorPane,
5
+ DualListSelectorList,
6
+ DualListSelectorListItem,
7
+ DualListSelectorControlsWrapper,
8
+ DualListSelectorControl
9
+ } from '@patternfly/react-core';
10
+ import { DragDropSort } from '@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
+
17
+ export const ComposableDualListSelector: React.FunctionComponent = () => {
18
+ const [ignoreNextOptionSelect, setIgnoreNextOptionSelect] = React.useState(false);
19
+ const [availableOptions, setAvailableOptions] = React.useState([
20
+ { text: 'Apple', selected: false, isVisible: true },
21
+ { text: 'Banana', selected: false, isVisible: true },
22
+ { text: 'Pineapple', selected: false, isVisible: true }
23
+ ]);
24
+ const [chosenOptions, setChosenOptions] = React.useState([
25
+ { text: 'Orange', selected: false, isVisible: true },
26
+ { text: 'Grape', selected: false, isVisible: true },
27
+ { text: 'Peach', selected: false, isVisible: true },
28
+ { text: 'Strawberry', selected: false, isVisible: true }
29
+ ]);
30
+ const [dragging, setDragging] = React.useState(false);
31
+
32
+ const moveSelected = fromAvailable => {
33
+ const sourceOptions = fromAvailable ? availableOptions : chosenOptions;
34
+ const destinationOptions = fromAvailable ? chosenOptions : availableOptions;
35
+ for (let i = 0; i < sourceOptions.length; i++) {
36
+ const option = sourceOptions[i];
37
+ if (option.selected && option.isVisible) {
38
+ sourceOptions.splice(i, 1);
39
+ destinationOptions.push(option);
40
+ option.selected = false;
41
+ i--;
42
+ }
43
+ }
44
+ if (fromAvailable) {
45
+ setAvailableOptions([...sourceOptions]);
46
+ setChosenOptions([...destinationOptions]);
47
+ } else {
48
+ setChosenOptions([...sourceOptions]);
49
+ setAvailableOptions([...destinationOptions]);
50
+ }
51
+ };
52
+
53
+ const moveAll = fromAvailable => {
54
+ if (fromAvailable) {
55
+ setChosenOptions([...availableOptions.filter(x => x.isVisible), ...chosenOptions]);
56
+ setAvailableOptions([...availableOptions.filter(x => !x.isVisible)]);
57
+ } else {
58
+ setAvailableOptions([...chosenOptions.filter(x => x.isVisible), ...availableOptions]);
59
+ setChosenOptions([...chosenOptions.filter(x => !x.isVisible)]);
60
+ }
61
+ };
62
+
63
+ const onOptionSelect = (event, index, isChosen) => {
64
+ if (ignoreNextOptionSelect) {
65
+ setIgnoreNextOptionSelect(false);
66
+ return;
67
+ }
68
+ if (isChosen) {
69
+ const newChosen = [...chosenOptions];
70
+ newChosen[index].selected = !chosenOptions[index].selected;
71
+ setChosenOptions(newChosen);
72
+ } else {
73
+ const newAvailable = [...availableOptions];
74
+ newAvailable[index].selected = !availableOptions[index].selected;
75
+ setAvailableOptions(newAvailable);
76
+ }
77
+ };
78
+
79
+ const onDrop = (oldIndex, newIndex) => {
80
+ const newList = [...chosenOptions];
81
+ const [removed] = newList.splice(oldIndex, 1);
82
+ newList.splice(newIndex, 0, removed);
83
+ setChosenOptions(newList);
84
+ };
85
+
86
+ const sortableChosenOptions = chosenOptions.map((option, index) =>
87
+ option.isVisible
88
+ ? {
89
+ id: `composable-available-option-${index}`,
90
+ content: option.text,
91
+ props: {
92
+ key: index,
93
+ isSelected: option.selected,
94
+ onOptionSelect: e => onOptionSelect(e, index, true)
95
+ }
96
+ }
97
+ : null
98
+ );
99
+
100
+ return (
101
+ <DualListSelector>
102
+ <DualListSelectorPane
103
+ title="Available"
104
+ status={`${availableOptions.filter(x => x.selected && x.isVisible).length} of ${
105
+ availableOptions.filter(x => x.isVisible).length
106
+ } options selected`}
107
+ >
108
+ <DualListSelectorList>
109
+ {availableOptions.map((option, index) =>
110
+ option.isVisible ? (
111
+ <DualListSelectorListItem
112
+ key={index}
113
+ isSelected={option.selected}
114
+ id={`composable-available-option-${index}`}
115
+ onOptionSelect={e => onOptionSelect(e, index, false)}
116
+ >
117
+ {option.text}
118
+ </DualListSelectorListItem>
119
+ ) : null
120
+ )}
121
+ </DualListSelectorList>
122
+ </DualListSelectorPane>
123
+ <DualListSelectorControlsWrapper aria-label="Selector controls">
124
+ <DualListSelectorControl
125
+ isDisabled={!availableOptions.some(option => option.selected)}
126
+ onClick={() => moveSelected(true)}
127
+ aria-label="Add selected"
128
+ >
129
+ <AngleRightIcon />
130
+ </DualListSelectorControl>
131
+ <DualListSelectorControl
132
+ isDisabled={availableOptions.length === 0}
133
+ onClick={() => moveAll(true)}
134
+ aria-label="Add all"
135
+ >
136
+ <AngleDoubleRightIcon />
137
+ </DualListSelectorControl>
138
+ <DualListSelectorControl
139
+ isDisabled={chosenOptions.length === 0}
140
+ onClick={() => moveAll(false)}
141
+ aria-label="Remove all"
142
+ >
143
+ <AngleDoubleLeftIcon />
144
+ </DualListSelectorControl>
145
+ <DualListSelectorControl
146
+ onClick={() => moveSelected(false)}
147
+ isDisabled={!chosenOptions.some(option => option.selected)}
148
+ aria-label="Remove selected"
149
+ >
150
+ <AngleLeftIcon />
151
+ </DualListSelectorControl>
152
+ </DualListSelectorControlsWrapper>
153
+ <DualListSelectorPane
154
+ title="Chosen"
155
+ status={`${chosenOptions.filter(x => x.selected && x.isVisible).length} of ${
156
+ chosenOptions.filter(x => x.isVisible).length
157
+ } options selected`}
158
+ isChosen
159
+ >
160
+ <DualListSelectorList className={`pf-c-droppable ${dragging ? 'pf-m-dragging' : ''}`}>
161
+ <DragDropSort
162
+ items={sortableChosenOptions}
163
+ onDrop={(_, oldIndex, newIndex) => {
164
+ onDrop(oldIndex, newIndex);
165
+ setDragging(false);
166
+ }}
167
+ onDrag={() => setDragging(true)}
168
+ variant="DualListSelectorList"
169
+ />
170
+ </DualListSelectorList>
171
+ </DualListSelectorPane>
172
+ </DualListSelector>
173
+ );
174
+ };
@@ -0,0 +1 @@
1
+ export * from './DragDropSort';
@@ -0,0 +1 @@
1
+ export * from './DragDrop';
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './components';
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist/js",
5
+ "module": "commonjs",
6
+ "tsBuildInfoFile": "dist/cjs.tsbuildinfo"
7
+ }
8
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "extends": "../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "outDir": "./dist/esm",
6
+ "tsBuildInfoFile": "dist/esm.tsbuildinfo"
7
+ },
8
+ "include": [
9
+ "./src/*",
10
+ "./src/**/*"
11
+ ],
12
+ "references": [
13
+ {
14
+ "path": "../react-core"
15
+ },
16
+ {
17
+ "path": "../react-icons"
18
+ },
19
+ {
20
+ "path": "../react-styles"
21
+ }
22
+ ]
23
+ }