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

Auto select one and only cluster from pasted config (#888)

* Auto select one and only cluster from pasted config

- Disable add button with tooltip if no clusters are selected
- Add functionality to tooltip to display correctly over disabled
  elements

* feature should work of any method of adding kube config

Signed-off-by: Sebastian Malton <sebastian@malton.name>
Co-authored-by: Sebastian Malton <smalton@mirantis.com>
This commit is contained in:
Sebastian Malton 2020-09-21 16:11:43 -04:00 committed by GitHub
parent c82b54d5d7
commit 7f6ae6e8c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 19 deletions

View File

@ -17,10 +17,6 @@ describe("app start", () => {
const addMinikubeCluster = async (app: Application) => { const addMinikubeCluster = async (app: Application) => {
await app.client.click("div.add-cluster") await app.client.click("div.add-cluster")
await app.client.waitUntilTextExists("div", "Select kubeconfig file") await app.client.waitUntilTextExists("div", "Select kubeconfig file")
await app.client.click("div.Select__control")
await app.client.waitUntilTextExists("div", "minikube")
await app.client.click("div.minikube")
await app.client.click("div.Select__control")
await app.client.click("button.primary") await app.client.click("button.primary")
} }

View File

@ -80,7 +80,6 @@ export class AddCluster extends React.Component {
const contexts = this.getContexts(this.kubeConfigLocal); const contexts = this.getContexts(this.kubeConfigLocal);
this.kubeContexts.replace(contexts); this.kubeContexts.replace(contexts);
break; break;
case KubeConfigSourceTab.TEXT: case KubeConfigSourceTab.TEXT:
try { try {
this.error = "" this.error = ""
@ -91,6 +90,10 @@ export class AddCluster extends React.Component {
} }
break; break;
} }
if (this.kubeContexts.size === 1) {
this.selectedContexts.push(this.kubeContexts.keys().next().value)
}
} }
getContexts(config: KubeConfig): Map<string, KubeConfig> { getContexts(config: KubeConfig): Map<string, KubeConfig> {
@ -206,7 +209,7 @@ export class AddCluster extends React.Component {
<Tab <Tab
value={KubeConfigSourceTab.FILE} value={KubeConfigSourceTab.FILE}
label={<Trans>Select kubeconfig file</Trans>} label={<Trans>Select kubeconfig file</Trans>}
active={this.sourceTab == KubeConfigSourceTab.FILE}/> active={this.sourceTab == KubeConfigSourceTab.FILE} />
<Tab <Tab
value={KubeConfigSourceTab.TEXT} value={KubeConfigSourceTab.TEXT}
label={<Trans>Paste as text</Trans>} label={<Trans>Paste as text</Trans>}
@ -320,13 +323,15 @@ export class AddCluster extends React.Component {
return ( return (
<div className={cssNames("kube-context flex gaps align-center", context)}> <div className={cssNames("kube-context flex gaps align-center", context)}>
<span>{context}</span> <span>{context}</span>
{isNew && <Icon small material="fiber_new"/>} {isNew && <Icon small material="fiber_new" />}
{isSelected && <Icon small material="check" className="box right"/>} {isSelected && <Icon small material="check" className="box right" />}
</div> </div>
) )
}; };
render() { render() {
const addDisabled = this.selectedContexts.length === 0
return ( return (
<WizardLayout <WizardLayout
className="AddCluster" className="AddCluster"
@ -374,9 +379,12 @@ export class AddCluster extends React.Component {
<div className="actions-panel"> <div className="actions-panel">
<Button <Button
primary primary
disabled={addDisabled}
label={<Trans>Add cluster(s)</Trans>} label={<Trans>Add cluster(s)</Trans>}
onClick={this.addClusters} onClick={this.addClusters}
waiting={this.isWaiting} waiting={this.isWaiting}
tooltip={addDisabled ? _i18n._("Select at least one cluster to add.") : undefined}
tooltipOverrideDisabled
/> />
</div> </div>
</WizardLayout> </WizardLayout>

View File

@ -54,8 +54,7 @@
form:not([novalidate]):invalid &[type=submit]:not(.active), form:not([novalidate]):invalid &[type=submit]:not(.active),
&:disabled { &:disabled {
color: silver; opacity: 50%;
background: $buttonDisabledBackground;
pointer-events: none; pointer-events: none;
} }

View File

@ -15,6 +15,7 @@ export enum TooltipPosition {
export interface TooltipProps { export interface TooltipProps {
targetId: string; // html-id of target element to bind for targetId: string; // html-id of target element to bind for
tooltipOnParentHover?: boolean; // detect hover on parent of target
visible?: boolean; // initial visibility visible?: boolean; // initial visibility
offset?: number; // offset from target element in pixels (all sides) offset?: number; // offset from target element in pixels (all sides)
usePortal?: boolean; // renders element outside of parent (in body), disable for "easy-styling", default: true usePortal?: boolean; // renders element outside of parent (in body), disable for "easy-styling", default: true
@ -50,14 +51,22 @@ export class Tooltip extends React.Component<TooltipProps> {
return document.getElementById(this.props.targetId) return document.getElementById(this.props.targetId)
} }
get hoverTarget(): HTMLElement {
if (this.props.tooltipOnParentHover) {
return this.targetElem.parentElement
}
return this.targetElem
}
componentDidMount() { componentDidMount() {
this.targetElem.addEventListener("mouseenter", this.onEnterTarget) this.hoverTarget.addEventListener("mouseenter", this.onEnterTarget)
this.targetElem.addEventListener("mouseleave", this.onLeaveTarget) this.hoverTarget.addEventListener("mouseleave", this.onLeaveTarget)
} }
componentWillUnmount() { componentWillUnmount() {
this.targetElem.removeEventListener("mouseenter", this.onEnterTarget) this.hoverTarget.removeEventListener("mouseenter", this.onEnterTarget)
this.targetElem.removeEventListener("mouseleave", this.onLeaveTarget) this.hoverTarget.removeEventListener("mouseleave", this.onLeaveTarget)
} }
@autobind() @autobind()

View File

@ -8,6 +8,11 @@ import uniqueId from "lodash/uniqueId"
export interface TooltipDecoratorProps { export interface TooltipDecoratorProps {
tooltip?: ReactNode | Omit<TooltipProps, "targetId">; tooltip?: ReactNode | Omit<TooltipProps, "targetId">;
/**
* forces tooltip to detect the target's parent for mouse events. This is
* useful for displaying tooltips even when the target is "disabled"
*/
tooltipOverrideDisabled?: boolean;
} }
export function withTooltip<T extends React.ComponentType<any>>(Target: T): T { export function withTooltip<T extends React.ComponentType<any>>(Target: T): T {
@ -17,22 +22,25 @@ export function withTooltip<T extends React.ComponentType<any>>(Target: T): T {
protected tooltipId = uniqueId("tooltip_target_"); protected tooltipId = uniqueId("tooltip_target_");
render() { render() {
const { tooltip, ...targetProps } = this.props; const { tooltip, tooltipOverrideDisabled, ...targetProps } = this.props;
if (tooltip) { if (tooltip) {
const tooltipId = targetProps.id || this.tooltipId; const tooltipId = targetProps.id || this.tooltipId;
const tooltipProps: TooltipProps = { const tooltipProps: TooltipProps = {
targetId: tooltipId, targetId: tooltipId,
tooltipOnParentHover: tooltipOverrideDisabled,
...(isReactNode(tooltip) ? { children: tooltip } : tooltip), ...(isReactNode(tooltip) ? { children: tooltip } : tooltip),
}; };
targetProps.id = tooltipId; targetProps.id = tooltipId;
targetProps.children = ( targetProps.children = (
<> <>
{targetProps.children} <div>
<Tooltip {...tooltipProps}/> {targetProps.children}
</div>
<Tooltip {...tooltipProps} />
</> </>
) )
} }
return <Target {...targetProps as any}/>; return <Target {...targetProps as any} />;
} }
} }