2021-09-20 15:31:05 +00:00
|
|
|
import React from "react";
|
|
|
|
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";
|
|
|
|
|
|
|
|
export const ScriptComponent = ({
|
|
|
|
name,
|
|
|
|
label,
|
|
|
|
helpText,
|
|
|
|
defaultValue,
|
|
|
|
}: 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
|
2021-12-08 15:08:42 +00:00
|
|
|
name={`config.${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}
|
2021-09-20 15:31:05 +00:00
|
|
|
type="text"
|
|
|
|
onChange={onChange}
|
|
|
|
code={value}
|
|
|
|
height="600px"
|
|
|
|
language={Language.javascript}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
);
|
|
|
|
};
|