7
7
DualListSelectorControlsWrapper ,
8
8
DualListSelectorControl
9
9
} from '@patternfly/react-core' ;
10
- import { DragDropSort , DragDropSortDragEndEvent , DraggableObject } from '@patternfly/react-drag-drop' ;
10
+ import { DragDropSort , DraggableObject } from '@patternfly/react-drag-drop' ;
11
11
12
12
import AngleDoubleLeftIcon from '@patternfly/react-icons/dist/esm/icons/angle-double-left-icon' ;
13
13
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
16
16
17
17
export const ComposableDualListSelector : React . FunctionComponent = ( ) => {
18
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 }
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 } }
23
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 }
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 } }
29
30
] ) ;
30
31
31
32
const moveSelected = ( fromAvailable ) => {
32
33
const sourceOptions = fromAvailable ? availableOptions : chosenOptions ;
33
34
const destinationOptions = fromAvailable ? chosenOptions : availableOptions ;
34
35
for ( let i = 0 ; i < sourceOptions . length ; i ++ ) {
35
36
const option = sourceOptions [ i ] ;
36
- if ( option . selected && option . isVisible ) {
37
+ if ( option . props . isSelected ) {
37
38
sourceOptions . splice ( i , 1 ) ;
38
39
destinationOptions . push ( option ) ;
39
- option . selected = false ;
40
+ option . props . isSelected = false ;
40
41
i -- ;
41
42
}
42
43
}
@@ -51,11 +52,11 @@ export const ComposableDualListSelector: React.FunctionComponent = () => {
51
52
52
53
const moveAll = ( fromAvailable ) => {
53
54
if ( fromAvailable ) {
54
- setChosenOptions ( [ ...availableOptions . filter ( ( x ) => x . isVisible ) , ...chosenOptions ] ) ;
55
- setAvailableOptions ( [ ... availableOptions . filter ( ( x ) => ! x . isVisible ) ] ) ;
55
+ setChosenOptions ( [ ...availableOptions , ...chosenOptions ] ) ;
56
+ setAvailableOptions ( [ ] ) ;
56
57
} else {
57
- setAvailableOptions ( [ ...chosenOptions . filter ( ( x ) => x . isVisible ) , ...availableOptions ] ) ;
58
- setChosenOptions ( [ ... chosenOptions . filter ( ( x ) => ! x . isVisible ) ] ) ;
58
+ setAvailableOptions ( [ ...chosenOptions , ...availableOptions ] ) ;
59
+ setChosenOptions ( [ ] ) ;
59
60
}
60
61
} ;
61
62
@@ -66,62 +67,39 @@ export const ComposableDualListSelector: React.FunctionComponent = () => {
66
67
}
67
68
if ( isChosen ) {
68
69
const newChosen = [ ...chosenOptions ] ;
69
- newChosen [ index ] . selected = ! chosenOptions [ index ] . selected ;
70
+ newChosen [ index ] . props . isSelected = ! chosenOptions [ index ] . props . isSelected ;
70
71
setChosenOptions ( newChosen ) ;
71
72
} else {
72
73
const newAvailable = [ ...availableOptions ] ;
73
- newAvailable [ index ] . selected = ! availableOptions [ index ] . selected ;
74
+ newAvailable [ index ] . props . isSelected = ! availableOptions [ index ] . props . isSelected ;
74
75
setAvailableOptions ( newAvailable ) ;
75
76
}
76
77
} ;
77
78
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
-
99
79
return (
100
80
< DualListSelector >
101
81
< DualListSelectorPane
102
82
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
105
85
} options selected`}
106
86
>
107
87
< 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
+ ) ) }
120
98
</ DualListSelectorList >
121
99
</ DualListSelectorPane >
122
100
< DualListSelectorControlsWrapper aria-label = "Selector controls" >
123
101
< DualListSelectorControl
124
- isDisabled = { ! availableOptions . some ( ( option ) => option . selected ) }
102
+ isDisabled = { ! availableOptions . some ( ( option ) => option . props . isSelected ) }
125
103
onClick = { ( ) => moveSelected ( true ) }
126
104
aria-label = "Add selected"
127
105
>
@@ -143,20 +121,31 @@ export const ComposableDualListSelector: React.FunctionComponent = () => {
143
121
</ DualListSelectorControl >
144
122
< DualListSelectorControl
145
123
onClick = { ( ) => moveSelected ( false ) }
146
- isDisabled = { ! chosenOptions . some ( ( option ) => option . selected ) }
124
+ isDisabled = { ! chosenOptions . some ( ( option ) => option . props . isSelected ) }
147
125
aria-label = "Remove selected"
148
126
>
149
127
< AngleLeftIcon />
150
128
</ DualListSelectorControl >
151
129
</ DualListSelectorControlsWrapper >
152
130
< DualListSelectorPane
153
131
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` }
157
133
isChosen
158
134
>
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
+ >
160
149
< DualListSelectorList />
161
150
</ DragDropSort >
162
151
</ DualListSelectorPane >
0 commit comments