-
Notifications
You must be signed in to change notification settings - Fork 993
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #35287 - Create column selector on host index page
- Loading branch information
Showing
7 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
webpack/assets/javascripts/react_app/components/ColumnSelector/ColumnSelector.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
import React, { useState } from 'react'; | ||
import { PropTypes } from 'prop-types'; | ||
import { Button, Modal, ModalVariant, TreeView } from '@patternfly/react-core'; | ||
import { translate as __ } from '../../common/I18n'; | ||
import API from '../../API'; | ||
import { changeQuery } from '../../common/urlHelpers'; | ||
import './column-selector.scss'; | ||
|
||
const ColumnSelector = props => { | ||
const { | ||
data: { url, columns }, | ||
} = props; | ||
|
||
const [isModalOpen, setModalOpen] = useState(false); | ||
const [selectedColumns, setSelectedColumns] = useState(columns); | ||
|
||
const getColumnKeys = event => { | ||
return columns | ||
.find(category => category.key === 'general') | ||
.children.map(col => { | ||
if (col.checkProps.checked) { | ||
return col.key; | ||
} | ||
return null; | ||
}) | ||
.filter(item => item); | ||
}; | ||
|
||
const updateTablePreference = event => { | ||
API.put(url, { columns: getColumnKeys() }); | ||
changeQuery({}); | ||
}; | ||
|
||
const onClose = event => { | ||
setModalOpen(!isModalOpen); | ||
}; | ||
|
||
const filterItems = (item, checkedItem) => { | ||
if (item.key === checkedItem.key) { | ||
return true; | ||
} | ||
|
||
if (item.children) { | ||
item.children = item.children | ||
.map(opt => Object.assign({}, opt)) | ||
.filter(column => filterItems(column, checkedItem)); | ||
return item.children; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
const flattenTree = tree => { | ||
let result = []; | ||
tree.forEach(item => { | ||
result.push(item); | ||
if (item.children) { | ||
result = result.concat(flattenTree(item.children)); | ||
} | ||
}); | ||
return result; | ||
}; | ||
|
||
const unCheck = treeViewItem => { | ||
treeViewItem.checkProps.checked = false; | ||
if (treeViewItem.children) { | ||
treeViewItem.children.forEach(item => { | ||
item.checkProps.checked = false; | ||
}); | ||
} | ||
}; | ||
|
||
const onCheck = (evt, treeViewItem) => { | ||
const { checked } = evt.target; | ||
const checkedItemTree = columns | ||
.map(column => Object.assign({}, column)) | ||
.filter(item => filterItems(item, treeViewItem)); | ||
const flatCheckedItems = flattenTree(checkedItemTree); | ||
|
||
if (checked) { | ||
setSelectedColumns( | ||
selectedColumns.concat( | ||
flatCheckedItems.filter( | ||
item => !selectedColumns.some(i => i.key === item.key) | ||
) | ||
) | ||
); | ||
} else { | ||
unCheck(treeViewItem); | ||
setSelectedColumns( | ||
selectedColumns.filter( | ||
item => !flatCheckedItems.some(i => i.key === item.key) | ||
) | ||
); | ||
} | ||
}; | ||
|
||
const isChecked = dataItem => | ||
selectedColumns.some(item => item.key === dataItem.key); | ||
const areAllDescendantsChecked = dataItem => | ||
dataItem.children | ||
? dataItem.children.every(child => areAllDescendantsChecked(child)) | ||
: isChecked(dataItem); | ||
const areSomeDescendantsChecked = dataItem => | ||
dataItem.children | ||
? dataItem.children.some(child => areSomeDescendantsChecked(child)) | ||
: isChecked(dataItem); | ||
|
||
const mapTree = item => { | ||
const hasCheck = areAllDescendantsChecked(item); | ||
|
||
if (hasCheck) { | ||
item.checkProps.checked = true; | ||
} else { | ||
const hasPartialCheck = areSomeDescendantsChecked(item); | ||
if (hasPartialCheck) { | ||
item.checkProps.checked = null; | ||
} | ||
} | ||
|
||
if (item.children) { | ||
return { | ||
...item, | ||
children: item.children.map(child => mapTree(child)), | ||
}; | ||
} | ||
|
||
return item; | ||
}; | ||
|
||
return ( | ||
<div className="pf-c-select-input"> | ||
<div className="column-selector pf-c-input-group" id="column-selector"> | ||
<Button | ||
id="btn-filter" | ||
variant="tertiary" | ||
className="pull-left" | ||
onClick={() => onClose()} | ||
> | ||
{__('Filter columns')} | ||
</Button> | ||
<Modal | ||
variant={ModalVariant.small} | ||
title={__('Filter columns')} | ||
isOpen={isModalOpen} | ||
onClose={onClose} | ||
actions={[ | ||
<Button | ||
key="save" | ||
variant="primary" | ||
onClick={() => updateTablePreference()} | ||
> | ||
{__('Save')} | ||
</Button>, | ||
<Button key="cancel" variant="secondary" onClick={() => onClose()}> | ||
{__('Cancel')} | ||
</Button>, | ||
]} | ||
> | ||
<TreeView | ||
data={columns.map(category => mapTree(category))} | ||
onCheck={onCheck} | ||
hasChecks | ||
/> | ||
</Modal> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
ColumnSelector.propTypes = { | ||
data: PropTypes.shape({ | ||
url: PropTypes.string, | ||
columns: PropTypes.arrayOf(PropTypes.object), | ||
}), | ||
}; | ||
|
||
ColumnSelector.defaultProps = { | ||
data: { | ||
url: '', | ||
columns: [], | ||
}, | ||
}; | ||
|
||
export default ColumnSelector; |
13 changes: 13 additions & 0 deletions
13
webpack/assets/javascripts/react_app/components/ColumnSelector/column-selector.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.column-selector { | ||
width: 100%; | ||
|
||
label { | ||
font-size: 16px; | ||
height: 36px; | ||
border-bottom-color: var(--pf-global--BorderColor--200); | ||
border-top-color: var(--pf-global--BorderColor--300); | ||
border-left-color: var(--pf-global--BorderColor--300); | ||
border-left-width: 0.7px; | ||
border-top-width: 0; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
webpack/assets/javascripts/react_app/components/ColumnSelector/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import ColumnSelector from './ColumnSelector'; | ||
|
||
export default ColumnSelector; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters