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
1212import AngleDoubleLeftIcon from '@patternfly/react-icons/dist/esm/icons/angle-double-left-icon' ;
1313import 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
1717export 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 >
0 commit comments