Skip to content

Commit

Permalink
add enum field support (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
agoelzer authored Nov 5, 2024
1 parent 57f4ba3 commit 7ba9a04
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 27 deletions.
4 changes: 2 additions & 2 deletions ui/src/components/fields/FieldBoolean.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export default function FieldBoolean(props: FieldProps): FieldBaseType {
}

function getDisplayValue(): ReactNode {
const { prefix, values } = props;
const { prefix, values, t } = props;
const value = getValue(values, prefix);
return value ? "true" : "false";
return value ? t("button.true") : t("button.false");
}

return { ...FieldBase(props), show, getDisplayValue };
Expand Down
6 changes: 5 additions & 1 deletion ui/src/components/fields/FieldFactory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import FieldMessage from "./FieldMessage";
import FieldDateTime from "./FieldDateTime";
import { FieldBaseType, FieldProps, FieldPropsDisplay, FieldPropsValidate } from "./FieldBase";
import { ReactNode } from "react";
import FieldSelect from "./FieldSelect";

/** Factory function to create components based on the field type */
const FieldFactory = {
create: (props: FieldProps): FieldBaseType => {
props = { ...props };
let leftOvers = JSON.parse(JSON.stringify(props.parameter));
let leftOvers = JSON.parse(JSON.stringify(props.parameter || {}));
if (!("schema" in leftOvers)) {
leftOvers["schema"] = {};
}
Expand Down Expand Up @@ -59,6 +60,9 @@ const FieldFactory = {
else if (props.parameter["x-tf-sensitive"] === true) {
return FieldPassword(props);
}
else if (props.parameter.enum) {
return FieldSelect(props);
}
else {
return FieldString(props);
}
Expand Down
10 changes: 5 additions & 5 deletions ui/src/components/fields/FieldSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ export default function FieldSelect(props: FieldProps): FieldBaseType {
* show Field of type Boolean using the values and schema definition
*/
function show(): ReactNode {
const { prefix, label, values, parameter, required, setValues, autoFocus, readonly } = props;
const { prefix, label, values, parameter, required, setValues, autoFocus, readonly, t } = props;
let value = getValue(values, prefix);

return <Select id={prefix} key={prefix} label={label} value={value} autoFocus={autoFocus} required={required} onChange={(e: any) => {
let v = { ...values };
setValue(v, prefix, e.target.value);
setValues(v);
}} onBlur={() => FieldBase(props).validate()} disabled={readonly}>
<SelectOption value="">--- Select Item ---</SelectOption>
{parameter && parameter.enums && parameter.enums.map(e => <SelectOption key={e.key} value={e.key}>{e.label}</SelectOption>)}
<SelectOption value="">{t("field.select.selectItem")}</SelectOption>
{parameter && parameter.enum && parameter.enum.map(e => <SelectOption key={e} value={e}>{t("field.enum." + prefix + "." + e, prefix + "." + e)}</SelectOption>)}
</Select>;
}

function getDisplayValue(): ReactNode {
const { prefix, values } = props;
const { prefix, values, t } = props;
const value = getValue(values, prefix);
return value ? "true" : "false";
return t("field.enum." + prefix + "." + value, prefix + "." + value);
}

return { ...FieldBase(props), show, getDisplayValue };
Expand Down
5 changes: 1 addition & 4 deletions ui/src/components/pages/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ function Settings({ t }: SettingsProps) {
label: t("form.settings.label.theme"),
parameter: {
type: "select",
enums: [
{ key: "material", label: "Material UI" },
{ key: "plain", label: "plain" }
]
enum: ["material", "plain"]
},
t

Expand Down
35 changes: 21 additions & 14 deletions ui/src/components/pages/parts/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { TableBody, TableTh, TableCell, Table as TableCustom, TableHead, TableRo
import { getResourceByPath, getCreatePath, getChild, replaceVariables } from "../../../utils/schema";
import FieldFactory from "../../fields/FieldFactory";
import RestSpinner from "./RestSpinner";
import { getValue } from "../../fields/utils";
import Dialog from "./Dialog";
import { MenuItemProps, TempAny } from "../../../utils/types";
import { CustomViewField, evaluate, getCustomizationsView } from '../../../utils/Customizations';
Expand Down Expand Up @@ -217,6 +216,20 @@ function Table(props: TempAny) {

}

/* gets the schema of the specified field.
If the field is hierarchical, it will find the schema of the right most field.
Returns defaults if not found. */
function getFieldSchema(fieldName: string) {
const fieldsSchema = getChild(getResourceByPath(schema, getCreatePath(schema, path)), ["get", "responses", "200", "content", "application/json", "schema", "properties"]);
let fs = fieldsSchema;
let fn = fieldName;
while (fs && fn.includes(".") && fn.split(".")[0] in fs) {
fs = fs[fn.split(".")[0]].properties;
fn = fn.substring(fn.indexOf(".") + 1);
}
return fs[fn] || {};
}

function renderDataCell(fieldName: string, row: TempAny) {
const cv = getCustomizationsView(path)
const cf: CustomViewField | null = (cv && cv.fields && cv.fields[fieldName]) || null;
Expand All @@ -234,26 +247,20 @@ function Table(props: TempAny) {
}
}
else {
if (fieldsSchema && fieldName in fieldsSchema) {
value = FieldFactory.createDisplayValue({
prefix: fieldName,
label: t("field.label." + fieldName, fieldName),
parameter: fieldsSchema[fieldName],
values: row,
t
});
}
else {
value = showValue(getValue(row, fieldName));
}
value = FieldFactory.createDisplayValue({
prefix: fieldName,
label: t("field.label." + fieldName, fieldName),
parameter: getFieldSchema(fieldName),
values: row,
t
});
}

return <TableCell key={fieldName}>{value}</TableCell>;

}

const tableLabels = getTableLabels();
const fieldsSchema = getChild(getResourceByPath(schema, getCreatePath(schema, path)), ["get", "responses", "200", "content", "application/json", "schema", "properties"]);
const visibleColumns = columns.filter(col => col.selected);
return (
<TableCustom data-testid={props["data-testid"]}>
Expand Down
32 changes: 32 additions & 0 deletions ui/src/resources/translation/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,43 @@
"frequency": "Frequenz",
"selector": "Auswahl"
},
"enum": {
"theme.material": "Material UI",
"theme.plain": "einfach",
"retention.settings": {
"dayOfWeek": {
"Sunday": "Sonntag",
"Monday": "Montag",
"Tuesday": "Dienstag",
"Wednesday": "Mittwoch",
"Thursday": "Donnerstag",
"Friday": "Freitag",
"Saturday": "Samstag"
},
"month": {
"January": "Januar",
"February": "Februar",
"March": "März",
"April": "April",
"May": "Mai",
"June": "Juni",
"July": "Juli",
"August": "August",
"September": "September",
"October": "Oktober",
"November": "November",
"December": "Dezember"
}
}
},
"map": {
"header.label.key": "Schlüssel",
"header.label.value": "Wert",
"hint.new.key": "Neuer Schlüssel",
"hint.new.value": "Neuer Wert"
},
"select": {
"selectItem": "--- Eintrag auswählen ---"
}
},
"form": {
Expand Down
38 changes: 38 additions & 0 deletions ui/src/resources/translation/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@
"database": "Database",
"frequency": "Frequency",
"selector": "Selector",
"selector.scope": "Scope",
"selector.slas": "SLAs",
"selector.tiers": "Tiers",
"selector.labels": "Labels",
"properties.propagatePolicyLabels": "Propagate policy labels",
"properties.propagateDatabaseLabels": "Propagate database labels",
"retention": "Retention",
"retention.hourly": "Hourly",
"retention.daily": "Daily",
Expand All @@ -80,11 +86,43 @@
"retention.settings.promoteLatestToDaily": "Promote latest to daily",
"retention.settings.promoteLatestToMonthly": "Promote latest to monthly"
},
"enum": {
"theme.material": "Material UI",
"theme.plain": "plain",
"retention.settings": {
"dayOfWeek": {
"Sunday": "Sunday",
"Monday": "Monday",
"Tuesday": "Tuesday",
"Wednesday": "Wednesday",
"Thursday": "Thursday",
"Friday": "Friday",
"Saturday": "Saturday"
},
"month": {
"January": "January",
"February": "February",
"March": "March",
"April": "April",
"May": "May",
"June": "June",
"July": "July",
"August": "August",
"September": "September",
"October": "October",
"November": "November",
"December": "December"
}
}
},
"map": {
"header.label.key": "Key",
"header.label.value": "Value",
"hint.new.key": "new key",
"hint.new.value": "new value"
},
"select": {
"selectItem": "--- Select Item ---"
}
},
"form": {
Expand Down
2 changes: 1 addition & 1 deletion ui/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type FieldParameterType = {
items?: FieldParameterType,
properties?: FieldParametersType,
additionalProperties?: FieldParameterType
enums?: {key: string, label: string}[],
enum?: string[],
schema?: {
default: boolean|string,
type: string
Expand Down

0 comments on commit 7ba9a04

Please sign in to comment.