Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table component injection tokens #7754

Merged
merged 20 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@
"@k8slens/routing": "^1.0.0-alpha.5",
"@k8slens/run-many": "^1.0.0-alpha.1",
"@k8slens/startable-stoppable": "^1.0.0-alpha.1",
"@k8slens/table-tokens": "^6.5.0-alpha.7",
"@k8slens/test-utils": "^1.0.0-alpha.3",
"@k8slens/tooltip": "^1.0.0-alpha.5",
"@k8slens/utilities": "^1.0.0-alpha.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import type { ToggleTableColumnVisibility } from "../../../features/user-prefere
import toggleTableColumnVisibilityInjectable from "../../../features/user-preferences/common/toggle-table-column-visibility.injectable";
import type { IsTableColumnHidden } from "../../../features/user-preferences/common/is-table-column-hidden.injectable";
import isTableColumnHiddenInjectable from "../../../features/user-preferences/common/is-table-column-hidden.injectable";
import { TableComponent, TableDataContext, TableDataContextValue, tableComponentInjectionToken } from "@k8slens/table-tokens";

export interface ItemListLayoutContentProps<Item extends ItemObject, PreLoadStores extends boolean> {
getFilters: () => Filter[];
Expand Down Expand Up @@ -79,13 +80,17 @@ interface Dependencies {
openConfirmDialog: OpenConfirmDialog;
toggleTableColumnVisibility: ToggleTableColumnVisibility;
isTableColumnHidden: IsTableColumnHidden;
table?: TableComponent;
}

@observer
class NonInjectedItemListLayoutContent<
Item extends ItemObject,
PreLoadStores extends boolean,
> extends React.Component<ItemListLayoutContentProps<Item, PreLoadStores> & Dependencies> {
static contextType = TableDataContext;
declare context: TableDataContextValue;

constructor(props: ItemListLayoutContentProps<Item, PreLoadStores> & Dependencies) {
super(props);
makeObservable(this);
Expand Down Expand Up @@ -299,12 +304,17 @@ class NonInjectedItemListLayoutContent<
const {
store, hasDetailsView, addRemoveButtons = {}, virtual, sortingCallbacks,
detailsItem, className, tableProps = {}, tableId, getItems, activeTheme,
table,
} = this.props;
const selectedItemId = detailsItem && detailsItem.getId();
const classNames = cssNames(className, "box", "grow", activeTheme.get().type);
const items = getItems();
const selectedItems = store.pickOnlySelected(items);

if (table) {
return <table.Component tableId={tableId} columns={this.context.columns} {...this.props} />
aleksfront marked this conversation as resolved.
Show resolved Hide resolved
}

return (
<div className="items box grow flex column">
<Table
Expand Down Expand Up @@ -385,5 +395,6 @@ export const ItemListLayoutContent = withInjectables<Dependencies, ItemListLayou
openConfirmDialog: di.inject(openConfirmDialogInjectable),
toggleTableColumnVisibility: di.inject(toggleTableColumnVisibilityInjectable),
isTableColumnHidden: di.inject(isTableColumnHiddenInjectable),
table: di.inject(tableComponentInjectionToken),
}),
}) as <Item extends ItemObject, PreLoadStores extends boolean>(props: ItemListLayoutContentProps<Item, PreLoadStores>) => React.ReactElement;
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type { ClusterContext } from "../../cluster-frame-context/cluster-frame-c
import type { GeneralKubeObjectListLayoutColumn, SpecificKubeListLayoutColumn } from "@k8slens/list-layout";
import { kubeObjectListLayoutColumnInjectionToken } from "@k8slens/list-layout";
import { sortBy } from "lodash";
import { TableDataContext } from "@k8slens/table-tokens";

export type KubeItemListStore<K extends KubeObject> = ItemListStore<K, false> & SubscribableStore & {
getByPath: (path: string) => K | undefined;
Expand Down Expand Up @@ -186,7 +187,7 @@ class NonInjectedKubeObjectListLayout<
...targetColumns,
], (v) => -v.priority).map((col) => col.header);

return (
const itemsListLayout = (
<ItemListLayout<K, false>
className={cssNames("KubeObjectListLayout", className)}
store={store}
Expand Down Expand Up @@ -233,6 +234,15 @@ class NonInjectedKubeObjectListLayout<
{...layoutProps}
/>
);

return (
<TableDataContext.Provider
value={{
columns: targetColumns as GeneralKubeObjectListLayoutColumn[],
}}>
{itemsListLayout}
</TableDataContext.Provider>
);
ixrock marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
3 changes: 3 additions & 0 deletions packages/table/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Description

The package exports tokens needed for external table configuration.
37 changes: 37 additions & 0 deletions packages/table/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getInjectionToken } from "@ogre-tools/injectable";
import type { KubeObject } from "@k8slens/kube-object/src/kube-object";
import type {
BaseKubeObjectListLayoutColumn,
GeneralKubeObjectListLayoutColumn,
SpecificKubeListLayoutColumn,
} from "@k8slens/list-layout/src/kube-list-layout-column";
aleksfront marked this conversation as resolved.
Show resolved Hide resolved
import React from "react";

type Column = (
| BaseKubeObjectListLayoutColumn<KubeObject>
| SpecificKubeListLayoutColumn<KubeObject>
| GeneralKubeObjectListLayoutColumn
);

export interface TableComponentProps {
tableId?: string;
columns?: Column[];
save?: (state: object) => void;
load?: (tableId: string) => object;
}

export interface TableDataContextValue {
columns?: Column[];
}

export const TableDataContext = React.createContext<TableDataContextValue>({
columns: [],
});

export interface TableComponent {
Component: React.ComponentType<TableComponentProps>;
}

export const tableComponentInjectionToken = getInjectionToken<TableComponent>({
id: "table-component-injection-token",
});
29 changes: 29 additions & 0 deletions packages/table/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@k8slens/table-tokens",
"version": "6.5.0-alpha.7",
"description": "Injection token exporter for table components",
"license": "MIT",
"type": "commonjs",
"private": false,
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"clean": "rimraf dist/",
"build": "lens-webpack-build"
},
"devDependencies": {
"@k8slens/webpack": "^6.5.0-alpha.8",
"rimraf": "^4.4.1"
},
"peerDependencies": {
"@ogre-tools/injectable": "^16.1.0",
"react": "^17.0.2"
}
}
4 changes: 4 additions & 0 deletions packages/table/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@k8slens/typescript/config/base.json",
"include": ["**/*.ts"]
}
1 change: 1 addition & 0 deletions packages/table/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@k8slens/webpack").configForNode;