Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit d8d13ab

Browse files
myan9starpit
authored andcommitted
feat: add a command to return the Card Component in Terminal
Fixes #4973
1 parent 2307a46 commit d8d13ab

File tree

12 files changed

+216
-12
lines changed

12 files changed

+216
-12
lines changed

packages/test/src/api/selectors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ export const WATCHER_N_DROPDOWN_ITEM = (N: number, label: string) =>
155155
export const WATCHER_N_CLOSE = (N: number) => WATCHER_N_DROPDOWN_ITEM(N, 'Stop watching')
156156
export const WATCHER_N_SHOW_AS_TABLE = (N: number) => WATCHER_N_DROPDOWN_ITEM(N, 'Show as table')
157157

158+
// terminal card
159+
export const TERMINAl_CARD = `.kui--card`
160+
158161
/**
159162
* Terminal splits
160163
*
Loading

plugins/plugin-client-common/src/components/spi/Card/impl/PatternFly.tsx

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
import * as React from 'react'
1818
import {
19+
Brand,
1920
Card,
2021
CardActions,
2122
CardBody,
2223
CardHeader,
2324
CardHeaderMain,
25+
CardTitle,
2426
Dropdown,
2527
DropdownItem,
2628
KebabToggle
@@ -29,7 +31,7 @@ import {
2931
import Props from '../model'
3032
import { DropDownAction } from '../../DropDown'
3133

32-
import '../../../../../web/scss/components/Card/Patternfly.scss'
34+
import '../../../../../web/scss/components/Card/PatternFly.scss'
3335

3436
interface State {
3537
isOpen: boolean
@@ -73,16 +75,41 @@ export default class PatternflyCard extends React.PureComponent<Props, State> {
7375
)
7476
}
7577

78+
private icon() {
79+
return <Brand src={this.props.icon} alt="card icon" className="kui--card-icon" />
80+
}
81+
82+
/** card actions, icon and custom header node will be situated in Card Head */
83+
private header() {
84+
if (this.props.header || this.props.actions || this.props.icon) {
85+
return (
86+
<CardHeader>
87+
<CardHeaderMain>{this.props.header || (this.props.icon && this.icon())}</CardHeaderMain>
88+
{this.props.actions && this.cardActions()}
89+
</CardHeader>
90+
)
91+
}
92+
}
93+
94+
private title() {
95+
if (this.props.title) {
96+
return <CardTitle> {this.props.title} </CardTitle>
97+
}
98+
}
99+
76100
public render() {
101+
const basicClassName = 'kui--card small-top-pad small-bottom-pad'
77102
return (
78-
<Card className={this.props.className}>
103+
<Card className={!this.props.className ? basicClassName : `${basicClassName} ${this.props.className}`}>
79104
{React.Children.count(this.props.children) > 0 && (
80105
<React.Fragment>
81-
<CardHeader>
82-
<CardHeaderMain>{this.props.header}</CardHeaderMain>
83-
{this.props.actions && this.cardActions()}
84-
</CardHeader>
85-
<CardBody>{this.props.children}</CardBody>
106+
{this.header()}
107+
{this.title()}
108+
{Array.isArray(this.props.children) ? (
109+
this.props.children.map((child, idx) => <CardBody key={idx}>{child}</CardBody>)
110+
) : (
111+
<CardBody>{this.props.children}</CardBody>
112+
)}
86113
</React.Fragment>
87114
)}
88115
</Card>

plugins/plugin-client-common/src/components/spi/Card/index.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@
1515
*/
1616

1717
import * as React from 'react'
18-
19-
import PatternFly4 from './impl/PatternFly'
20-
2118
import Props from './model'
2219

20+
/**
21+
* PatternFly4 Card Component. We use React.lazy import here because
22+
* the @patternfly/react-core/src/components/Card code import css files,
23+
* which will result in the `card` command fails at kui-prescan stage.
24+
*
25+
*/
26+
const PatternFly4 = React.lazy(() => import('./impl/PatternFly'))
27+
2328
// FIXME There's no ideal Card component in Carbon Component Libary, so we use Patternfly
2429
export default function CardSpi(props: Props): React.ReactElement {
25-
return <PatternFly4 {...props} />
30+
return (
31+
<React.Suspense fallback={<div />}>
32+
<PatternFly4 {...props} />
33+
</React.Suspense>
34+
)
2635
}

plugins/plugin-client-common/src/components/spi/Card/model.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ import { DropDownAction } from '../DropDown'
2020

2121
interface Props {
2222
/** Place the given header node at the top of the Card */
23-
header: ReactNode
23+
header?: ReactNode
24+
25+
/** Content rendered inside the CardTitle */
26+
title?: string
27+
28+
/** Place the given icon image at the top of the Card */
29+
icon?: string
2430

2531
/** Force children to be a required property */
2632
children: ReactNode
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2020 IBM Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { Arguments, Registrar, ParsedOptions, UsageModel, ReactResponse } from '@kui-shell/core'
18+
import card from '../components/spi/Card'
19+
20+
/**
21+
* card command parsedOptions type
22+
*/
23+
interface CardOptions extends ParsedOptions {
24+
title: string
25+
icon: string
26+
}
27+
28+
/**
29+
* card command usage
30+
*/
31+
const usage: UsageModel = {
32+
command: 'card',
33+
strict: 'card',
34+
example: 'card [<card body text>] [--title <card title text>]',
35+
docs: 'Card',
36+
required: [
37+
{
38+
name: 'body',
39+
docs: 'card body text'
40+
}
41+
],
42+
optional: [
43+
{
44+
name: '--title',
45+
docs: 'Content rendered inside the CardTitle'
46+
},
47+
{
48+
name: '--icon',
49+
docs: 'Attribute that specifies the URL of the image to put on the card.'
50+
}
51+
]
52+
}
53+
54+
/**
55+
* card command handler
56+
*
57+
*/
58+
function doCard(opts: Arguments<CardOptions>): ReactResponse {
59+
const argv = opts.argvNoOptions
60+
const option = opts.parsedOptions
61+
const body = argv.slice(1)
62+
const { title, icon } = option
63+
64+
return { react: card({ title, children: body, icon }) }
65+
}
66+
67+
/**
68+
* This plugin introduces the /card command
69+
*
70+
*/
71+
export default async (commandTree: Registrar) => {
72+
commandTree.listen('/card', doCard, { usage })
73+
}

plugins/plugin-client-common/src/plugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ export default async (registrar: Registrar) => {
2121
await import(/* webpackMode: "lazy" */ './controller/confirm').then(_ => _.default(registrar))
2222
await import(/* webpackMode: "lazy" */ './controller/split').then(_ => registrar.listen('/split', _.default))
2323
await import(/* webpackMode: "lazy" */ './controller/alert').then(_ => _.default(registrar))
24+
await import(/* webpackMode: "lazy" */ './controller/card').then(_ => _.default(registrar))
2425
}
2526
}

plugins/plugin-client-common/web/scss/components/Alert/Carbon.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
background-color: var(--color-repl-background);
2323
color: var(--color-text-01);
2424
width: inherit;
25+
white-space: normal;
2526
height: auto;
2627

2728
.bx--toast-notification__title,

plugins/plugin-client-common/web/scss/components/Card/Patternfly.scss renamed to plugins/plugin-client-common/web/scss/components/Card/PatternFly.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,12 @@
4848
}
4949
}
5050
}
51+
52+
[kui-theme-style] .kui--card {
53+
white-space: normal;
54+
55+
.kui--card-icon {
56+
width: 10em;
57+
height: 7.43em;
58+
}
59+
}
Loading

0 commit comments

Comments
 (0)