/* eslint-disable */ // @ts-nocheck import { ActionList, ActionListItem, Button, EmptyState, EmptyStateBody, EmptyStateFooter, Grid, GridItem, HelperText, HelperTextItem, TextInput, } from "../../../shared/@patternfly/react-core"; import { MinusCircleIcon, PlusCircleIcon } from "../../../shared/@patternfly/react-icons"; import { Fragment, FunctionComponent, PropsWithChildren } from "react"; import { FieldValues, useFieldArray, useFormContext, useWatch, } from "react-hook-form"; import { useTranslation } from "react-i18next"; export type DefaultValue = { key: string; values?: string[]; label: string; }; type Field = { name: string; error: boolean; }; type ValueField = Field & { keyValue: string; }; type KeyValueInputProps = PropsWithChildren & { name: string; label?: string; isDisabled?: boolean; keyLabel?: string; valueLabel?: string; KeyComponent?: FunctionComponent; ValueComponent?: FunctionComponent; filterKeys?: string[]; }; export const KeyValueInput = ({ name, label = "attributes", isDisabled = false, keyLabel = "key", valueLabel = "value", KeyComponent, ValueComponent, filterKeys, }: KeyValueInputProps) => { const { t } = useTranslation(); const { control, register, formState: { errors }, } = useFormContext(); const { fields, append, remove } = useFieldArray({ control, name, }); const appendNew = () => append({ key: "", value: "" }); const values = useWatch({ name, control, defaultValue: [], }); const getError = () => { return name.split(".").reduce((record: any, key) => record?.[key], errors); }; return fields.length > 0 ? ( <> {t(keyLabel)} {t(valueLabel)} {fields.map((attribute, index) => { if (filterKeys?.includes(values[index]?.key)) return null; const error = getError()?.[index]; const keyError = !!error?.key; const valueErrorPresent = !!error?.value || !!error?.message; const valueError = error?.message || t(`${valueLabel}Error`); return ( {KeyComponent ? ( ) : ( )} {keyError && ( {t(`${keyLabel}Error`)} )} {ValueComponent ? ( ) : ( )} {valueErrorPresent && ( {valueError} )} ); })} ) : ( {t("missingAttributes", { label })} ); };