Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 2 additions & 10 deletions web-console/src/components/auto-form/auto-form.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,8 @@
* limitations under the License.
*/

@import '../../variables';

.auto-form {
// Popover in info label
label.#{$bp-ns}-label {
position: relative;

.#{$bp-ns}-text-muted {
position: absolute;
right: 0;
}
.custom-input input {
cursor: pointer;
}
}
55 changes: 52 additions & 3 deletions web-console/src/components/auto-form/auto-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@
* limitations under the License.
*/

import { Button, ButtonGroup, FormGroup, Intent, NumericInput } from '@blueprintjs/core';
import {
Button,
ButtonGroup,
FormGroup,
InputGroup,
Intent,
NumericInput,
} from '@blueprintjs/core';
import { IconNames } from '@blueprintjs/icons';
import React from 'react';

Expand Down Expand Up @@ -46,7 +53,8 @@ export interface Field<M> {
| 'boolean'
| 'string-array'
| 'json'
| 'interval';
| 'interval'
| 'custom';
defaultValue?: any;
emptyValue?: any;
suggestions?: Functor<M, Suggestion[]>;
Expand All @@ -64,6 +72,13 @@ export interface Field<M> {
valueAdjustment?: (value: any) => any;
adjustment?: (model: Partial<M>) => Partial<M>;
issueWithValue?: (value: any) => string | undefined;

customSummary?: (v: any) => string;
customDialog?: (o: {
value: any;
onValueChange: (v: any) => void;
onClose: () => void;
}) => JSX.Element;
}

interface ComputedFieldValues {
Expand All @@ -84,6 +99,7 @@ export interface AutoFormProps<M> {

export interface AutoFormState {
showMore: boolean;
customDialog?: JSX.Element;
}

export class AutoForm<T extends Record<string, any>> extends React.PureComponent<
Expand Down Expand Up @@ -395,6 +411,36 @@ export class AutoForm<T extends Record<string, any>> extends React.PureComponent
);
}

private renderCustomInput(field: Field<T>): JSX.Element {
const { model } = this.props;
const { required, defaultValue, modelValue } = AutoForm.computeFieldValues(model, field);
const effectiveValue = modelValue || defaultValue;

const onEdit = () => {
this.setState({
customDialog: field.customDialog?.({
value: effectiveValue,
onValueChange: v => this.fieldChange(field, v),
onClose: () => {
this.setState({ customDialog: undefined });
},
}),
});
};

return (
<InputGroup
className="custom-input"
value={(field.customSummary || String)(effectiveValue)}
intent={required && modelValue == null ? AutoForm.REQUIRED_INTENT : undefined}
readOnly
placeholder={AutoForm.evaluateFunctor(field.placeholder, model, '')}
rightElement={<Button icon={IconNames.EDIT} minimal onClick={onEdit} />}
onClick={onEdit}
/>
);
}

renderFieldInput(field: Field<T>) {
switch (field.type) {
case 'number':
Expand All @@ -413,6 +459,8 @@ export class AutoForm<T extends Record<string, any>> extends React.PureComponent
return this.renderJsonInput(field);
case 'interval':
return this.renderIntervalInput(field);
case 'custom':
return this.renderCustomInput(field);
default:
throw new Error(`unknown field type '${field.type}'`);
}
Expand Down Expand Up @@ -464,7 +512,7 @@ export class AutoForm<T extends Record<string, any>> extends React.PureComponent

render(): JSX.Element {
const { fields, model, showCustom } = this.props;
const { showMore } = this.state;
const { showMore, customDialog } = this.state;

let shouldShowMore = false;
const shownFields = fields.filter(field => {
Expand All @@ -489,6 +537,7 @@ export class AutoForm<T extends Record<string, any>> extends React.PureComponent
{model && shownFields.map(this.renderField)}
{model && showCustom && showCustom(model) && this.renderCustom()}
{shouldShowMore && this.renderMoreOrLess()}
{customDialog}
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@
@import '../../variables';

.form-group-with-info {
label.#{$bp-ns}-label {
position: relative;

.#{$bp-ns}-text-muted {
position: absolute;
right: 0;

// This is only needed because BP4 alerts are too agro in setting CSS on icons
.#{$bp-ns}-icon {
margin-right: 0;
}
}
}

.#{$bp-ns}-text-muted .#{$bp-ns}-popover2-target {
margin-top: 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
overflow: hidden;
text-overflow: ellipsis;

&.disabled {
cursor: not-allowed;
}

.hover-icon {
position: absolute;
top: $table-cell-v-padding;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,23 @@ export interface TableClickableCellProps {
onClick: MouseEventHandler<any>;
hoverIcon?: IconName;
title?: string;
disabled?: boolean;
children?: ReactNode;
}

export const TableClickableCell = React.memo(function TableClickableCell(
props: TableClickableCellProps,
) {
const { className, onClick, hoverIcon, title, children } = props;
const { className, onClick, hoverIcon, title, disabled, children } = props;

return (
<div className={classNames('table-clickable-cell', className)} title={title} onClick={onClick}>
<div
className={classNames('table-clickable-cell', className, { disabled })}
title={title}
onClick={disabled ? undefined : onClick}
>
{children}
{hoverIcon && <Icon className="hover-icon" icon={hoverIcon} />}
{hoverIcon && !disabled && <Icon className="hover-icon" icon={hoverIcon} />}
</div>
);
});
18 changes: 10 additions & 8 deletions web-console/src/components/warning-checklist/warning-checklist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,31 @@
*/

import { Switch } from '@blueprintjs/core';
import React, { useState } from 'react';
import React, { ReactNode, useState } from 'react';

export interface WarningChecklistProps {
checks: string[];
onChange: (allChecked: boolean) => void;
checks: ReactNode[];
onChange(allChecked: boolean): void;
}

export const WarningChecklist = React.memo(function WarningChecklist(props: WarningChecklistProps) {
const { checks, onChange } = props;
const [checked, setChecked] = useState<Record<string, boolean>>({});
const [checked, setChecked] = useState<Record<number, boolean>>({});

function doCheck(check: string) {
function doCheck(checkIndex: number) {
const newChecked = { ...checked };
newChecked[check] = !newChecked[check];
newChecked[checkIndex] = !newChecked[checkIndex];
setChecked(newChecked);

onChange(checks.every(check => newChecked[check]));
onChange(checks.every((_, i) => newChecked[i]));
}

return (
<div className="warning-checklist">
{checks.map((check, i) => (
<Switch key={i} label={check} onChange={() => doCheck(check)} />
<Switch key={i} onChange={() => doCheck(i)}>
{check}
</Switch>
))}
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface AsyncActionDialogProps {
intent?: Intent;
successText: string;
failText: string;
warningChecks?: string[];
warningChecks?: ReactNode[];
children?: ReactNode;
}

Expand Down
Loading