import "./InputYaml.css"; import * as yaml from "js-yaml"; import { useEffect, useState, ReactNode } from "react"; import CodeMirror from "@uiw/react-codemirror"; import FormControl from "@mui/material/FormControl"; import InputLabel from "@mui/material/InputLabel"; import FormHelperText from "@mui/material/FormHelperText"; import InputAdornment from "@mui/material/InputAdornment"; import { Save } from "@mui/icons-material"; import { ButtonIcon } from "components/Icons/ButtonIcons"; export interface Props { label: string; value: T; onChange: (v: T) => void; error?: JSX.Element | string; className?: string; placeholder?: any; required?: boolean; title?: string; helperText?: ReactNode; endAdornment?: ReactNode; } export default function InputYaml({ label, error: incmError, value = {} as any, onChange, className, placeholder, required, title, helperText, endAdornment, }: Props) { const originalText = yaml.dump(value); const [{ text, error }, setState] = useState<{ text: string; error: undefined | JSX.Element | string; }>({ text: originalText, error: incmError, }); const hasChanged = text !== originalText; const setText = (t: string) => { try { yaml.load(t); setState({ text: t, error: undefined }); } catch (_) { setState({ text: t, error: "! Invalid Yaml !" }); } }; const onSave = () => { 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); } } }; useEffect(() => { setState({ text: yaml.dump(value), error: incmError, }); }, [value, incmError]); return ( {label} {required ? "*" : ""} {!!text ? "" : placeholder} onSave()} title={!!error ? "Invalid YAML" : hasChanged ? "Save" : ""} icon={ } /> {endAdornment} {error ? error : helperText} ); }