2021-09-20 15:31:05 +00:00
|
|
|
import { Controller, useFormContext } from "react-hook-form";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { FormGroup } from "@patternfly/react-core";
|
|
|
|
import { CodeEditor, Language } from "@patternfly/react-code-editor";
|
|
|
|
|
2021-11-08 09:46:03 +00:00
|
|
|
import { HelpItem } from "../help-enabler/HelpItem";
|
2021-09-20 15:31:05 +00:00
|
|
|
import type { ComponentProps } from "./components";
|
2022-08-09 08:32:16 +00:00
|
|
|
import { convertToName } from "./DynamicComponents";
|
2021-09-20 15:31:05 +00:00
|
|
|
|
|
|
|
export const ScriptComponent = ({
|
|
|
|
name,
|
|
|
|
label,
|
|
|
|
helpText,
|
|
|
|
defaultValue,
|
2022-02-16 14:53:45 +00:00
|
|
|
isDisabled = false,
|
2021-09-20 15:31:05 +00:00
|
|
|
}: ComponentProps) => {
|
2021-11-08 09:46:03 +00:00
|
|
|
const { t } = useTranslation("dynamic");
|
2021-09-20 15:31:05 +00:00
|
|
|
const { control } = useFormContext();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormGroup
|
|
|
|
label={t(label!)}
|
|
|
|
labelIcon={
|
2021-09-27 10:32:16 +00:00
|
|
|
<HelpItem
|
|
|
|
helpText={<span style={{ whiteSpace: "pre-wrap" }}>{helpText}</span>}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId={`dynamic:${label}`}
|
2021-09-27 10:32:16 +00:00
|
|
|
/>
|
2021-09-20 15:31:05 +00:00
|
|
|
}
|
|
|
|
fieldId={name!}
|
|
|
|
>
|
|
|
|
<Controller
|
2022-08-09 08:32:16 +00:00
|
|
|
name={convertToName(name!)}
|
2021-09-20 15:31:05 +00:00
|
|
|
defaultValue={defaultValue}
|
|
|
|
control={control}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<CodeEditor
|
|
|
|
id={name!}
|
2021-10-26 20:16:19 +00:00
|
|
|
data-testid={name}
|
2022-02-16 14:53:45 +00:00
|
|
|
isReadOnly={isDisabled}
|
2021-09-20 15:31:05 +00:00
|
|
|
type="text"
|
|
|
|
onChange={onChange}
|
|
|
|
code={value}
|
|
|
|
height="600px"
|
|
|
|
language={Language.javascript}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
);
|
|
|
|
};
|