import React, { useEffect, useState } from "react" import { useTranslation } from "react-i18next" import Button from "../../fundamentals/button" import PlusIcon from "../../fundamentals/icons/plus-icon" import TrashIcon from "../../fundamentals/icons/trash-icon" import InputField from "../../molecules/input" type AddMetadataProps = { metadata: MetadataField[] setMetadata: (metadata: MetadataField[]) => void heading?: string } export type MetadataField = { key: string value: string } const Metadata: React.FC = ({ metadata, setMetadata, heading = "Metadata", }) => { const { t } = useTranslation() const [localData, setLocalData] = useState([]) useEffect(() => { setLocalData(metadata) }, [metadata]) const addKeyPair = () => { setMetadata([...metadata, { key: ``, value: `` }]) } const onKeyChange = (index: number) => { return (key: string) => { const newFields = metadata newFields[index] = { key: key, value: newFields[index].value } setMetadata(newFields) } } const onValueChange = (index: number) => { return (value: any) => { const newFields = metadata newFields[index] = { key: newFields[index].key, value: value, } setMetadata(newFields) } } const deleteKeyPair = (index: number) => { return () => { setMetadata(metadata.filter((_, i) => i !== index)) } } return (
{heading}
{localData.map((field, index) => { return ( ) })}
) } type FieldProps = { field: MetadataField updateKey: (key: string) => void updateValue: (value: string) => void } const Field: React.FC = ({ field, updateKey, updateValue }) => { return (
{ updateKey(e.currentTarget.value) }} />
{ updateValue(e.currentTarget.value) }} />
) } type DeletableElementProps = { onDelete: () => void children?: React.ReactNode } const DeletableElement: React.FC = ({ onDelete, children, }) => { return (
{children}
) } export default Metadata