-
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
192 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
139 changes: 139 additions & 0 deletions
139
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,139 @@ | ||
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 './column-selector.scss'; | ||
|
||
const ColumnSelector = props => { | ||
const { | ||
data: { url, controller, selected, defined }, | ||
} = props; | ||
|
||
const [isModalOpen, setModalOpen] = useState(false); | ||
const [selectedColumns, setSelectedColumns] = useState(selected); | ||
|
||
const onClose = event => { | ||
setModalOpen(!isModalOpen); | ||
}; | ||
|
||
const filterItems = (item, checkedItem) => { | ||
if (item.id === checkedItem.id) { | ||
return true; | ||
} | ||
|
||
if(item.children) { | ||
return ( | ||
(item.children = item.children | ||
.map(opt => Object.assign({}, opt)) | ||
.filter(column => filterItems(column, checkedItem))).length > 0 | ||
); | ||
} | ||
}; | ||
|
||
const flattenTree = tree => { | ||
var result = []; | ||
tree.forEach((item) => { | ||
result.push(item); | ||
if (item.children) { | ||
result = result.concat(flattenTree(item.children)); | ||
} | ||
}); | ||
return result; | ||
}; | ||
|
||
const onCheck = (evt, treeViewItem) => { | ||
const checked = evt.target.checked; | ||
const checkedItemTree = defined.map(column => Object.assign({}, column)) | ||
.filter(item => filterItems(item, treeViewItem)); | ||
const flatCheckedItems = flattenTree(checkedItemTree); | ||
}; | ||
|
||
const isChecked = dataItem => { | ||
dataItem.checked = selected.some((item) => item === dataItem.id); | ||
return dataItem.checked; | ||
}; | ||
const areAllDescendantsChecked = dataItem => | ||
dataItem.children ? dataItem.children.every((column) => isChecked(column)) : null; | ||
const areSomeDescendantsChecked = dataItem => | ||
dataItem.children ? dataItem.children.some((column) => isChecked(column)) : null; | ||
|
||
const mapTree = item => { | ||
const hasCheck = areAllDescendantsChecked(item); | ||
|
||
if (hasCheck) { | ||
item.checked = false; | ||
} else { | ||
const hasPartialCheck = areSomeDescendantsChecked(item); | ||
if (hasPartialCheck) { | ||
item.checked = null; | ||
} | ||
} | ||
|
||
if (item.children) { | ||
return { | ||
...item, | ||
children: item.children.map((column) => mapTree(column)) | ||
}; | ||
} | ||
|
||
return item; | ||
}; | ||
|
||
const mapped = defined.map((category) => mapTree(category)); | ||
|
||
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"> | ||
{__('Save')} | ||
</Button>, | ||
<Button key="cancel" variant="secondary" onClick={() => onClose()}> | ||
{__('Cancel')} | ||
</Button> | ||
]} | ||
> | ||
<TreeView | ||
data={mapped} | ||
onCheck={onCheck} | ||
hasChecks | ||
> | ||
</TreeView> | ||
</Modal> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
ColumnSelector.propTypes = { | ||
data: PropTypes.shape({ | ||
controller: PropTypes.string, | ||
url: PropTypes.string, | ||
selected: PropTypes.arrayOf(PropTypes.string), | ||
defined: PropTypes.arrayOf(PropTypes.object), | ||
}), | ||
}; | ||
|
||
ColumnSelector.defaultProps = { | ||
data: { | ||
controller: null, | ||
url: null, | ||
selected: [], | ||
defined: [], | ||
}, | ||
}; | ||
|
||
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