1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add showWithColumn property

Signed-off-by: Pavel Ashevskii <pashevskii@mirantis.com>
This commit is contained in:
Pavel Ashevskii 2020-12-02 18:09:06 +04:00
parent 43dd1bbd81
commit 4b8ed095a7
4 changed files with 40 additions and 24 deletions

View File

@ -96,7 +96,7 @@ export class Pods extends React.Component<Props> {
renderHeaderTitle="Pods" renderHeaderTitle="Pods"
renderTableHeader={[ renderTableHeader={[
{ title: "Name", className: "name", sortBy: sortBy.name }, { title: "Name", className: "name", sortBy: sortBy.name },
{ className: "warning" }, { className: "warning", showWithColumn: "name" },
{ title: "Namespace", className: "namespace", sortBy: sortBy.namespace }, { title: "Namespace", className: "namespace", sortBy: sortBy.namespace },
{ title: "Containers", className: "containers", sortBy: sortBy.containers }, { title: "Containers", className: "containers", sortBy: sortBy.containers },
{ title: "Restarts", className: "restarts", sortBy: sortBy.restarts }, { title: "Restarts", className: "restarts", sortBy: sortBy.restarts },

View File

@ -1,4 +1,5 @@
import "./item-list-layout.scss"; import "./item-list-layout.scss";
import "./table-menu.scss";
import groupBy from "lodash/groupBy"; import groupBy from "lodash/groupBy";
import React, { ReactNode } from "react"; import React, { ReactNode } from "react";
@ -119,7 +120,8 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
} }
async componentDidMount() { async componentDidMount() {
const { store, dependentStores, isClusterScoped, isConfigurable, tableId, renderTableHeader } = this.props; const { store, dependentStores, isClusterScoped, tableId } = this.props;
if (this.canBeConfigured) this.hiddenColumnNames = new Set(userStore.preferences?.hiddenTableColumns?.[tableId]); if (this.canBeConfigured) this.hiddenColumnNames = new Set(userStore.preferences?.hiddenTableColumns?.[tableId]);
const stores = [store, ...dependentStores]; const stores = [store, ...dependentStores];
@ -139,11 +141,9 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
componentWillUnmount() { componentWillUnmount() {
this.isUnmounting = true; this.isUnmounting = true;
const { store, isSelectable, isConfigurable, tableId } = this.props; const { store, isSelectable } = this.props;
if (isSelectable) store.resetSelection(); if (isSelectable) store.resetSelection();
if (this.canBeConfigured) {
userStore.preferences.hiddenTableColumns[tableId] = Array.from(this.hiddenColumnNames);
}
} }
private filterCallbacks: { [type: string]: ItemsFilter } = { private filterCallbacks: { [type: string]: ItemsFilter } = {
@ -233,15 +233,21 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
} else { } else {
this.hiddenColumnNames.add(columnName); this.hiddenColumnNames.add(columnName);
} }
if (this.canBeConfigured) {
userStore.preferences.hiddenTableColumns[this.props.tableId] = Array.from(this.hiddenColumnNames);
}
} }
showColumn(index: number): boolean { columnIsVisible(index: number): boolean {
const {isConfigurable, tableId , renderTableHeader} = this.props; const {renderTableHeader} = this.props;
if (!this.canBeConfigured) return true; if (!this.canBeConfigured) return true;
return !this.hiddenColumnNames.has(renderTableHeader[index].className);
return !this.hiddenColumnNames.has(renderTableHeader[index].showWithColumn ?? renderTableHeader[index].className);
} }
@computed get canBeConfigured(): boolean { get canBeConfigured(): boolean {
const { isConfigurable, tableId, renderTableHeader } = this.props; const { isConfigurable, tableId, renderTableHeader } = this.props;
if (!isConfigurable || !tableId) { if (!isConfigurable || !tableId) {
@ -250,6 +256,7 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
if (!renderTableHeader?.every(({ className }) => className)) { if (!renderTableHeader?.every(({ className }) => className)) {
logger.warning("[ItemObjectList]: cannot configure an object list without all headers being identifiable"); logger.warning("[ItemObjectList]: cannot configure an object list without all headers being identifiable");
return false; return false;
} }
@ -298,7 +305,8 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
cellProps.className = cssNames(cellProps.className, headCell.className); cellProps.className = cssNames(cellProps.className, headCell.className);
} }
} }
return this.showColumn(index) ? <TableCell key={index} {...cellProps} /> : null;
return this.columnIsVisible(index) ? <TableCell key={index} {...cellProps} /> : null;
}) })
} }
{renderItemMenu && ( {renderItemMenu && (
@ -435,9 +443,8 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
renderList() { renderList() {
const { const {
isSelectable, tableProps = {}, renderTableHeader, isSelectable, tableProps = {}, renderTableHeader, renderItemMenu,
store, hasDetailsView, addRemoveButtons = {}, virtual, sortingCallbacks, detailsItem, store, hasDetailsView, addRemoveButtons = {}, virtual, sortingCallbacks, detailsItem
isConfigurable, tableId
} = this.props; } = this.props;
const { isReady, removeItemsDialog, items } = this; const { isReady, removeItemsDialog, items } = this;
const { selectedItems } = store; const { selectedItems } = store;
@ -471,8 +478,8 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
onClick={prevDefault(() => store.toggleSelectionAll(items))} onClick={prevDefault(() => store.toggleSelectionAll(items))}
/> />
)} )}
{renderTableHeader.map((cellProps, index) => this.showColumn(index) ? <TableCell key={index} {...cellProps} /> : null)} {renderTableHeader.map((cellProps, index) => this.columnIsVisible(index) ? <TableCell key={index} {...cellProps} /> : null)}
{ { renderItemMenu &&
<TableCell className="menu" > <TableCell className="menu" >
{this.canBeConfigured && this.renderColumnMenu()} {this.canBeConfigured && this.renderColumnMenu()}
</TableCell> </TableCell>
@ -496,19 +503,23 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
renderColumnMenu() { renderColumnMenu() {
const { renderTableHeader} = this.props; const { renderTableHeader} = this.props;
return(
return (
<MenuActions <MenuActions
toolbar = {false} toolbar = {false}
autoCloseOnSelect = {false} autoCloseOnSelect = {false}
className={cssNames("KubeObjectMenu")} className={cssNames("KubeObjectMenu")}
> >
{renderTableHeader.map((cellProps, index) => ( {renderTableHeader.map((cellProps, index) => (
<MenuItem key={index}> !cellProps.showWithColumn &&
<Checkbox label = {cellProps.title ? cellProps.title : `<${cellProps.className}>`} <MenuItem key={index} className="input">
value ={!this.hiddenColumnNames.has(cellProps.className)} <Checkbox label = {cellProps.title ?? `<${cellProps.className}>`}
onChange = {v =>this.updateColumnFilter(v, cellProps.className)} /> className = "MenuCheckbox"
</MenuItem>)) value ={!this.hiddenColumnNames.has(cellProps.className)}
} onChange = {(v) => this.updateColumnFilter(v, cellProps.className)}
/>
</MenuItem>
))}
</MenuActions> </MenuActions>
); );
} }

View File

@ -0,0 +1,4 @@
.MenuCheckbox {
width: 100%;
height: 100%;
}

View File

@ -15,6 +15,7 @@ export interface TableCellProps extends React.DOMAttributes<HTMLDivElement> {
isChecked?: boolean; // mark checkbox as checked or not isChecked?: boolean; // mark checkbox as checked or not
renderBoolean?: boolean; // show "true" or "false" for all of the children elements are "typeof boolean" renderBoolean?: boolean; // show "true" or "false" for all of the children elements are "typeof boolean"
sortBy?: TableSortBy; // column name, must be same as key in sortable object <Table sortable={}/> sortBy?: TableSortBy; // column name, must be same as key in sortable object <Table sortable={}/>
showWithColumn?: string // className of another column, if it is not empty the current column is not shown in the filter menu, visibility of this one is the same as a specified column, applicable to headers only
_sorting?: Partial<TableSortParams>; // <Table> sorting state, don't use this prop outside (!) _sorting?: Partial<TableSortParams>; // <Table> sorting state, don't use this prop outside (!)
_sort?(sortBy: TableSortBy): void; // <Table> sort function, don't use this prop outside (!) _sort?(sortBy: TableSortBy): void; // <Table> sort function, don't use this prop outside (!)
_nowrap?: boolean; // indicator, might come from parent <TableHead>, don't use this prop outside (!) _nowrap?: boolean; // indicator, might come from parent <TableHead>, don't use this prop outside (!)
@ -63,7 +64,7 @@ export class TableCell extends React.Component<TableCellProps> {
} }
render() { render() {
const { className, checkbox, isChecked, sortBy, _sort, _sorting, _nowrap, children, title, renderBoolean: displayBoolean, ...cellProps } = this.props; const { className, checkbox, isChecked, sortBy, _sort, _sorting, _nowrap, children, title, renderBoolean: displayBoolean, showWithColumn, ...cellProps } = this.props;
const classNames = cssNames("TableCell", className, { const classNames = cssNames("TableCell", className, {
checkbox, checkbox,
nowrap: _nowrap, nowrap: _nowrap,