updated inputyaml

This commit is contained in:
ari committed 2022-06-19 00:53:01 -04:00
1 parent 19a2d10a2f
commit 7154f206b4
3 files changed
+107 -32

No files matched your search

@@ -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);
}
+67 -32
View File
@@ -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<T extends {}> {
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<T extends {}>({
label,
@@ -19,45 +26,73 @@ export default function InputYaml<T extends {}>({
value = {} as any,
onChange,
className,
placeholder,
required,
title,
helperText,
endAdornment,
}: Props<T>) {
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 <InputWrapper className={className} label={label} error={error}>
<CodeMirror
theme={"dark"}
value={text}
lang="yaml"
onChange={setText}
/>
</InputWrapper>
},
[text],
1000
);
return (
<FormControl
className={["input-yaml", className ?? ""].join(" ")}
fullWidth
title={title}
>
<InputLabel>
{label} {required ? "*" : ""}
</InputLabel>
<CodeMirror
theme={"dark"}
value={text}
lang="yaml"
onChange={setText}
maxWidth="100%"
width="100%"
minWidth="100%"
/>
<span className="input-yaml--placeholder">
{!!text ? "" : placeholder}
</span>
<InputAdornment position="end">{endAdornment}</InputAdornment>
<FormHelperText variant="filled" error={!!error}>
{error ? error : helperText}
</FormHelperText>
</FormControl>
);
}
@@ -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<ServiceEditorFieldProps> = ({
return <InputText {...prop} multiline={options.multiline} />;
} else if ("boolean" in option.data.selector) {
return <InputBoolean {...prop} />;
} else if ("object" in option.data.selector) {
return <InputYaml {...prop} />;
}
}
return <InputText {...prop} />;