/* eslint-disable */ // @ts-nocheck import { HelpItem, KeycloakSelect, SelectVariant, } from "../../../shared/keycloak-ui-shared"; import { ActionList, ActionListItem, Button, EmptyState, EmptyStateBody, EmptyStateFooter, Flex, FlexItem, FormGroup, SelectOption, TextInput, } from "../../../shared/@patternfly/react-core"; import { MinusCircleIcon, PlusCircleIcon } from "../../../shared/@patternfly/react-icons"; import { useEffect, useState } from "react"; import { useFormContext } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { KeyValueType } from "../key-value-form/key-value-convert"; import type { ComponentProps } from "./components"; export const MapComponent = ({ name, label, helpText, required, isDisabled, defaultValue, options, convertToName, }: ComponentProps) => { const { t } = useTranslation(); const { watch, setValue } = useFormContext(); const [map, setMap] = useState([]); const fieldName = convertToName(name!); const value = watch(fieldName, defaultValue || "[]"); useEffect(() => { const values: KeyValueType[] = JSON.parse(value ? value : "[]"); setMap(values); }, [value]); const appendNew = () => setMap([...map, { key: "", value: "" }]); const update = (val = map) => { const v = val.filter((e) => e.key !== ""); setValue(fieldName, v.length > 0 ? JSON.stringify(v) : ""); }; const updateKey = (index: number, key: string) => { return updateEntry(index, { ...map[index], key }); }; const updateValue = (index: number, value: string) => { return updateEntry(index, { ...map[index], value }); }; const updateEntry = (index: number, entry: KeyValueType) => { const newMap = [...map.slice(0, index), entry, ...map.slice(index + 1)]; setMap(newMap); return newMap; }; const remove = (index: number) => { const value = [...map.slice(0, index), ...map.slice(index + 1)]; setMap(value); update(value); }; const [open, setOpen] = useState(-1); return ( } fieldId={name!} isRequired={required} > {map.length !== 0 ? ( <> {t("key")} {t("value")} {map.map((attribute, index) => ( {options ? ( setOpen(v ? index : -1)} selections={attribute.key} data-testid={`${fieldName}.${index}.key`} onSelect={(value) => { update(updateKey(index, value.toString())); }} isOpen={open === index} className={ attribute.key && !options.includes(attribute.key) ? "pf-m-danger" : "" } > {t("choose")} {options.map((option, index) => ( {option} ))} ) : ( updateKey(index, value)} onBlur={() => update()} /> )} updateValue(index, value)} onBlur={() => update()} /> ))} ) : ( {t("missingAttributes", { label: t(label!) })} )} ); };