Skip to content

Commit d11d772

Browse files
committed
update duallistselector example, try exporting interface from separate file
1 parent 6394a55 commit d11d772

File tree

4 files changed

+59
-73
lines changed

4 files changed

+59
-73
lines changed

packages/react-drag-drop/src/components/DragDrop/DragDropSort.tsx

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,12 @@ import {
2121
import { Draggable } from './Draggable';
2222
import { DraggableDataListItem } from './DraggableDataListItem';
2323
import { DraggableDualListSelectorListItem } from './DraggableDualListSelectorListItem';
24+
import { DraggableObject } from './DragDropUtil';
2425
import styles from '@patternfly/react-styles/css/components/DragDrop/drag-drop';
2526

2627
export type DragDropSortDragEndEvent = DragEndEvent;
2728
export type DragDropSortDragStartEvent = DragStartEvent;
2829

29-
export interface DraggableObject {
30-
/** Unique id of the draggable object */
31-
id: string;
32-
/** Content rendered in the draggable object */
33-
content: React.ReactNode;
34-
/** Props spread to the rendered wrapper of the draggable object */
35-
props?: any;
36-
}
37-
3830
export interface DragDropSortProps extends DndContextProps {
3931
/** Custom defined content wrapper for draggable items. By default, draggable items are wrapped in a styled div.
4032
* Intended to be a 'DataList' or 'DualListSelectorList' without children. */
@@ -63,8 +55,6 @@ export const DragDropSort: React.FunctionComponent<DragDropSortProps> = ({
6355
...props
6456
}: DragDropSortProps) => {
6557
const [activeId, setActiveId] = React.useState<string>(null);
66-
// const [dragging, setDragging] = React.useState(false);
67-
6858
const itemIds = React.useMemo(() => (items ? Array.from(items, (item) => item.id as string) : []), [items]);
6959

7060
const getItemById = (id: string): DraggableObject => items.find((item) => item.id === id);
@@ -81,14 +71,12 @@ export const DragDropSort: React.FunctionComponent<DragDropSortProps> = ({
8171
const oldIndex = itemIds.indexOf(active.id as string);
8272
const newIndex = itemIds.indexOf(over.id as string);
8373
const newItems = arrayMove(items, oldIndex, newIndex);
84-
// setDragging(false);
8574
onDrop(event, newItems, oldIndex, newIndex);
86-
return newItems;
75+
setActiveId(null);
8776
};
8877

8978
const handleDragStart = (event: DragStartEvent) => {
9079
setActiveId(event.active.id as string);
91-
// setDragging(true);
9280
onDrag(event, itemIds.indexOf(event.active.id as string));
9381
};
9482

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export interface DraggableObject {
2+
/** Unique id of the draggable object */
3+
id: string;
4+
/** Content rendered in the draggable object */
5+
content: React.ReactNode;
6+
/** Props spread to the rendered wrapper of the draggable object */
7+
props?: any;
8+
}

packages/react-drag-drop/src/components/DragDrop/examples/DualListSelectorDraggable.tsx

Lines changed: 48 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
DualListSelectorControlsWrapper,
88
DualListSelectorControl
99
} from '@patternfly/react-core';
10-
import { DragDropSort, DragDropSortDragEndEvent, DraggableObject } from '@patternfly/react-drag-drop';
10+
import { DragDropSort, DraggableObject } from '@patternfly/react-drag-drop';
1111

1212
import AngleDoubleLeftIcon from '@patternfly/react-icons/dist/esm/icons/angle-double-left-icon';
1313
import AngleLeftIcon from '@patternfly/react-icons/dist/esm/icons/angle-left-icon';
@@ -16,27 +16,28 @@ import AngleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-right-i
1616

1717
export const ComposableDualListSelector: React.FunctionComponent = () => {
1818
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 }
19+
const [availableOptions, setAvailableOptions] = React.useState<DraggableObject[]>([
20+
{ id: 'Apple', content: 'Apple', props: { key: 'Apple', isSelected: false } },
21+
{ id: 'Banana', content: 'Banana', props: { key: 'Banana', isSelected: false } },
22+
{ id: 'Pineapple', content: 'Pineapple', props: { key: 'Pineapple', isSelected: false } }
2323
]);
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 }
24+
25+
const [chosenOptions, setChosenOptions] = React.useState<DraggableObject[]>([
26+
{ id: 'Orange', content: 'Orange', props: { key: 'Orange', isSelected: false } },
27+
{ id: 'Grape', content: 'Grape', props: { key: 'Grape', isSelected: false } },
28+
{ id: 'Peach', content: 'Peach', props: { key: 'Peach', isSelected: false } },
29+
{ id: 'Strawberry', content: 'Strawberry', props: { key: 'Strawberry', isSelected: false } }
2930
]);
3031

3132
const moveSelected = (fromAvailable) => {
3233
const sourceOptions = fromAvailable ? availableOptions : chosenOptions;
3334
const destinationOptions = fromAvailable ? chosenOptions : availableOptions;
3435
for (let i = 0; i < sourceOptions.length; i++) {
3536
const option = sourceOptions[i];
36-
if (option.selected && option.isVisible) {
37+
if (option.props.isSelected) {
3738
sourceOptions.splice(i, 1);
3839
destinationOptions.push(option);
39-
option.selected = false;
40+
option.props.isSelected = false;
4041
i--;
4142
}
4243
}
@@ -51,11 +52,11 @@ export const ComposableDualListSelector: React.FunctionComponent = () => {
5152

5253
const moveAll = (fromAvailable) => {
5354
if (fromAvailable) {
54-
setChosenOptions([...availableOptions.filter((x) => x.isVisible), ...chosenOptions]);
55-
setAvailableOptions([...availableOptions.filter((x) => !x.isVisible)]);
55+
setChosenOptions([...availableOptions, ...chosenOptions]);
56+
setAvailableOptions([]);
5657
} else {
57-
setAvailableOptions([...chosenOptions.filter((x) => x.isVisible), ...availableOptions]);
58-
setChosenOptions([...chosenOptions.filter((x) => !x.isVisible)]);
58+
setAvailableOptions([...chosenOptions, ...availableOptions]);
59+
setChosenOptions([]);
5960
}
6061
};
6162

@@ -66,62 +67,39 @@ export const ComposableDualListSelector: React.FunctionComponent = () => {
6667
}
6768
if (isChosen) {
6869
const newChosen = [...chosenOptions];
69-
newChosen[index].selected = !chosenOptions[index].selected;
70+
newChosen[index].props.isSelected = !chosenOptions[index].props.isSelected;
7071
setChosenOptions(newChosen);
7172
} else {
7273
const newAvailable = [...availableOptions];
73-
newAvailable[index].selected = !availableOptions[index].selected;
74+
newAvailable[index].props.isSelected = !availableOptions[index].props.isSelected;
7475
setAvailableOptions(newAvailable);
7576
}
7677
};
7778

78-
const onDrop = (event: DragDropSortDragEndEvent, newItems: DraggableObject[], oldIndex: number, newIndex: number) => {
79-
const newList = [...chosenOptions];
80-
const [removed] = newList.splice(oldIndex, 1);
81-
newList.splice(newIndex, 0, removed);
82-
setChosenOptions(newList);
83-
};
84-
85-
const sortableChosenOptions = chosenOptions.map((option, index) =>
86-
option.isVisible
87-
? {
88-
id: `composable-available-option-${option.text}`,
89-
content: option.text,
90-
props: {
91-
key: index,
92-
isSelected: option.selected,
93-
onOptionSelect: (e) => onOptionSelect(e, index, true)
94-
}
95-
}
96-
: null
97-
);
98-
9979
return (
10080
<DualListSelector>
10181
<DualListSelectorPane
10282
title="Available"
103-
status={`${availableOptions.filter((x) => x.selected && x.isVisible).length} of ${
104-
availableOptions.filter((x) => x.isVisible).length
83+
status={`${availableOptions.filter((x) => x.props.isSelected).length} of ${
84+
availableOptions.length
10585
} options selected`}
10686
>
10787
<DualListSelectorList>
108-
{availableOptions.map((option, index) =>
109-
option.isVisible ? (
110-
<DualListSelectorListItem
111-
key={index}
112-
isSelected={option.selected}
113-
id={`composable-available-option-${option.text}`}
114-
onOptionSelect={(e) => onOptionSelect(e, index, false)}
115-
>
116-
{option.text}
117-
</DualListSelectorListItem>
118-
) : null
119-
)}
88+
{availableOptions.map((option, index) => (
89+
<DualListSelectorListItem
90+
key={index}
91+
isSelected={option.props.isSelected}
92+
id={`composable-available-option-${option.content}`}
93+
onOptionSelect={(e) => onOptionSelect(e, index, false)}
94+
>
95+
{option.content}
96+
</DualListSelectorListItem>
97+
))}
12098
</DualListSelectorList>
12199
</DualListSelectorPane>
122100
<DualListSelectorControlsWrapper aria-label="Selector controls">
123101
<DualListSelectorControl
124-
isDisabled={!availableOptions.some((option) => option.selected)}
102+
isDisabled={!availableOptions.some((option) => option.props.isSelected)}
125103
onClick={() => moveSelected(true)}
126104
aria-label="Add selected"
127105
>
@@ -143,20 +121,31 @@ export const ComposableDualListSelector: React.FunctionComponent = () => {
143121
</DualListSelectorControl>
144122
<DualListSelectorControl
145123
onClick={() => moveSelected(false)}
146-
isDisabled={!chosenOptions.some((option) => option.selected)}
124+
isDisabled={!chosenOptions.some((option) => option.props.isSelected)}
147125
aria-label="Remove selected"
148126
>
149127
<AngleLeftIcon />
150128
</DualListSelectorControl>
151129
</DualListSelectorControlsWrapper>
152130
<DualListSelectorPane
153131
title="Chosen"
154-
status={`${chosenOptions.filter((x) => x.selected && x.isVisible).length} of ${
155-
chosenOptions.filter((x) => x.isVisible).length
156-
} options selected`}
132+
status={`${chosenOptions.filter((x) => x.props.isSelected).length} of ${chosenOptions.length} options selected`}
157133
isChosen
158134
>
159-
<DragDropSort items={sortableChosenOptions} onDrop={onDrop} variant="DualListSelectorList">
135+
<DragDropSort
136+
items={chosenOptions.map((option, index) => ({
137+
...option,
138+
props: {
139+
key: option.props.key,
140+
isSelected: option.props.isSelected,
141+
onOptionSelect: (e) => onOptionSelect(e, index, true)
142+
}
143+
}))}
144+
onDrop={(_, newItems) => {
145+
setChosenOptions(newItems);
146+
}}
147+
variant="DualListSelectorList"
148+
>
160149
<DualListSelectorList />
161150
</DragDropSort>
162151
</DualListSelectorPane>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './DragDropSort';
2+
export * from './DragDropUtil';

0 commit comments

Comments
 (0)