11import type { FunctionComponent } from 'react' ;
2- import { useState , useEffect } from 'react' ;
2+ import { useState } from 'react' ;
33import {
44 DataList ,
55 DataListItemRow ,
@@ -8,17 +8,20 @@ import {
88 DataListItemCells ,
99 Button ,
1010 ButtonVariant ,
11+ ActionList ,
12+ ActionListItem ,
13+ ActionListGroup ,
1114} from '@patternfly/react-core' ;
1215import { DragDropSort , Droppable } from '@patternfly/react-drag-drop' ;
1316import BulkSelect , { BulkSelectValue } from '../BulkSelect' ;
1417
15- export interface Column {
18+ export interface ListManagerItem {
1619 /** Internal identifier of a column by which table displayed columns are filtered. */
1720 key : string ;
1821 /** The actual display name of the column possibly with a tooltip or icon. */
1922 title : React . ReactNode ;
2023 /** If user changes checkboxes, the component will send back column array with this property altered. */
21- isShown ?: boolean ;
24+ isSelected ?: boolean ;
2225 /** Set to false if the column should be hidden initially */
2326 isShownByDefault : boolean ;
2427 /** The checkbox will be disabled, this is applicable to columns which should not be toggleable by user */
@@ -27,17 +30,17 @@ export interface Column {
2730
2831export interface ListManagerProps {
2932 /** Current column state */
30- columns : Column [ ] ;
33+ columns : ListManagerItem [ ] ;
3134 /** Custom OUIA ID */
3235 ouiaId ?: string | number ;
3336 /** Callback when a column is selected or deselected */
34- onSelect ?: ( column : Column ) => void ;
37+ onSelect ?: ( column : ListManagerItem ) => void ;
3538 /** Callback when all columns are selected or deselected */
36- onSelectAll ?: ( columns : Column [ ] ) => void ;
39+ onSelectAll ?: ( columns : ListManagerItem [ ] ) => void ;
3740 /** Callback when the column order changes */
38- onOrderChange ?: ( columns : Column [ ] ) => void ;
41+ onOrderChange ?: ( columns : ListManagerItem [ ] ) => void ;
3942 /** Callback to save the column state */
40- onSave ?: ( columns : Column [ ] ) => void ;
43+ onSave ?: ( columns : ListManagerItem [ ] ) => void ;
4144 /** Callback to close the modal */
4245 onCancel ?: ( ) => void ;
4346}
@@ -52,57 +55,62 @@ const ListManager: FunctionComponent<ListManagerProps> = (
5255 onCancel } : ListManagerProps ) => {
5356
5457 const [ currentColumns , setCurrentColumns ] = useState (
55- ( ) => columns . map ( column => ( { ...column , isShown : column . isShown ?? column . isShownByDefault , id : column . key } ) )
58+ ( ) => columns . map ( column => ( { ...column , isSelected : column . isSelected ?? column . isShownByDefault , id : column . key } ) )
5659 ) ;
5760
58- useEffect ( ( ) => {
59- setCurrentColumns ( columns . map ( column => ( { ...column , isShown : column . isShown ?? column . isShownByDefault , id : column . key } ) ) ) ;
60- } , [ columns ] ) ;
6161
62- const handleChange = index => {
62+ const handleChange = ( columnKey : string ) => {
6363 const newColumns = [ ...currentColumns ] ;
64+ const index = newColumns . findIndex ( col => col . key === columnKey ) ;
65+ if ( index === - 1 ) { return ; }
66+
6467 const changedColumn = { ...newColumns [ index ] } ;
65-
66- changedColumn . isShown = ! changedColumn . isShown ;
68+ changedColumn . isSelected = ! changedColumn . isSelected ;
6769 newColumns [ index ] = changedColumn ;
6870
6971 setCurrentColumns ( newColumns ) ;
7072 onSelect ?.( changedColumn ) ;
7173 } ;
7274
73- const onDrag = ( _event , newOrder ) => {
74- const newColumns = newOrder . map ( item => currentColumns . find ( c => c . key === item . id ) ) ;
75+ const onDrag = ( _event : unknown , newOrder : any [ ] ) => {
76+ const newColumns = newOrder . map ( ( item : any ) => {
77+ const found = currentColumns . find ( c => c . key === item . id ) ;
78+ if ( ! found ) {
79+ throw new Error ( `Column with key ${ item . id } not found` ) ;
80+ }
81+ return found ;
82+ } ) ;
7583 setCurrentColumns ( newColumns ) ;
7684 onOrderChange ?.( newColumns ) ;
7785 } ;
7886
7987 const handleSave = ( ) => {
8088 onSave ?.( currentColumns ) ;
81- }
89+ } ;
8290
8391 const handleBulkSelect = ( value : BulkSelectValue ) => {
8492 const allSelected = value === 'all' || value === 'page' ;
8593 handleSelectAll ( allSelected ) ;
8694 } ;
8795
8896 const handleSelectAll = ( select = true ) => {
89- const newColumns = currentColumns . map ( c => ( { ...c , isShown : c . isUntoggleable ? c . isShown : select } ) ) ;
97+ const newColumns = currentColumns . map ( c => ( { ...c , isSelected : c . isUntoggleable ? c . isSelected : select } ) ) ;
9098 setCurrentColumns ( newColumns ) ;
9199 onSelectAll ?.( newColumns ) ;
92- }
100+ } ;
93101
94102 return (
95103 < >
96104 < div style = { { paddingBottom : '1rem' } } >
97105 < BulkSelect
98106 canSelectAll
99107 isDataPaginated = { false }
100- selectedCount = { currentColumns . filter ( ( { isShown } ) => isShown ) . length }
108+ selectedCount = { currentColumns . filter ( ( { isSelected } ) => isSelected ) . length }
101109 totalCount = { currentColumns . length }
102110 onSelect = { handleBulkSelect }
103- pageSelected = { currentColumns . every ( ( item ) => item . isShown ) }
111+ pageSelected = { currentColumns . every ( ( item ) => item . isSelected ) }
104112 pagePartiallySelected = {
105- currentColumns . some ( ( item ) => item . isShown ) && ! currentColumns . every ( ( item ) => item . isShown )
113+ currentColumns . some ( ( item ) => item . isSelected ) && ! currentColumns . every ( ( item ) => item . isSelected )
106114 }
107115 />
108116 </ div >
@@ -112,8 +120,8 @@ const ListManager: FunctionComponent<ListManagerProps> = (
112120 < DataListItemRow >
113121 < DataListCheck
114122 data-testid = { `column-check-${ column . key } ` }
115- isChecked = { column . isShown }
116- onChange = { ( ) => handleChange ( index ) }
123+ isChecked = { column . isSelected }
124+ onChange = { ( ) => handleChange ( column . key ) }
117125 isDisabled = { column . isUntoggleable }
118126 aria-labelledby = { `${ ouiaId } -column-${ index } -label` }
119127 ouiaId = { `${ ouiaId } -column-${ index } -checkbox` }
@@ -140,14 +148,21 @@ const ListManager: FunctionComponent<ListManagerProps> = (
140148 ) }
141149 wrapper = { < DataList aria-label = "Selected columns" isCompact data-ouia-component-id = { `${ ouiaId } -column-list` } /> }
142150 />
143- </ DragDropSort > < div style = { { display : 'flex' , justifyContent : 'normal' , paddingTop : '1rem' } } >
144- < Button key = "save" variant = { ButtonVariant . primary } onClick = { handleSave } ouiaId = { `${ ouiaId } -save-button` } >
145- Save
146- </ Button >
147- < Button key = "cancel" variant = { ButtonVariant . link } onClick = { onCancel } ouiaId = { `${ ouiaId } -cancel-button` } >
148- Cancel
149- </ Button >
150- </ div >
151+ </ DragDropSort >
152+ < ActionList style = { { paddingTop : '1rem' } } >
153+ < ActionListGroup >
154+ < ActionListItem >
155+ < Button key = "save" variant = { ButtonVariant . primary } onClick = { handleSave } ouiaId = { `${ ouiaId } -save-button` } >
156+ Save
157+ </ Button >
158+ </ ActionListItem >
159+ < ActionListItem >
160+ < Button key = "cancel" variant = { ButtonVariant . link } onClick = { onCancel } ouiaId = { `${ ouiaId } -cancel-button` } >
161+ Cancel
162+ </ Button >
163+ </ ActionListItem >
164+ </ ActionListGroup >
165+ </ ActionList >
151166 </ >
152167 ) ;
153168}
0 commit comments