import React from "react"; import { useTranslation } from "react-i18next"; import { ArrayField, UseFormMethods } from "react-hook-form"; import { ActionGroup, Button, TextInput } from "@patternfly/react-core"; import { TableComposable, Tbody, Td, Th, Thead, Tr, } from "@patternfly/react-table"; import { MinusCircleIcon, PlusCircleIcon } from "@patternfly/react-icons"; import { FormAccess } from "../form-access/FormAccess"; import "./attribute-form.css"; export type KeyValueType = { key: string; value: string }; export type AttributeForm = { attributes: KeyValueType[]; }; export type AttributesFormProps = { form: UseFormMethods; save: (model: AttributeForm) => void; reset: () => void; array: { fields: Partial, "id">>[]; append: ( value: Partial> | Partial>[], shouldFocus?: boolean | undefined ) => void; remove: (index?: number | number[] | undefined) => void; }; }; export const arrayToAttributes = (attributeArray: KeyValueType[]) => { const initValue: { [index: string]: string[] } = {}; return attributeArray.reduce((acc, attribute) => { acc[attribute.key] = [attribute.value]; return acc; }, initValue); }; export const attributesToArray = (attributes?: { [key: string]: string[]; }): KeyValueType[] => { if (!attributes || Object.keys(attributes).length == 0) { return []; } return Object.keys(attributes).map((key) => ({ key: key, value: attributes[key][0], })); }; export const AttributesForm = ({ form: { handleSubmit, register, formState, errors, watch }, array: { fields, append, remove }, reset, save, }: AttributesFormProps) => { const { t } = useTranslation("roles"); const columns = ["Key", "Value"]; const watchFirstKey = watch("attributes[0].key", ""); return ( <> {columns[0]} {columns[1]} {fields.map((attribute, rowIndex) => ( {rowIndex !== fields.length - 1 && fields.length - 1 !== 0 && ( )} {rowIndex === fields.length - 1 && ( {fields[rowIndex].key === "" && ( )} ); };