diff --git a/webapp/src/components/Inputs/InputYaml.css b/webapp/src/components/Inputs/InputYaml.css new file mode 100644 index 0000000..8ab62ef --- /dev/null +++ b/webapp/src/components/Inputs/InputYaml.css @@ -0,0 +1,37 @@ +.input-yaml { + align-items: flex-start; + position: relative; +} + +.input-yaml label { + position: initial; + margin-bottom: 1em; +} + +.input-yaml [lang=yaml] { + flex: 1; + width: 100%; + min-width: 100%; +} + +.input-yaml .MuiFormHelperText-root { + width: 100%; + max-width: 100%; + display: block; + overflow: hidden; + margin: 0; + padding: 0.25em 0.1em; +} + +.input-yaml .MuiInputAdornment-positionEnd { + position: absolute; + right: 0; + top: 55%; +} + +.input-yaml .input-yaml--placeholder { + position: absolute; + left: 2em; + top: 2.5em; + color: var(--primary-accent); +} \ No newline at end of file diff --git a/webapp/src/components/Inputs/InputYaml.tsx b/webapp/src/components/Inputs/InputYaml.tsx index 290f2a8..0c6fc3d 100644 --- a/webapp/src/components/Inputs/InputYaml.tsx +++ b/webapp/src/components/Inputs/InputYaml.tsx @@ -1,17 +1,24 @@ +import "./InputYaml.css"; import * as yaml from "js-yaml"; -import { useEffect, useState } from "react"; -import { useDelayEffect } from 'utils/useDelay'; - -import CodeMirror from '@uiw/react-codemirror'; -import InputWrapper from "./InputWrapper"; +import { useEffect, useState, ReactNode } from "react"; +import { useDelayEffect } from "utils/useDelay"; +import CodeMirror from "@uiw/react-codemirror"; +import FormControl from "@mui/material/FormControl"; +import InputLabel from "@mui/material/InputLabel"; +import { FormHelperText, InputAdornment } from "@mui/material"; export interface Props { label: string; value: T; onChange: (v: T) => void; - error?: JSX.Element | string, + error?: JSX.Element | string; className?: string; + placeholder?: any; + required?: boolean; + title?: string; + helperText?: ReactNode; + endAdornment?: ReactNode; } export default function InputYaml({ label, @@ -19,45 +26,73 @@ export default function InputYaml({ value = {} as any, onChange, className, + placeholder, + required, + title, + helperText, + endAdornment, }: Props) { const [{ text, error }, setState] = useState<{ - text: string, - error: undefined | JSX.Element | string, + text: string; + error: undefined | JSX.Element | string; }>({ text: yaml.dump(value), error: incmError, }); const setText = (t: string) => { try { - yaml.load(t) - setState({ text: t, error: undefined }) + yaml.load(t); + setState({ text: t, error: undefined }); } catch (_) { - setState({ text: t, error: "! Invalid Yaml !" }) + setState({ text: t, error: "! Invalid Yaml !" }); } - } + }; useEffect(() => { setState({ text: yaml.dump(value), error: incmError, - }) - }, [value, incmError]) - useDelayEffect(() => { - if (!error && (yaml.dump(value) !== text)) { - if (text) { - onChange(yaml.load(text) as any) - } else if (Array.isArray(value)) { - onChange([] as any) - } else { - onChange({} as any) + }); + }, [value, incmError]); + useDelayEffect( + () => { + if (!error && yaml.dump(value) !== text) { + if (text) { + onChange(yaml.load(text) as any); + } else if (Array.isArray(value)) { + onChange([] as any); + } else { + onChange({} as any); + } } - } - }, [text], 1000) - return - - + }, + [text], + 1000 + ); + return ( + + + {label} {required ? "*" : ""} + + + + {!!text ? "" : placeholder} + + {endAdornment} + + {error ? error : helperText} + + + ); } diff --git a/webapp/src/components/ServiceEditor/ServiceEditorField.tsx b/webapp/src/components/ServiceEditor/ServiceEditorField.tsx index a9dc84a..198492c 100644 --- a/webapp/src/components/ServiceEditor/ServiceEditorField.tsx +++ b/webapp/src/components/ServiceEditor/ServiceEditorField.tsx @@ -9,6 +9,7 @@ import { InputAutoComplete, Option } from "components/Inputs/InputAutoComplete"; import { prettyName } from "utils/formatting"; import InputBoolean from "components/Inputs/InputBoolean"; import { SelectorNumber, SelectorText } from "haService/fieldTypes"; +import InputYaml from "components/Inputs/InputYaml"; export type ServiceEditorFieldProps = { option: ServiceEditorOption; @@ -90,6 +91,8 @@ export const ServiceEditorField: FC = ({ return ; } else if ("boolean" in option.data.selector) { return ; + } else if ("object" in option.data.selector) { + return ; } } return ;